/[gentoo-alt]/trunk/overlay/sys-devel/binutils/files/binutils-2.15-fbsd.patch
Gentoo

Contents of /trunk/overlay/sys-devel/binutils/files/binutils-2.15-fbsd.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1083 - (show annotations) (download)
Mon Feb 20 21:25:47 2006 UTC (8 years, 4 months ago) by flameeyes
File size: 277133 byte(s)
Add binutils 2.16.1 and 2.16.91.6 with FreeBSD hints file loading patches.
1 Index: binutils-2.16.1/bfd/elf32-i386-fbsd.c
2 ===================================================================
3 --- /dev/null
4 +++ binutils-2.16.1/bfd/elf32-i386-fbsd.c
5 @@ -0,0 +1,56 @@
6 +/* Intel IA-32 specific support for 32-bit ELF on FreeBSD.
7 + Copyright 2002 Free Software Foundation, Inc.
8 +
9 +This file is part of BFD, the Binary File Descriptor library.
10 +
11 +This program is free software; you can redistribute it and/or modify
12 +it under the terms of the GNU General Public License as published by
13 +the Free Software Foundation; either version 2 of the License, or
14 +(at your option) any later version.
15 +
16 +This program is distributed in the hope that it will be useful,
17 +but WITHOUT ANY WARRANTY; without even the implied warranty of
18 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 +GNU General Public License for more details.
20 +
21 +You should have received a copy of the GNU General Public License
22 +along with this program; if not, write to the Free Software
23 +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 +
25 +#define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
26 +#define TARGET_LITTLE_NAME "elf32-i386-freebsd"
27 +#define ELF_ARCH bfd_arch_i386
28 +#define ELF_MACHINE_CODE EM_386
29 +#define ELF_MAXPAGESIZE 0x1000
30 +
31 +#include "bfd.h"
32 +#include "sysdep.h"
33 +#include "elf-bfd.h"
34 +
35 +/* The kernel recognizes executables as valid only if they carry a
36 + "FreeBSD" label in the ELF header. So we put this label on all
37 + executables and (for simplicity) also all other object files. */
38 +
39 +static void elf_i386_post_process_headers
40 + PARAMS ((bfd *, struct bfd_link_info *));
41 +
42 +static void
43 +elf_i386_post_process_headers (abfd, link_info)
44 + bfd * abfd;
45 + struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
46 +{
47 + Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
48 +
49 + i_ehdrp = elf_elfheader (abfd);
50 +
51 + /* Put an ABI label supported by FreeBSD >= 4.1. */
52 + i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
53 +#ifdef OLD_FREEBSD_ABI_LABEL
54 + /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
55 + memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
56 +#endif
57 +}
58 +
59 +#define elf_backend_post_process_headers elf_i386_post_process_headers
60 +
61 +#include "elf32-i386.c"
62 Index: binutils-2.16.1/bfd/elf64-alpha-fbsd.c
63 ===================================================================
64 --- /dev/null
65 +++ binutils-2.16.1/bfd/elf64-alpha-fbsd.c
66 @@ -0,0 +1,56 @@
67 +/* Alpha specific support for 64-bit ELF on FreeBSD.
68 + Copyright 2002 Free Software Foundation, Inc.
69 +
70 +This file is part of BFD, the Binary File Descriptor library.
71 +
72 +This program is free software; you can redistribute it and/or modify
73 +it under the terms of the GNU General Public License as published by
74 +the Free Software Foundation; either version 2 of the License, or
75 +(at your option) any later version.
76 +
77 +This program is distributed in the hope that it will be useful,
78 +but WITHOUT ANY WARRANTY; without even the implied warranty of
79 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80 +GNU General Public License for more details.
81 +
82 +You should have received a copy of the GNU General Public License
83 +along with this program; if not, write to the Free Software
84 +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
85 +
86 +#define TARGET_LITTLE_SYM bfd_elf64_alpha_freebsd_vec
87 +#define TARGET_LITTLE_NAME "elf64-alpha-freebsd"
88 +#define ELF_ARCH bfd_arch_alpha
89 +#define ELF_MACHINE_CODE EM_ALPHA
90 +#define ELF_MAXPAGESIZE 0x10000
91 +
92 +#include "bfd.h"
93 +#include "sysdep.h"
94 +#include "elf-bfd.h"
95 +
96 +/* The kernel recognizes executables as valid only if they carry a
97 + "FreeBSD" label in the ELF header. So we put this label on all
98 + executables and (for simplicity) also all other object files. */
99 +
100 +static void elf_alpha_post_process_headers
101 + PARAMS ((bfd *, struct bfd_link_info *));
102 +
103 +static void
104 +elf_alpha_post_process_headers (abfd, link_info)
105 + bfd * abfd;
106 + struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
107 +{
108 + Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
109 +
110 + i_ehdrp = elf_elfheader (abfd);
111 +
112 + /* Put an ABI label supported by FreeBSD >= 4.1. */
113 + i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
114 +#ifdef OLD_FREEBSD_ABI_LABEL
115 + /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
116 + memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
117 +#endif
118 +}
119 +
120 +#define elf_backend_post_process_headers elf_alpha_post_process_headers
121 +
122 +#include "elf64-alpha.c"
123 Index: binutils-2.16.1/bfd/elflink.h
124 ===================================================================
125 --- /dev/null
126 +++ binutils-2.16.1/bfd/elflink.h
127 @@ -0,0 +1,8532 @@
128 +/* ELF linker support.
129 + Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
130 + Free Software Foundation, Inc.
131 +
132 +This file is part of BFD, the Binary File Descriptor library.
133 +
134 +This program is free software; you can redistribute it and/or modify
135 +it under the terms of the GNU General Public License as published by
136 +the Free Software Foundation; either version 2 of the License, or
137 +(at your option) any later version.
138 +
139 +This program is distributed in the hope that it will be useful,
140 +but WITHOUT ANY WARRANTY; without even the implied warranty of
141 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
142 +GNU General Public License for more details.
143 +
144 +You should have received a copy of the GNU General Public License
145 +along with this program; if not, write to the Free Software
146 +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
147 +
148 +/* ELF linker code. */
149 +
150 +/* This struct is used to pass information to routines called via
151 + elf_link_hash_traverse which must return failure. */
152 +
153 +struct elf_info_failed
154 +{
155 + boolean failed;
156 + struct bfd_link_info *info;
157 + struct bfd_elf_version_tree *verdefs;
158 +};
159 +
160 +static boolean is_global_data_symbol_definition
161 + PARAMS ((bfd *, Elf_Internal_Sym *));
162 +static boolean elf_link_is_defined_archive_symbol
163 + PARAMS ((bfd *, carsym *));
164 +static boolean elf_link_add_object_symbols
165 + PARAMS ((bfd *, struct bfd_link_info *));
166 +static boolean elf_link_add_archive_symbols
167 + PARAMS ((bfd *, struct bfd_link_info *));
168 +static boolean elf_merge_symbol
169 + PARAMS ((bfd *, struct bfd_link_info *, const char *,
170 + Elf_Internal_Sym *, asection **, bfd_vma *,
171 + struct elf_link_hash_entry **, boolean *, boolean *,
172 + boolean *, boolean));
173 +static boolean elf_add_default_symbol
174 + PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
175 + const char *, Elf_Internal_Sym *, asection **, bfd_vma *,
176 + boolean *, boolean, boolean));
177 +static boolean elf_export_symbol
178 + PARAMS ((struct elf_link_hash_entry *, PTR));
179 +static boolean elf_finalize_dynstr
180 + PARAMS ((bfd *, struct bfd_link_info *));
181 +static boolean elf_fix_symbol_flags
182 + PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
183 +static boolean elf_adjust_dynamic_symbol
184 + PARAMS ((struct elf_link_hash_entry *, PTR));
185 +static boolean elf_link_find_version_dependencies
186 + PARAMS ((struct elf_link_hash_entry *, PTR));
187 +static boolean elf_link_assign_sym_version
188 + PARAMS ((struct elf_link_hash_entry *, PTR));
189 +static boolean elf_collect_hash_codes
190 + PARAMS ((struct elf_link_hash_entry *, PTR));
191 +static boolean elf_link_read_relocs_from_section
192 + PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
193 +static size_t compute_bucket_count
194 + PARAMS ((struct bfd_link_info *));
195 +static boolean elf_link_output_relocs
196 + PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
197 +static boolean elf_link_size_reloc_section
198 + PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
199 +static void elf_link_adjust_relocs
200 + PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
201 + struct elf_link_hash_entry **));
202 +static int elf_link_sort_cmp1
203 + PARAMS ((const void *, const void *));
204 +static int elf_link_sort_cmp2
205 + PARAMS ((const void *, const void *));
206 +static size_t elf_link_sort_relocs
207 + PARAMS ((bfd *, struct bfd_link_info *, asection **));
208 +static boolean elf_section_ignore_discarded_relocs
209 + PARAMS ((asection *));
210 +
211 +/* Given an ELF BFD, add symbols to the global hash table as
212 + appropriate. */
213 +
214 +boolean
215 +elf_bfd_link_add_symbols (abfd, info)
216 + bfd *abfd;
217 + struct bfd_link_info *info;
218 +{
219 + switch (bfd_get_format (abfd))
220 + {
221 + case bfd_object:
222 + return elf_link_add_object_symbols (abfd, info);
223 + case bfd_archive:
224 + return elf_link_add_archive_symbols (abfd, info);
225 + default:
226 + bfd_set_error (bfd_error_wrong_format);
227 + return false;
228 + }
229 +}
230 +
231 +/* Return true iff this is a non-common, definition of a non-function symbol. */
232 +static boolean
233 +is_global_data_symbol_definition (abfd, sym)
234 + bfd * abfd ATTRIBUTE_UNUSED;
235 + Elf_Internal_Sym * sym;
236 +{
237 + /* Local symbols do not count, but target specific ones might. */
238 + if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
239 + && ELF_ST_BIND (sym->st_info) < STB_LOOS)
240 + return false;
241 +
242 + /* Function symbols do not count. */
243 + if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
244 + return false;
245 +
246 + /* If the section is undefined, then so is the symbol. */
247 + if (sym->st_shndx == SHN_UNDEF)
248 + return false;
249 +
250 + /* If the symbol is defined in the common section, then
251 + it is a common definition and so does not count. */
252 + if (sym->st_shndx == SHN_COMMON)
253 + return false;
254 +
255 + /* If the symbol is in a target specific section then we
256 + must rely upon the backend to tell us what it is. */
257 + if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
258 + /* FIXME - this function is not coded yet:
259 +
260 + return _bfd_is_global_symbol_definition (abfd, sym);
261 +
262 + Instead for now assume that the definition is not global,
263 + Even if this is wrong, at least the linker will behave
264 + in the same way that it used to do. */
265 + return false;
266 +
267 + return true;
268 +}
269 +
270 +/* Search the symbol table of the archive element of the archive ABFD
271 + whose archive map contains a mention of SYMDEF, and determine if
272 + the symbol is defined in this element. */
273 +static boolean
274 +elf_link_is_defined_archive_symbol (abfd, symdef)
275 + bfd * abfd;
276 + carsym * symdef;
277 +{
278 + Elf_Internal_Shdr * hdr;
279 + bfd_size_type symcount;
280 + bfd_size_type extsymcount;
281 + bfd_size_type extsymoff;
282 + Elf_Internal_Sym *isymbuf;
283 + Elf_Internal_Sym *isym;
284 + Elf_Internal_Sym *isymend;
285 + boolean result;
286 +
287 + abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
288 + if (abfd == (bfd *) NULL)
289 + return false;
290 +
291 + if (! bfd_check_format (abfd, bfd_object))
292 + return false;
293 +
294 + /* If we have already included the element containing this symbol in the
295 + link then we do not need to include it again. Just claim that any symbol
296 + it contains is not a definition, so that our caller will not decide to
297 + (re)include this element. */
298 + if (abfd->archive_pass)
299 + return false;
300 +
301 + /* Select the appropriate symbol table. */
302 + if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
303 + hdr = &elf_tdata (abfd)->symtab_hdr;
304 + else
305 + hdr = &elf_tdata (abfd)->dynsymtab_hdr;
306 +
307 + symcount = hdr->sh_size / sizeof (Elf_External_Sym);
308 +
309 + /* The sh_info field of the symtab header tells us where the
310 + external symbols start. We don't care about the local symbols. */
311 + if (elf_bad_symtab (abfd))
312 + {
313 + extsymcount = symcount;
314 + extsymoff = 0;
315 + }
316 + else
317 + {
318 + extsymcount = symcount - hdr->sh_info;
319 + extsymoff = hdr->sh_info;
320 + }
321 +
322 + if (extsymcount == 0)
323 + return false;
324 +
325 + /* Read in the symbol table. */
326 + isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
327 + NULL, NULL, NULL);
328 + if (isymbuf == NULL)
329 + return false;
330 +
331 + /* Scan the symbol table looking for SYMDEF. */
332 + result = false;
333 + for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
334 + {
335 + const char *name;
336 +
337 + name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
338 + isym->st_name);
339 + if (name == (const char *) NULL)
340 + break;
341 +
342 + if (strcmp (name, symdef->name) == 0)
343 + {
344 + result = is_global_data_symbol_definition (abfd, isym);
345 + break;
346 + }
347 + }
348 +
349 + free (isymbuf);
350 +
351 + return result;
352 +}
353 +
354 +/* Add symbols from an ELF archive file to the linker hash table. We
355 + don't use _bfd_generic_link_add_archive_symbols because of a
356 + problem which arises on UnixWare. The UnixWare libc.so is an
357 + archive which includes an entry libc.so.1 which defines a bunch of
358 + symbols. The libc.so archive also includes a number of other
359 + object files, which also define symbols, some of which are the same
360 + as those defined in libc.so.1. Correct linking requires that we
361 + consider each object file in turn, and include it if it defines any
362 + symbols we need. _bfd_generic_link_add_archive_symbols does not do
363 + this; it looks through the list of undefined symbols, and includes
364 + any object file which defines them. When this algorithm is used on
365 + UnixWare, it winds up pulling in libc.so.1 early and defining a
366 + bunch of symbols. This means that some of the other objects in the
367 + archive are not included in the link, which is incorrect since they
368 + precede libc.so.1 in the archive.
369 +
370 + Fortunately, ELF archive handling is simpler than that done by
371 + _bfd_generic_link_add_archive_symbols, which has to allow for a.out
372 + oddities. In ELF, if we find a symbol in the archive map, and the
373 + symbol is currently undefined, we know that we must pull in that
374 + object file.
375 +
376 + Unfortunately, we do have to make multiple passes over the symbol
377 + table until nothing further is resolved. */
378 +
379 +static boolean
380 +elf_link_add_archive_symbols (abfd, info)
381 + bfd *abfd;
382 + struct bfd_link_info *info;
383 +{
384 + symindex c;
385 + boolean *defined = NULL;
386 + boolean *included = NULL;
387 + carsym *symdefs;
388 + boolean loop;
389 + bfd_size_type amt;
390 +
391 + if (! bfd_has_map (abfd))
392 + {
393 + /* An empty archive is a special case. */
394 + if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
395 + return true;
396 + bfd_set_error (bfd_error_no_armap);
397 + return false;
398 + }
399 +
400 + /* Keep track of all symbols we know to be already defined, and all
401 + files we know to be already included. This is to speed up the
402 + second and subsequent passes. */
403 + c = bfd_ardata (abfd)->symdef_count;
404 + if (c == 0)
405 + return true;
406 + amt = c;
407 + amt *= sizeof (boolean);
408 + defined = (boolean *) bfd_zmalloc (amt);
409 + included = (boolean *) bfd_zmalloc (amt);
410 + if (defined == (boolean *) NULL || included == (boolean *) NULL)
411 + goto error_return;
412 +
413 + symdefs = bfd_ardata (abfd)->symdefs;
414 +
415 + do
416 + {
417 + file_ptr last;
418 + symindex i;
419 + carsym *symdef;
420 + carsym *symdefend;
421 +
422 + loop = false;
423 + last = -1;
424 +
425 + symdef = symdefs;
426 + symdefend = symdef + c;
427 + for (i = 0; symdef < symdefend; symdef++, i++)
428 + {
429 + struct elf_link_hash_entry *h;
430 + bfd *element;
431 + struct bfd_link_hash_entry *undefs_tail;
432 + symindex mark;
433 +
434 + if (defined[i] || included[i])
435 + continue;
436 + if (symdef->file_offset == last)
437 + {
438 + included[i] = true;
439 + continue;
440 + }
441 +
442 + h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
443 + false, false, false);
444 +
445 + if (h == NULL)
446 + {
447 + char *p, *copy;
448 + size_t len, first;
449 +
450 + /* If this is a default version (the name contains @@),
451 + look up the symbol again with only one `@' as well
452 + as without the version. The effect is that references
453 + to the symbol with and without the version will be
454 + matched by the default symbol in the archive. */
455 +
456 + p = strchr (symdef->name, ELF_VER_CHR);
457 + if (p == NULL || p[1] != ELF_VER_CHR)
458 + continue;
459 +
460 + /* First check with only one `@'. */
461 + len = strlen (symdef->name);
462 + copy = bfd_alloc (abfd, (bfd_size_type) len);
463 + if (copy == NULL)
464 + goto error_return;
465 + first = p - symdef->name + 1;
466 + memcpy (copy, symdef->name, first);
467 + memcpy (copy + first, symdef->name + first + 1, len - first);
468 +
469 + h = elf_link_hash_lookup (elf_hash_table (info), copy,
470 + false, false, false);
471 +
472 + if (h == NULL)
473 + {
474 + /* We also need to check references to the symbol
475 + without the version. */
476 +
477 + copy[first - 1] = '\0';
478 + h = elf_link_hash_lookup (elf_hash_table (info),
479 + copy, false, false, false);
480 + }
481 +
482 + bfd_release (abfd, copy);
483 + }
484 +
485 + if (h == NULL)
486 + continue;
487 +
488 + if (h->root.type == bfd_link_hash_common)
489 + {
490 + /* We currently have a common symbol. The archive map contains
491 + a reference to this symbol, so we may want to include it. We
492 + only want to include it however, if this archive element
493 + contains a definition of the symbol, not just another common
494 + declaration of it.
495 +
496 + Unfortunately some archivers (including GNU ar) will put
497 + declarations of common symbols into their archive maps, as
498 + well as real definitions, so we cannot just go by the archive
499 + map alone. Instead we must read in the element's symbol
500 + table and check that to see what kind of symbol definition
501 + this is. */
502 + if (! elf_link_is_defined_archive_symbol (abfd, symdef))
503 + continue;
504 + }
505 + else if (h->root.type != bfd_link_hash_undefined)
506 + {
507 + if (h->root.type != bfd_link_hash_undefweak)
508 + defined[i] = true;
509 + continue;
510 + }
511 +
512 + /* We need to include this archive member. */
513 + element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
514 + if (element == (bfd *) NULL)
515 + goto error_return;
516 +
517 + if (! bfd_check_format (element, bfd_object))
518 + goto error_return;
519 +
520 + /* Doublecheck that we have not included this object
521 + already--it should be impossible, but there may be
522 + something wrong with the archive. */
523 + if (element->archive_pass != 0)
524 + {
525 + bfd_set_error (bfd_error_bad_value);
526 + goto error_return;
527 + }
528 + element->archive_pass = 1;
529 +
530 + undefs_tail = info->hash->undefs_tail;
531 +
532 + if (! (*info->callbacks->add_archive_element) (info, element,
533 + symdef->name))
534 + goto error_return;
535 + if (! elf_link_add_object_symbols (element, info))
536 + goto error_return;
537 +
538 + /* If there are any new undefined symbols, we need to make
539 + another pass through the archive in order to see whether
540 + they can be defined. FIXME: This isn't perfect, because
541 + common symbols wind up on undefs_tail and because an
542 + undefined symbol which is defined later on in this pass
543 + does not require another pass. This isn't a bug, but it
544 + does make the code less efficient than it could be. */
545 + if (undefs_tail != info->hash->undefs_tail)
546 + loop = true;
547 +
548 + /* Look backward to mark all symbols from this object file
549 + which we have already seen in this pass. */
550 + mark = i;
551 + do
552 + {
553 + included[mark] = true;
554 + if (mark == 0)
555 + break;
556 + --mark;
557 + }
558 + while (symdefs[mark].file_offset == symdef->file_offset);
559 +
560 + /* We mark subsequent symbols from this object file as we go
561 + on through the loop. */
562 + last = symdef->file_offset;
563 + }
564 + }
565 + while (loop);
566 +
567 + free (defined);
568 + free (included);
569 +
570 + return true;
571 +
572 + error_return:
573 + if (defined != (boolean *) NULL)
574 + free (defined);
575 + if (included != (boolean *) NULL)
576 + free (included);
577 + return false;
578 +}
579 +
580 +/* This function is called when we want to define a new symbol. It
581 + handles the various cases which arise when we find a definition in
582 + a dynamic object, or when there is already a definition in a
583 + dynamic object. The new symbol is described by NAME, SYM, PSEC,
584 + and PVALUE. We set SYM_HASH to the hash table entry. We set
585 + OVERRIDE if the old symbol is overriding a new definition. We set
586 + TYPE_CHANGE_OK if it is OK for the type to change. We set
587 + SIZE_CHANGE_OK if it is OK for the size to change. By OK to
588 + change, we mean that we shouldn't warn if the type or size does
589 + change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
590 + a shared object. */
591 +
592 +static boolean
593 +elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
594 + override, type_change_ok, size_change_ok, dt_needed)
595 + bfd *abfd;
596 + struct bfd_link_info *info;
597 + const char *name;
598 + Elf_Internal_Sym *sym;
599 + asection **psec;
600 + bfd_vma *pvalue;
601 + struct elf_link_hash_entry **sym_hash;
602 + boolean *override;
603 + boolean *type_change_ok;
604 + boolean *size_change_ok;
605 + boolean dt_needed;
606 +{
607 + asection *sec;
608 + struct elf_link_hash_entry *h;
609 + int bind;
610 + bfd *oldbfd;
611 + boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
612 +
613 + *override = false;
614 +
615 + sec = *psec;
616 + bind = ELF_ST_BIND (sym->st_info);
617 +
618 + if (! bfd_is_und_section (sec))
619 + h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
620 + else
621 + h = ((struct elf_link_hash_entry *)
622 + bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
623 + if (h == NULL)
624 + return false;
625 + *sym_hash = h;
626 +
627 + /* This code is for coping with dynamic objects, and is only useful
628 + if we are doing an ELF link. */
629 + if (info->hash->creator != abfd->xvec)
630 + return true;
631 +
632 + /* For merging, we only care about real symbols. */
633 +
634 + while (h->root.type == bfd_link_hash_indirect
635 + || h->root.type == bfd_link_hash_warning)
636 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
637 +
638 + /* If we just created the symbol, mark it as being an ELF symbol.
639 + Other than that, there is nothing to do--there is no merge issue
640 + with a newly defined symbol--so we just return. */
641 +
642 + if (h->root.type == bfd_link_hash_new)
643 + {
644 + h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
645 + return true;
646 + }
647 +
648 + /* OLDBFD is a BFD associated with the existing symbol. */
649 +
650 + switch (h->root.type)
651 + {
652 + default:
653 + oldbfd = NULL;
654 + break;
655 +
656 + case bfd_link_hash_undefined:
657 + case bfd_link_hash_undefweak:
658 + oldbfd = h->root.u.undef.abfd;
659 + break;
660 +
661 + case bfd_link_hash_defined:
662 + case bfd_link_hash_defweak:
663 + oldbfd = h->root.u.def.section->owner;
664 + break;
665 +
666 + case bfd_link_hash_common:
667 + oldbfd = h->root.u.c.p->section->owner;
668 + break;
669 + }
670 +
671 + /* In cases involving weak versioned symbols, we may wind up trying
672 + to merge a symbol with itself. Catch that here, to avoid the
673 + confusion that results if we try to override a symbol with
674 + itself. The additional tests catch cases like
675 + _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
676 + dynamic object, which we do want to handle here. */
677 + if (abfd == oldbfd
678 + && ((abfd->flags & DYNAMIC) == 0
679 + || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
680 + return true;
681 +
682 + /* NEWDYN and OLDDYN indicate whether the new or old symbol,
683 + respectively, is from a dynamic object. */
684 +
685 + if ((abfd->flags & DYNAMIC) != 0)
686 + newdyn = true;
687 + else
688 + newdyn = false;
689 +
690 + if (oldbfd != NULL)
691 + olddyn = (oldbfd->flags & DYNAMIC) != 0;
692 + else
693 + {
694 + asection *hsec;
695 +
696 + /* This code handles the special SHN_MIPS_{TEXT,DATA} section
697 + indices used by MIPS ELF. */
698 + switch (h->root.type)
699 + {
700 + default:
701 + hsec = NULL;
702 + break;
703 +
704 + case bfd_link_hash_defined:
705 + case bfd_link_hash_defweak:
706 + hsec = h->root.u.def.section;
707 + break;
708 +
709 + case bfd_link_hash_common:
710 + hsec = h->root.u.c.p->section;
711 + break;
712 + }
713 +
714 + if (hsec == NULL)
715 + olddyn = false;
716 + else
717 + olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
718 + }
719 +
720 + /* NEWDEF and OLDDEF indicate whether the new or old symbol,
721 + respectively, appear to be a definition rather than reference. */
722 +
723 + if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
724 + newdef = false;
725 + else
726 + newdef = true;
727 +
728 + if (h->root.type == bfd_link_hash_undefined
729 + || h->root.type == bfd_link_hash_undefweak
730 + || h->root.type == bfd_link_hash_common)
731 + olddef = false;
732 + else
733 + olddef = true;
734 +
735 + /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
736 + symbol, respectively, appears to be a common symbol in a dynamic
737 + object. If a symbol appears in an uninitialized section, and is
738 + not weak, and is not a function, then it may be a common symbol
739 + which was resolved when the dynamic object was created. We want
740 + to treat such symbols specially, because they raise special
741 + considerations when setting the symbol size: if the symbol
742 + appears as a common symbol in a regular object, and the size in
743 + the regular object is larger, we must make sure that we use the
744 + larger size. This problematic case can always be avoided in C,
745 + but it must be handled correctly when using Fortran shared
746 + libraries.
747 +
748 + Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
749 + likewise for OLDDYNCOMMON and OLDDEF.
750 +
751 + Note that this test is just a heuristic, and that it is quite
752 + possible to have an uninitialized symbol in a shared object which
753 + is really a definition, rather than a common symbol. This could
754 + lead to some minor confusion when the symbol really is a common
755 + symbol in some regular object. However, I think it will be
756 + harmless. */
757 +
758 + if (newdyn
759 + && newdef
760 + && (sec->flags & SEC_ALLOC) != 0
761 + && (sec->flags & SEC_LOAD) == 0
762 + && sym->st_size > 0
763 + && bind != STB_WEAK
764 + && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
765 + newdyncommon = true;
766 + else
767 + newdyncommon = false;
768 +
769 + if (olddyn
770 + && olddef
771 + && h->root.type == bfd_link_hash_defined
772 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
773 + && (h->root.u.def.section->flags & SEC_ALLOC) != 0
774 + && (h->root.u.def.section->flags & SEC_LOAD) == 0
775 + && h->size > 0
776 + && h->type != STT_FUNC)
777 + olddyncommon = true;
778 + else
779 + olddyncommon = false;
780 +
781 + /* It's OK to change the type if either the existing symbol or the
782 + new symbol is weak unless it comes from a DT_NEEDED entry of
783 + a shared object, in which case, the DT_NEEDED entry may not be
784 + required at the run time. */
785 +
786 + if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
787 + || h->root.type == bfd_link_hash_undefweak
788 + || bind == STB_WEAK)
789 + *type_change_ok = true;
790 +
791 + /* It's OK to change the size if either the existing symbol or the
792 + new symbol is weak, or if the old symbol is undefined. */
793 +
794 + if (*type_change_ok
795 + || h->root.type == bfd_link_hash_undefined)
796 + *size_change_ok = true;
797 +
798 + /* If both the old and the new symbols look like common symbols in a
799 + dynamic object, set the size of the symbol to the larger of the
800 + two. */
801 +
802 + if (olddyncommon
803 + && newdyncommon
804 + && sym->st_size != h->size)
805 + {
806 + /* Since we think we have two common symbols, issue a multiple
807 + common warning if desired. Note that we only warn if the
808 + size is different. If the size is the same, we simply let
809 + the old symbol override the new one as normally happens with
810 + symbols defined in dynamic objects. */
811 +
812 + if (! ((*info->callbacks->multiple_common)
813 + (info, h->root.root.string, oldbfd, bfd_link_hash_common,
814 + h->size, abfd, bfd_link_hash_common, sym->st_size)))
815 + return false;
816 +
817 + if (sym->st_size > h->size)
818 + h->size = sym->st_size;
819 +
820 + *size_change_ok = true;
821 + }
822 +
823 + /* If we are looking at a dynamic object, and we have found a
824 + definition, we need to see if the symbol was already defined by
825 + some other object. If so, we want to use the existing
826 + definition, and we do not want to report a multiple symbol
827 + definition error; we do this by clobbering *PSEC to be
828 + bfd_und_section_ptr.
829 +
830 + We treat a common symbol as a definition if the symbol in the
831 + shared library is a function, since common symbols always
832 + represent variables; this can cause confusion in principle, but
833 + any such confusion would seem to indicate an erroneous program or
834 + shared library. We also permit a common symbol in a regular
835 + object to override a weak symbol in a shared object.
836 +
837 + We prefer a non-weak definition in a shared library to a weak
838 + definition in the executable unless it comes from a DT_NEEDED
839 + entry of a shared object, in which case, the DT_NEEDED entry
840 + may not be required at the run time. */
841 +
842 + if (newdyn
843 + && newdef
844 + && (olddef
845 + || (h->root.type == bfd_link_hash_common
846 + && (bind == STB_WEAK
847 + || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
848 + && (h->root.type != bfd_link_hash_defweak
849 + || dt_needed
850 + || bind == STB_WEAK))
851 + {
852 + *override = true;
853 + newdef = false;
854 + newdyncommon = false;
855 +
856 + *psec = sec = bfd_und_section_ptr;
857 + *size_change_ok = true;
858 +
859 + /* If we get here when the old symbol is a common symbol, then
860 + we are explicitly letting it override a weak symbol or
861 + function in a dynamic object, and we don't want to warn about
862 + a type change. If the old symbol is a defined symbol, a type
863 + change warning may still be appropriate. */
864 +
865 + if (h->root.type == bfd_link_hash_common)
866 + *type_change_ok = true;
867 + }
868 +
869 + /* Handle the special case of an old common symbol merging with a
870 + new symbol which looks like a common symbol in a shared object.
871 + We change *PSEC and *PVALUE to make the new symbol look like a
872 + common symbol, and let _bfd_generic_link_add_one_symbol will do
873 + the right thing. */
874 +
875 + if (newdyncommon
876 + && h->root.type == bfd_link_hash_common)
877 + {
878 + *override = true;
879 + newdef = false;
880 + newdyncommon = false;
881 + *pvalue = sym->st_size;
882 + *psec = sec = bfd_com_section_ptr;
883 + *size_change_ok = true;
884 + }
885 +
886 + /* If the old symbol is from a dynamic object, and the new symbol is
887 + a definition which is not from a dynamic object, then the new
888 + symbol overrides the old symbol. Symbols from regular files
889 + always take precedence over symbols from dynamic objects, even if
890 + they are defined after the dynamic object in the link.
891 +
892 + As above, we again permit a common symbol in a regular object to
893 + override a definition in a shared object if the shared object
894 + symbol is a function or is weak.
895 +
896 + As above, we permit a non-weak definition in a shared object to
897 + override a weak definition in a regular object. */
898 +
899 + if (! newdyn
900 + && (newdef
901 + || (bfd_is_com_section (sec)
902 + && (h->root.type == bfd_link_hash_defweak
903 + || h->type == STT_FUNC)))
904 + && olddyn
905 + && olddef
906 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
907 + && (bind != STB_WEAK
908 + || h->root.type == bfd_link_hash_defweak))
909 + {
910 + /* Change the hash table entry to undefined, and let
911 + _bfd_generic_link_add_one_symbol do the right thing with the
912 + new definition. */
913 +
914 + h->root.type = bfd_link_hash_undefined;
915 + h->root.u.undef.abfd = h->root.u.def.section->owner;
916 + *size_change_ok = true;
917 +
918 + olddef = false;
919 + olddyncommon = false;
920 +
921 + /* We again permit a type change when a common symbol may be
922 + overriding a function. */
923 +
924 + if (bfd_is_com_section (sec))
925 + *type_change_ok = true;
926 +
927 + /* This union may have been set to be non-NULL when this symbol
928 + was seen in a dynamic object. We must force the union to be
929 + NULL, so that it is correct for a regular symbol. */
930 +
931 + h->verinfo.vertree = NULL;
932 +
933 + /* In this special case, if H is the target of an indirection,
934 + we want the caller to frob with H rather than with the
935 + indirect symbol. That will permit the caller to redefine the
936 + target of the indirection, rather than the indirect symbol
937 + itself. FIXME: This will break the -y option if we store a
938 + symbol with a different name. */
939 + *sym_hash = h;
940 + }
941 +
942 + /* Handle the special case of a new common symbol merging with an
943 + old symbol that looks like it might be a common symbol defined in
944 + a shared object. Note that we have already handled the case in
945 + which a new common symbol should simply override the definition
946 + in the shared library. */
947 +
948 + if (! newdyn
949 + && bfd_is_com_section (sec)
950 + && olddyncommon)
951 + {
952 + /* It would be best if we could set the hash table entry to a
953 + common symbol, but we don't know what to use for the section
954 + or the alignment. */
955 + if (! ((*info->callbacks->multiple_common)
956 + (info, h->root.root.string, oldbfd, bfd_link_hash_common,
957 + h->size, abfd, bfd_link_hash_common, sym->st_size)))
958 + return false;
959 +
960 + /* If the predumed common symbol in the dynamic object is
961 + larger, pretend that the new symbol has its size. */
962 +
963 + if (h->size > *pvalue)
964 + *pvalue = h->size;
965 +
966 + /* FIXME: We no longer know the alignment required by the symbol
967 + in the dynamic object, so we just wind up using the one from
968 + the regular object. */
969 +
970 + olddef = false;
971 + olddyncommon = false;
972 +
973 + h->root.type = bfd_link_hash_undefined;
974 + h->root.u.undef.abfd = h->root.u.def.section->owner;
975 +
976 + *size_change_ok = true;
977 + *type_change_ok = true;
978 +
979 + h->verinfo.vertree = NULL;
980 + }
981 +
982 + /* Handle the special case of a weak definition in a regular object
983 + followed by a non-weak definition in a shared object. In this
984 + case, we prefer the definition in the shared object unless it
985 + comes from a DT_NEEDED entry of a shared object, in which case,
986 + the DT_NEEDED entry may not be required at the run time. */
987 + if (olddef
988 + && ! dt_needed
989 + && h->root.type == bfd_link_hash_defweak
990 + && newdef
991 + && newdyn
992 + && bind != STB_WEAK)
993 + {
994 + /* To make this work we have to frob the flags so that the rest
995 + of the code does not think we are using the regular
996 + definition. */
997 + if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
998 + h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
999 + else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
1000 + h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1001 + h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
1002 + | ELF_LINK_HASH_DEF_DYNAMIC);
1003 +
1004 + /* If H is the target of an indirection, we want the caller to
1005 + use H rather than the indirect symbol. Otherwise if we are
1006 + defining a new indirect symbol we will wind up attaching it
1007 + to the entry we are overriding. */
1008 + *sym_hash = h;
1009 + }
1010 +
1011 + /* Handle the special case of a non-weak definition in a shared
1012 + object followed by a weak definition in a regular object. In
1013 + this case we prefer to definition in the shared object. To make
1014 + this work we have to tell the caller to not treat the new symbol
1015 + as a definition. */
1016 + if (olddef
1017 + && olddyn
1018 + && h->root.type != bfd_link_hash_defweak
1019 + && newdef
1020 + && ! newdyn
1021 + && bind == STB_WEAK)
1022 + *override = true;
1023 +
1024 + return true;
1025 +}
1026 +
1027 +/* This function is called to create an indirect symbol from the
1028 + default for the symbol with the default version if needed. The
1029 + symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1030 + set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
1031 + indicates if it comes from a DT_NEEDED entry of a shared object. */
1032 +
1033 +static boolean
1034 +elf_add_default_symbol (abfd, info, h, name, sym, psec, value,
1035 + dynsym, override, dt_needed)
1036 + bfd *abfd;
1037 + struct bfd_link_info *info;
1038 + struct elf_link_hash_entry *h;
1039 + const char *name;
1040 + Elf_Internal_Sym *sym;
1041 + asection **psec;
1042 + bfd_vma *value;
1043 + boolean *dynsym;
1044 + boolean override;
1045 + boolean dt_needed;
1046 +{
1047 + boolean type_change_ok;
1048 + boolean size_change_ok;
1049 + char *shortname;
1050 + struct elf_link_hash_entry *hi;
1051 + struct bfd_link_hash_entry *bh;
1052 + struct elf_backend_data *bed;
1053 + boolean collect;
1054 + boolean dynamic;
1055 + char *p;
1056 + size_t len, shortlen;
1057 + asection *sec;
1058 +
1059 + /* If this symbol has a version, and it is the default version, we
1060 + create an indirect symbol from the default name to the fully
1061 + decorated name. This will cause external references which do not
1062 + specify a version to be bound to this version of the symbol. */
1063 + p = strchr (name, ELF_VER_CHR);
1064 + if (p == NULL || p[1] != ELF_VER_CHR)
1065 + return true;
1066 +
1067 + if (override)
1068 + {
1069 + /* We are overridden by an old defition. We need to check if we
1070 + need to create the indirect symbol from the default name. */
1071 + hi = elf_link_hash_lookup (elf_hash_table (info), name, true,
1072 + false, false);
1073 + BFD_ASSERT (hi != NULL);
1074 + if (hi == h)
1075 + return true;
1076 + while (hi->root.type == bfd_link_hash_indirect
1077 + || hi->root.type == bfd_link_hash_warning)
1078 + {
1079 + hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1080 + if (hi == h)
1081 + return true;
1082 + }
1083 + }
1084 +
1085 + bed = get_elf_backend_data (abfd);
1086 + collect = bed->collect;
1087 + dynamic = (abfd->flags & DYNAMIC) != 0;
1088 +
1089 + shortlen = p - name;
1090 + shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1091 + if (shortname == NULL)
1092 + return false;
1093 + memcpy (shortname, name, shortlen);
1094 + shortname[shortlen] = '\0';
1095 +
1096 + /* We are going to create a new symbol. Merge it with any existing
1097 + symbol with this name. For the purposes of the merge, act as
1098 + though we were defining the symbol we just defined, although we
1099 + actually going to define an indirect symbol. */
1100 + type_change_ok = false;
1101 + size_change_ok = false;
1102 + sec = *psec;
1103 + if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1104 + &hi, &override, &type_change_ok,
1105 + &size_change_ok, dt_needed))
1106 + return false;
1107 +
1108 + if (! override)
1109 + {
1110 + bh = &hi->root;
1111 + if (! (_bfd_generic_link_add_one_symbol
1112 + (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1113 + (bfd_vma) 0, name, false, collect, &bh)))
1114 + return false;
1115 + hi = (struct elf_link_hash_entry *) bh;
1116 + }
1117 + else
1118 + {
1119 + /* In this case the symbol named SHORTNAME is overriding the
1120 + indirect symbol we want to add. We were planning on making
1121 + SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1122 + is the name without a version. NAME is the fully versioned
1123 + name, and it is the default version.
1124 +
1125 + Overriding means that we already saw a definition for the
1126 + symbol SHORTNAME in a regular object, and it is overriding
1127 + the symbol defined in the dynamic object.
1128 +
1129 + When this happens, we actually want to change NAME, the
1130 + symbol we just added, to refer to SHORTNAME. This will cause
1131 + references to NAME in the shared object to become references
1132 + to SHORTNAME in the regular object. This is what we expect
1133 + when we override a function in a shared object: that the
1134 + references in the shared object will be mapped to the
1135 + definition in the regular object. */
1136 +
1137 + while (hi->root.type == bfd_link_hash_indirect
1138 + || hi->root.type == bfd_link_hash_warning)
1139 + hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1140 +
1141 + h->root.type = bfd_link_hash_indirect;
1142 + h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1143 + if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1144 + {
1145 + h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1146 + hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1147 + if (hi->elf_link_hash_flags
1148 + & (ELF_LINK_HASH_REF_REGULAR
1149 + | ELF_LINK_HASH_DEF_REGULAR))
1150 + {
1151 + if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1152 + return false;
1153 + }
1154 + }
1155 +
1156 + /* Now set HI to H, so that the following code will set the
1157 + other fields correctly. */
1158 + hi = h;
1159 + }
1160 +
1161 + /* If there is a duplicate definition somewhere, then HI may not
1162 + point to an indirect symbol. We will have reported an error to
1163 + the user in that case. */
1164 +
1165 + if (hi->root.type == bfd_link_hash_indirect)
1166 + {
1167 + struct elf_link_hash_entry *ht;
1168 +
1169 + /* If the symbol became indirect, then we assume that we have
1170 + not seen a definition before. */
1171 + BFD_ASSERT ((hi->elf_link_hash_flags
1172 + & (ELF_LINK_HASH_DEF_DYNAMIC
1173 + | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1174 +
1175 + ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1176 + (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1177 +
1178 + /* See if the new flags lead us to realize that the symbol must
1179 + be dynamic. */
1180 + if (! *dynsym)
1181 + {
1182 + if (! dynamic)
1183 + {
1184 + if (info->shared
1185 + || ((hi->elf_link_hash_flags
1186 + & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1187 + *dynsym = true;
1188 + }
1189 + else
1190 + {
1191 + if ((hi->elf_link_hash_flags
1192 + & ELF_LINK_HASH_REF_REGULAR) != 0)
1193 + *dynsym = true;
1194 + }
1195 + }
1196 + }
1197 +
1198 + /* We also need to define an indirection from the nondefault version
1199 + of the symbol. */
1200 +
1201 + len = strlen (name);
1202 + shortname = bfd_hash_allocate (&info->hash->table, len);
1203 + if (shortname == NULL)
1204 + return false;
1205 + memcpy (shortname, name, shortlen);
1206 + memcpy (shortname + shortlen, p + 1, len - shortlen);
1207 +
1208 + /* Once again, merge with any existing symbol. */
1209 + type_change_ok = false;
1210 + size_change_ok = false;
1211 + sec = *psec;
1212 + if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1213 + &hi, &override, &type_change_ok,
1214 + &size_change_ok, dt_needed))
1215 + return false;
1216 +
1217 + if (override)
1218 + {
1219 + /* Here SHORTNAME is a versioned name, so we don't expect to see
1220 + the type of override we do in the case above unless it is
1221 + overridden by a versioned definiton. */
1222 + if (hi->root.type != bfd_link_hash_defined
1223 + && hi->root.type != bfd_link_hash_defweak)
1224 + (*_bfd_error_handler)
1225 + (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1226 + bfd_archive_filename (abfd), shortname);
1227 + }
1228 + else
1229 + {
1230 + bh = &hi->root;
1231 + if (! (_bfd_generic_link_add_one_symbol
1232 + (info, abfd, shortname, BSF_INDIRECT,
1233 + bfd_ind_section_ptr, (bfd_vma) 0, name, false, collect, &bh)))
1234 + return false;
1235 + hi = (struct elf_link_hash_entry *) bh;
1236 +
1237 + /* If there is a duplicate definition somewhere, then HI may not
1238 + point to an indirect symbol. We will have reported an error
1239 + to the user in that case. */
1240 +
1241 + if (hi->root.type == bfd_link_hash_indirect)
1242 + {
1243 + /* If the symbol became indirect, then we assume that we have
1244 + not seen a definition before. */
1245 + BFD_ASSERT ((hi->elf_link_hash_flags
1246 + & (ELF_LINK_HASH_DEF_DYNAMIC
1247 + | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1248 +
1249 + (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1250 +
1251 + /* See if the new flags lead us to realize that the symbol
1252 + must be dynamic. */
1253 + if (! *dynsym)
1254 + {
1255 + if (! dynamic)
1256 + {
1257 + if (info->shared
1258 + || ((hi->elf_link_hash_flags
1259 + & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1260 + *dynsym = true;
1261 + }
1262 + else
1263 + {
1264 + if ((hi->elf_link_hash_flags
1265 + & ELF_LINK_HASH_REF_REGULAR) != 0)
1266 + *dynsym = true;
1267 + }
1268 + }
1269 + }
1270 + }
1271 +
1272 + return true;
1273 +}
1274 +
1275 +/* Add symbols from an ELF object file to the linker hash table. */
1276 +
1277 +static boolean
1278 +elf_link_add_object_symbols (abfd, info)
1279 + bfd *abfd;
1280 + struct bfd_link_info *info;
1281 +{
1282 + boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
1283 + const Elf_Internal_Sym *,
1284 + const char **, flagword *,
1285 + asection **, bfd_vma *));
1286 + boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
1287 + asection *, const Elf_Internal_Rela *));
1288 + boolean collect;
1289 + Elf_Internal_Shdr *hdr;
1290 + bfd_size_type symcount;
1291 + bfd_size_type extsymcount;
1292 + bfd_size_type extsymoff;
1293 + struct elf_link_hash_entry **sym_hash;
1294 + boolean dynamic;
1295 + Elf_External_Versym *extversym = NULL;
1296 + Elf_External_Versym *ever;
1297 + struct elf_link_hash_entry *weaks;
1298 + Elf_Internal_Sym *isymbuf = NULL;
1299 + Elf_Internal_Sym *isym;
1300 + Elf_Internal_Sym *isymend;
1301 + struct elf_backend_data *bed;
1302 + boolean dt_needed;
1303 + struct elf_link_hash_table * hash_table;
1304 + bfd_size_type amt;
1305 +
1306 + hash_table = elf_hash_table (info);
1307 +
1308 + bed = get_elf_backend_data (abfd);
1309 + add_symbol_hook = bed->elf_add_symbol_hook;
1310 + collect = bed->collect;
1311 +
1312 + if ((abfd->flags & DYNAMIC) == 0)
1313 + dynamic = false;
1314 + else
1315 + {
1316 + dynamic = true;
1317 +
1318 + /* You can't use -r against a dynamic object. Also, there's no
1319 + hope of using a dynamic object which does not exactly match
1320 + the format of the output file. */
1321 + if (info->relocateable || info->hash->creator != abfd->xvec)
1322 + {
1323 + bfd_set_error (bfd_error_invalid_operation);
1324 + goto error_return;
1325 + }
1326 + }
1327 +
1328 + /* As a GNU extension, any input sections which are named
1329 + .gnu.warning.SYMBOL are treated as warning symbols for the given
1330 + symbol. This differs from .gnu.warning sections, which generate
1331 + warnings when they are included in an output file. */
1332 + if (! info->shared)
1333 + {
1334 + asection *s;
1335 +
1336 + for (s = abfd->sections; s != NULL; s = s->next)
1337 + {
1338 + const char *name;
1339 +
1340 + name = bfd_get_section_name (abfd, s);
1341 + if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1342 + {
1343 + char *msg;
1344 + bfd_size_type sz;
1345 +
1346 + name += sizeof ".gnu.warning." - 1;
1347 +
1348 + /* If this is a shared object, then look up the symbol
1349 + in the hash table. If it is there, and it is already
1350 + been defined, then we will not be using the entry
1351 + from this shared object, so we don't need to warn.
1352 + FIXME: If we see the definition in a regular object
1353 + later on, we will warn, but we shouldn't. The only
1354 + fix is to keep track of what warnings we are supposed
1355 + to emit, and then handle them all at the end of the
1356 + link. */
1357 + if (dynamic && abfd->xvec == info->hash->creator)
1358 + {
1359 + struct elf_link_hash_entry *h;
1360 +
1361 + h = elf_link_hash_lookup (hash_table, name,
1362 + false, false, true);
1363 +
1364 + /* FIXME: What about bfd_link_hash_common? */
1365 + if (h != NULL
1366 + && (h->root.type == bfd_link_hash_defined
1367 + || h->root.type == bfd_link_hash_defweak))
1368 + {
1369 + /* We don't want to issue this warning. Clobber
1370 + the section size so that the warning does not
1371 + get copied into the output file. */
1372 + s->_raw_size = 0;
1373 + continue;
1374 + }
1375 + }
1376 +
1377 + sz = bfd_section_size (abfd, s);
1378 + msg = (char *) bfd_alloc (abfd, sz + 1);
1379 + if (msg == NULL)
1380 + goto error_return;
1381 +
1382 + if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1383 + goto error_return;
1384 +
1385 + msg[sz] = '\0';
1386 +
1387 + if (! (_bfd_generic_link_add_one_symbol
1388 + (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1389 + false, collect, (struct bfd_link_hash_entry **) NULL)))
1390 + goto error_return;
1391 +
1392 + if (! info->relocateable)
1393 + {
1394 + /* Clobber the section size so that the warning does
1395 + not get copied into the output file. */
1396 + s->_raw_size = 0;
1397 + }
1398 + }
1399 + }
1400 + }
1401 +
1402 + dt_needed = false;
1403 + if (! dynamic)
1404 + {
1405 + /* If we are creating a shared library, create all the dynamic
1406 + sections immediately. We need to attach them to something,
1407 + so we attach them to this BFD, provided it is the right
1408 + format. FIXME: If there are no input BFD's of the same
1409 + format as the output, we can't make a shared library. */
1410 + if (info->shared
1411 + && is_elf_hash_table (info)
1412 + && ! hash_table->dynamic_sections_created
1413 + && abfd->xvec == info->hash->creator)
1414 + {
1415 + if (! elf_link_create_dynamic_sections (abfd, info))
1416 + goto error_return;
1417 + }
1418 + }
1419 + else if (! is_elf_hash_table (info))
1420 + goto error_return;
1421 + else
1422 + {
1423 + asection *s;
1424 + boolean add_needed;
1425 + const char *name;
1426 + bfd_size_type oldsize;
1427 + bfd_size_type strindex;
1428 + struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
1429 +
1430 + /* ld --just-symbols and dynamic objects don't mix very well.
1431 + Test for --just-symbols by looking at info set up by
1432 + _bfd_elf_link_just_syms. */
1433 + if ((s = abfd->sections) != NULL
1434 + && elf_section_data (s)->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
1435 + goto error_return;
1436 +
1437 + /* Find the name to use in a DT_NEEDED entry that refers to this
1438 + object. If the object has a DT_SONAME entry, we use it.
1439 + Otherwise, if the generic linker stuck something in
1440 + elf_dt_name, we use that. Otherwise, we just use the file
1441 + name. If the generic linker put a null string into
1442 + elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1443 + there is a DT_SONAME entry. */
1444 + add_needed = true;
1445 + name = bfd_get_filename (abfd);
1446 + if (elf_dt_name (abfd) != NULL)
1447 + {
1448 + name = elf_dt_name (abfd);
1449 + if (*name == '\0')
1450 + {
1451 + if (elf_dt_soname (abfd) != NULL)
1452 + dt_needed = true;
1453 +
1454 + add_needed = false;
1455 + }
1456 + }
1457 + s = bfd_get_section_by_name (abfd, ".dynamic");
1458 + if (s != NULL)
1459 + {
1460 + Elf_External_Dyn *dynbuf = NULL;
1461 + Elf_External_Dyn *extdyn;
1462 + Elf_External_Dyn *extdynend;
1463 + int elfsec;
1464 + unsigned long shlink;
1465 +
1466 + dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
1467 + if (dynbuf == NULL)
1468 + goto error_return;
1469 +
1470 + if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1471 + (file_ptr) 0, s->_raw_size))
1472 + goto error_free_dyn;
1473 +
1474 + elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1475 + if (elfsec == -1)
1476 + goto error_free_dyn;
1477 + shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1478 +
1479 + extdyn = dynbuf;
1480 + extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1481 + for (; extdyn < extdynend; extdyn++)
1482 + {
1483 + Elf_Internal_Dyn dyn;
1484 +
1485 + elf_swap_dyn_in (abfd, extdyn, &dyn);
1486 + if (dyn.d_tag == DT_SONAME)
1487 + {
1488 + unsigned int tagv = dyn.d_un.d_val;
1489 + name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1490 + if (name == NULL)
1491 + goto error_free_dyn;
1492 + }
1493 + if (dyn.d_tag == DT_NEEDED)
1494 + {
1495 + struct bfd_link_needed_list *n, **pn;
1496 + char *fnm, *anm;
1497 + unsigned int tagv = dyn.d_un.d_val;
1498 +
1499 + amt = sizeof (struct bfd_link_needed_list);
1500 + n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1501 + fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1502 + if (n == NULL || fnm == NULL)
1503 + goto error_free_dyn;
1504 + amt = strlen (fnm) + 1;
1505 + anm = bfd_alloc (abfd, amt);
1506 + if (anm == NULL)
1507 + goto error_free_dyn;
1508 + memcpy (anm, fnm, (size_t) amt);
1509 + n->name = anm;
1510 + n->by = abfd;
1511 + n->next = NULL;
1512 + for (pn = & hash_table->needed;
1513 + *pn != NULL;
1514 + pn = &(*pn)->next)
1515 + ;
1516 + *pn = n;
1517 + }
1518 + if (dyn.d_tag == DT_RUNPATH)
1519 + {
1520 + struct bfd_link_needed_list *n, **pn;
1521 + char *fnm, *anm;
1522 + unsigned int tagv = dyn.d_un.d_val;
1523 +
1524 + amt = sizeof (struct bfd_link_needed_list);
1525 + n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1526 + fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1527 + if (n == NULL || fnm == NULL)
1528 + goto error_free_dyn;
1529 + amt = strlen (fnm) + 1;
1530 + anm = bfd_alloc (abfd, amt);
1531 + if (anm == NULL)
1532 + goto error_free_dyn;
1533 + memcpy (anm, fnm, (size_t) amt);
1534 + n->name = anm;
1535 + n->by = abfd;
1536 + n->next = NULL;
1537 + for (pn = & runpath;
1538 + *pn != NULL;
1539 + pn = &(*pn)->next)
1540 + ;
1541 + *pn = n;
1542 + }
1543 + /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1544 + if (!runpath && dyn.d_tag == DT_RPATH)
1545 + {
1546 + struct bfd_link_needed_list *n, **pn;
1547 + char *fnm, *anm;
1548 + unsigned int tagv = dyn.d_un.d_val;
1549 +
1550 + amt = sizeof (struct bfd_link_needed_list);
1551 + n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1552 + fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1553 + if (n == NULL || fnm == NULL)
1554 + goto error_free_dyn;
1555 + amt = strlen (fnm) + 1;
1556 + anm = bfd_alloc (abfd, amt);
1557 + if (anm == NULL)
1558 + {
1559 + error_free_dyn:
1560 + free (dynbuf);
1561 + goto error_return;
1562 + }
1563 + memcpy (anm, fnm, (size_t) amt);
1564 + n->name = anm;
1565 + n->by = abfd;
1566 + n->next = NULL;
1567 + for (pn = & rpath;
1568 + *pn != NULL;
1569 + pn = &(*pn)->next)
1570 + ;
1571 + *pn = n;
1572 + }
1573 + }
1574 +
1575 + free (dynbuf);
1576 + }
1577 +
1578 + /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
1579 + frees all more recently bfd_alloc'd blocks as well. */
1580 + if (runpath)
1581 + rpath = runpath;
1582 +
1583 + if (rpath)
1584 + {
1585 + struct bfd_link_needed_list **pn;
1586 + for (pn = & hash_table->runpath;
1587 + *pn != NULL;
1588 + pn = &(*pn)->next)
1589 + ;
1590 + *pn = rpath;
1591 + }
1592 +
1593 + /* We do not want to include any of the sections in a dynamic
1594 + object in the output file. We hack by simply clobbering the
1595 + list of sections in the BFD. This could be handled more
1596 + cleanly by, say, a new section flag; the existing
1597 + SEC_NEVER_LOAD flag is not the one we want, because that one
1598 + still implies that the section takes up space in the output
1599 + file. */
1600 + bfd_section_list_clear (abfd);
1601 +
1602 + /* If this is the first dynamic object found in the link, create
1603 + the special sections required for dynamic linking. */
1604 + if (! hash_table->dynamic_sections_created)
1605 + if (! elf_link_create_dynamic_sections (abfd, info))
1606 + goto error_return;
1607 +
1608 + if (add_needed)
1609 + {
1610 + /* Add a DT_NEEDED entry for this dynamic object. */
1611 + oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1612 + strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
1613 + if (strindex == (bfd_size_type) -1)
1614 + goto error_return;
1615 +
1616 + if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1617 + {
1618 + asection *sdyn;
1619 + Elf_External_Dyn *dyncon, *dynconend;
1620 +
1621 + /* The hash table size did not change, which means that
1622 + the dynamic object name was already entered. If we
1623 + have already included this dynamic object in the
1624 + link, just ignore it. There is no reason to include
1625 + a particular dynamic object more than once. */
1626 + sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
1627 + BFD_ASSERT (sdyn != NULL);
1628 +
1629 + dyncon = (Elf_External_Dyn *) sdyn->contents;
1630 + dynconend = (Elf_External_Dyn *) (sdyn->contents +
1631 + sdyn->_raw_size);
1632 + for (; dyncon < dynconend; dyncon++)
1633 + {
1634 + Elf_Internal_Dyn dyn;
1635 +
1636 + elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
1637 + if (dyn.d_tag == DT_NEEDED
1638 + && dyn.d_un.d_val == strindex)
1639 + {
1640 + _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
1641 + return true;
1642 + }
1643 + }
1644 + }
1645 +
1646 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1647 + goto error_return;
1648 + }
1649 +
1650 + /* Save the SONAME, if there is one, because sometimes the
1651 + linker emulation code will need to know it. */
1652 + if (*name == '\0')
1653 + name = basename (bfd_get_filename (abfd));
1654 + elf_dt_name (abfd) = name;
1655 + }
1656 +
1657 + /* If this is a dynamic object, we always link against the .dynsym
1658 + symbol table, not the .symtab symbol table. The dynamic linker
1659 + will only see the .dynsym symbol table, so there is no reason to
1660 + look at .symtab for a dynamic object. */
1661 +
1662 + if (! dynamic || elf_dynsymtab (abfd) == 0)
1663 + hdr = &elf_tdata (abfd)->symtab_hdr;
1664 + else
1665 + hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1666 +
1667 + symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1668 +
1669 + /* The sh_info field of the symtab header tells us where the
1670 + external symbols start. We don't care about the local symbols at
1671 + this point. */
1672 + if (elf_bad_symtab (abfd))
1673 + {
1674 + extsymcount = symcount;
1675 + extsymoff = 0;
1676 + }
1677 + else
1678 + {
1679 + extsymcount = symcount - hdr->sh_info;
1680 + extsymoff = hdr->sh_info;
1681 + }
1682 +
1683 + sym_hash = NULL;
1684 + if (extsymcount != 0)
1685 + {
1686 + isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
1687 + NULL, NULL, NULL);
1688 + if (isymbuf == NULL)
1689 + goto error_return;
1690 +
1691 + /* We store a pointer to the hash table entry for each external
1692 + symbol. */
1693 + amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1694 + sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1695 + if (sym_hash == NULL)
1696 + goto error_free_sym;
1697 + elf_sym_hashes (abfd) = sym_hash;
1698 + }
1699 +
1700 + if (dynamic)
1701 + {
1702 + /* Read in any version definitions. */
1703 + if (! _bfd_elf_slurp_version_tables (abfd))
1704 + goto error_free_sym;
1705 +
1706 + /* Read in the symbol versions, but don't bother to convert them
1707 + to internal format. */
1708 + if (elf_dynversym (abfd) != 0)
1709 + {
1710 + Elf_Internal_Shdr *versymhdr;
1711 +
1712 + versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1713 + extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1714 + if (extversym == NULL)
1715 + goto error_free_sym;
1716 + amt = versymhdr->sh_size;
1717 + if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1718 + || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1719 + goto error_free_vers;
1720 + }
1721 + }
1722 +
1723 + weaks = NULL;
1724 +
1725 + ever = extversym != NULL ? extversym + extsymoff : NULL;
1726 + for (isym = isymbuf, isymend = isymbuf + extsymcount;
1727 + isym < isymend;
1728 + isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
1729 + {
1730 + int bind;
1731 + bfd_vma value;
1732 + asection *sec;
1733 + flagword flags;
1734 + const char *name;
1735 + struct elf_link_hash_entry *h;
1736 + boolean definition;
1737 + boolean size_change_ok, type_change_ok;
1738 + boolean new_weakdef;
1739 + unsigned int old_alignment;
1740 + boolean override;
1741 +
1742 + override = false;
1743 +
1744 + flags = BSF_NO_FLAGS;
1745 + sec = NULL;
1746 + value = isym->st_value;
1747 + *sym_hash = NULL;
1748 +
1749 + bind = ELF_ST_BIND (isym->st_info);
1750 + if (bind == STB_LOCAL)
1751 + {
1752 + /* This should be impossible, since ELF requires that all
1753 + global symbols follow all local symbols, and that sh_info
1754 + point to the first global symbol. Unfortunatealy, Irix 5
1755 + screws this up. */
1756 + continue;
1757 + }
1758 + else if (bind == STB_GLOBAL)
1759 + {
1760 + if (isym->st_shndx != SHN_UNDEF
1761 + && isym->st_shndx != SHN_COMMON)
1762 + flags = BSF_GLOBAL;
1763 + }
1764 + else if (bind == STB_WEAK)
1765 + flags = BSF_WEAK;
1766 + else
1767 + {
1768 + /* Leave it up to the processor backend. */
1769 + }
1770 +
1771 + if (isym->st_shndx == SHN_UNDEF)
1772 + sec = bfd_und_section_ptr;
1773 + else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
1774 + {
1775 + sec = section_from_elf_index (abfd, isym->st_shndx);
1776 + if (sec == NULL)
1777 + sec = bfd_abs_section_ptr;
1778 + else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1779 + value -= sec->vma;
1780 + }
1781 + else if (isym->st_shndx == SHN_ABS)
1782 + sec = bfd_abs_section_ptr;
1783 + else if (isym->st_shndx == SHN_COMMON)
1784 + {
1785 + sec = bfd_com_section_ptr;
1786 + /* What ELF calls the size we call the value. What ELF
1787 + calls the value we call the alignment. */
1788 + value = isym->st_size;
1789 + }
1790 + else
1791 + {
1792 + /* Leave it up to the processor backend. */
1793 + }
1794 +
1795 + name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
1796 + isym->st_name);
1797 + if (name == (const char *) NULL)
1798 + goto error_free_vers;
1799 +
1800 + if (isym->st_shndx == SHN_COMMON
1801 + && ELF_ST_TYPE (isym->st_info) == STT_TLS)
1802 + {
1803 + asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
1804 +
1805 + if (tcomm == NULL)
1806 + {
1807 + tcomm = bfd_make_section (abfd, ".tcommon");
1808 + if (tcomm == NULL
1809 + || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
1810 + | SEC_IS_COMMON
1811 + | SEC_LINKER_CREATED
1812 + | SEC_THREAD_LOCAL)))
1813 + goto error_free_vers;
1814 + }
1815 + sec = tcomm;
1816 + }
1817 + else if (add_symbol_hook)
1818 + {
1819 + if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
1820 + &value))
1821 + goto error_free_vers;
1822 +
1823 + /* The hook function sets the name to NULL if this symbol
1824 + should be skipped for some reason. */
1825 + if (name == (const char *) NULL)
1826 + continue;
1827 + }
1828 +
1829 + /* Sanity check that all possibilities were handled. */
1830 + if (sec == (asection *) NULL)
1831 + {
1832 + bfd_set_error (bfd_error_bad_value);
1833 + goto error_free_vers;
1834 + }
1835 +
1836 + if (bfd_is_und_section (sec)
1837 + || bfd_is_com_section (sec))
1838 + definition = false;
1839 + else
1840 + definition = true;
1841 +
1842 + size_change_ok = false;
1843 + type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1844 + old_alignment = 0;
1845 + if (info->hash->creator->flavour == bfd_target_elf_flavour)
1846 + {
1847 + Elf_Internal_Versym iver;
1848 + unsigned int vernum = 0;
1849 +
1850 + if (ever != NULL)
1851 + {
1852 + _bfd_elf_swap_versym_in (abfd, ever, &iver);
1853 + vernum = iver.vs_vers & VERSYM_VERSION;
1854 +
1855 + /* If this is a hidden symbol, or if it is not version
1856 + 1, we append the version name to the symbol name.
1857 + However, we do not modify a non-hidden absolute
1858 + symbol, because it might be the version symbol
1859 + itself. FIXME: What if it isn't? */
1860 + if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1861 + || (vernum > 1 && ! bfd_is_abs_section (sec)))
1862 + {
1863 + const char *verstr;
1864 + size_t namelen, verlen, newlen;
1865 + char *newname, *p;
1866 +
1867 + if (isym->st_shndx != SHN_UNDEF)
1868 + {
1869 + if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1870 + {
1871 + (*_bfd_error_handler)
1872 + (_("%s: %s: invalid version %u (max %d)"),
1873 + bfd_archive_filename (abfd), name, vernum,
1874 + elf_tdata (abfd)->dynverdef_hdr.sh_info);
1875 + bfd_set_error (bfd_error_bad_value);
1876 + goto error_free_vers;
1877 + }
1878 + else if (vernum > 1)
1879 + verstr =
1880 + elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1881 + else
1882 + verstr = "";
1883 + }
1884 + else
1885 + {
1886 + /* We cannot simply test for the number of
1887 + entries in the VERNEED section since the
1888 + numbers for the needed versions do not start
1889 + at 0. */
1890 + Elf_Internal_Verneed *t;
1891 +
1892 + verstr = NULL;
1893 + for (t = elf_tdata (abfd)->verref;
1894 + t != NULL;
1895 + t = t->vn_nextref)
1896 + {
1897 + Elf_Internal_Vernaux *a;
1898 +
1899 + for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1900 + {
1901 + if (a->vna_other == vernum)
1902 + {
1903 + verstr = a->vna_nodename;
1904 + break;
1905 + }
1906 + }
1907 + if (a != NULL)
1908 + break;
1909 + }
1910 + if (verstr == NULL)
1911 + {
1912 + (*_bfd_error_handler)
1913 + (_("%s: %s: invalid needed version %d"),
1914 + bfd_archive_filename (abfd), name, vernum);
1915 + bfd_set_error (bfd_error_bad_value);
1916 + goto error_free_vers;
1917 + }
1918 + }
1919 +
1920 + namelen = strlen (name);
1921 + verlen = strlen (verstr);
1922 + newlen = namelen + verlen + 2;
1923 + if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1924 + && isym->st_shndx != SHN_UNDEF)
1925 + ++newlen;
1926 +
1927 + newname = (char *) bfd_alloc (abfd, (bfd_size_type) newlen);
1928 + if (newname == NULL)
1929 + goto error_free_vers;
1930 + memcpy (newname, name, namelen);
1931 + p = newname + namelen;
1932 + *p++ = ELF_VER_CHR;
1933 + /* If this is a defined non-hidden version symbol,
1934 + we add another @ to the name. This indicates the
1935 + default version of the symbol. */
1936 + if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1937 + && isym->st_shndx != SHN_UNDEF)
1938 + *p++ = ELF_VER_CHR;
1939 + memcpy (p, verstr, verlen + 1);
1940 +
1941 + name = newname;
1942 + }
1943 + }
1944 +
1945 + if (! elf_merge_symbol (abfd, info, name, isym, &sec, &value,
1946 + sym_hash, &override, &type_change_ok,
1947 + &size_change_ok, dt_needed))
1948 + goto error_free_vers;
1949 +
1950 + if (override)
1951 + definition = false;
1952 +
1953 + h = *sym_hash;
1954 + while (h->root.type == bfd_link_hash_indirect
1955 + || h->root.type == bfd_link_hash_warning)
1956 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
1957 +
1958 + /* Remember the old alignment if this is a common symbol, so
1959 + that we don't reduce the alignment later on. We can't
1960 + check later, because _bfd_generic_link_add_one_symbol
1961 + will set a default for the alignment which we want to
1962 + override. */
1963 + if (h->root.type == bfd_link_hash_common)
1964 + old_alignment = h->root.u.c.p->alignment_power;
1965 +
1966 + if (elf_tdata (abfd)->verdef != NULL
1967 + && ! override
1968 + && vernum > 1
1969 + && definition)
1970 + h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1971 + }
1972 +
1973 + if (! (_bfd_generic_link_add_one_symbol
1974 + (info, abfd, name, flags, sec, value, (const char *) NULL,
1975 + false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1976 + goto error_free_vers;
1977 +
1978 + h = *sym_hash;
1979 + while (h->root.type == bfd_link_hash_indirect
1980 + || h->root.type == bfd_link_hash_warning)
1981 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
1982 + *sym_hash = h;
1983 +
1984 + new_weakdef = false;
1985 + if (dynamic
1986 + && definition
1987 + && (flags & BSF_WEAK) != 0
1988 + && ELF_ST_TYPE (isym->st_info) != STT_FUNC
1989 + && info->hash->creator->flavour == bfd_target_elf_flavour
1990 + && h->weakdef == NULL)
1991 + {
1992 + /* Keep a list of all weak defined non function symbols from
1993 + a dynamic object, using the weakdef field. Later in this
1994 + function we will set the weakdef field to the correct
1995 + value. We only put non-function symbols from dynamic
1996 + objects on this list, because that happens to be the only
1997 + time we need to know the normal symbol corresponding to a
1998 + weak symbol, and the information is time consuming to
1999 + figure out. If the weakdef field is not already NULL,
2000 + then this symbol was already defined by some previous
2001 + dynamic object, and we will be using that previous
2002 + definition anyhow. */
2003 +
2004 + h->weakdef = weaks;
2005 + weaks = h;
2006 + new_weakdef = true;
2007 + }
2008 +
2009 + /* Set the alignment of a common symbol. */
2010 + if (isym->st_shndx == SHN_COMMON
2011 + && h->root.type == bfd_link_hash_common)
2012 + {
2013 + unsigned int align;
2014 +
2015 + align = bfd_log2 (isym->st_value);
2016 + if (align > old_alignment
2017 + /* Permit an alignment power of zero if an alignment of one
2018 + is specified and no other alignments have been specified. */
2019 + || (isym->st_value == 1 && old_alignment == 0))
2020 + h->root.u.c.p->alignment_power = align;
2021 + }
2022 +
2023 + if (info->hash->creator->flavour == bfd_target_elf_flavour)
2024 + {
2025 + int old_flags;
2026 + boolean dynsym;
2027 + int new_flag;
2028 +
2029 + /* Remember the symbol size and type. */
2030 + if (isym->st_size != 0
2031 + && (definition || h->size == 0))
2032 + {
2033 + if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
2034 + (*_bfd_error_handler)
2035 + (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
2036 + name, (unsigned long) h->size,
2037 + (unsigned long) isym->st_size, bfd_archive_filename (abfd));
2038 +
2039 + h->size = isym->st_size;
2040 + }
2041 +
2042 + /* If this is a common symbol, then we always want H->SIZE
2043 + to be the size of the common symbol. The code just above
2044 + won't fix the size if a common symbol becomes larger. We
2045 + don't warn about a size change here, because that is
2046 + covered by --warn-common. */
2047 + if (h->root.type == bfd_link_hash_common)
2048 + h->size = h->root.u.c.size;
2049 +
2050 + if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
2051 + && (definition || h->type == STT_NOTYPE))
2052 + {
2053 + if (h->type != STT_NOTYPE
2054 + && h->type != ELF_ST_TYPE (isym->st_info)
2055 + && ! type_change_ok)
2056 + (*_bfd_error_handler)
2057 + (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
2058 + name, h->type, ELF_ST_TYPE (isym->st_info),
2059 + bfd_archive_filename (abfd));
2060 +
2061 + h->type = ELF_ST_TYPE (isym->st_info);
2062 + }
2063 +
2064 + /* If st_other has a processor-specific meaning, specific code
2065 + might be needed here. */
2066 + if (isym->st_other != 0)
2067 + {
2068 + unsigned char hvis, symvis, other;
2069 +
2070 + /* Take the balance of OTHER from the definition. */
2071 + other = (definition ? isym->st_other : h->other);
2072 + other &= ~ ELF_ST_VISIBILITY (-1);
2073 +
2074 + /* Combine visibilities, using the most constraining one. */
2075 + hvis = ELF_ST_VISIBILITY (h->other);
2076 + symvis = ELF_ST_VISIBILITY (isym->st_other);
2077 +
2078 + h->other = other | (hvis > symvis ? hvis : symvis);
2079 + }
2080 +
2081 + /* Set a flag in the hash table entry indicating the type of
2082 + reference or definition we just found. Keep a count of
2083 + the number of dynamic symbols we find. A dynamic symbol
2084 + is one which is referenced or defined by both a regular
2085 + object and a shared object. */
2086 + old_flags = h->elf_link_hash_flags;
2087 + dynsym = false;
2088 + if (! dynamic)
2089 + {
2090 + if (! definition)
2091 + {
2092 + new_flag = ELF_LINK_HASH_REF_REGULAR;
2093 + if (bind != STB_WEAK)
2094 + new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
2095 + }
2096 + else
2097 + new_flag = ELF_LINK_HASH_DEF_REGULAR;
2098 + if (info->shared
2099 + || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2100 + | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
2101 + dynsym = true;
2102 + }
2103 + else
2104 + {
2105 + if (! definition)
2106 + new_flag = ELF_LINK_HASH_REF_DYNAMIC;
2107 + else
2108 + new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
2109 + if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
2110 + | ELF_LINK_HASH_REF_REGULAR)) != 0
2111 + || (h->weakdef != NULL
2112 + && ! new_weakdef
2113 + && h->weakdef->dynindx != -1))
2114 + dynsym = true;
2115 + }
2116 +
2117 + h->elf_link_hash_flags |= new_flag;
2118 +
2119 + /* Check to see if we need to add an indirect symbol for
2120 + the default name. */
2121 + if (definition || h->root.type == bfd_link_hash_common)
2122 + if (! elf_add_default_symbol (abfd, info, h, name, isym,
2123 + &sec, &value, &dynsym,
2124 + override, dt_needed))
2125 + goto error_free_vers;
2126 +
2127 + if (dynsym && h->dynindx == -1)
2128 + {
2129 + if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2130 + goto error_free_vers;
2131 + if (h->weakdef != NULL
2132 + && ! new_weakdef
2133 + && h->weakdef->dynindx == -1)
2134 + {
2135 + if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2136 + goto error_free_vers;
2137 + }
2138 + }
2139 + else if (dynsym && h->dynindx != -1)
2140 + /* If the symbol already has a dynamic index, but
2141 + visibility says it should not be visible, turn it into
2142 + a local symbol. */
2143 + switch (ELF_ST_VISIBILITY (h->other))
2144 + {
2145 + case STV_INTERNAL:
2146 + case STV_HIDDEN:
2147 + (*bed->elf_backend_hide_symbol) (info, h, true);
2148 + break;
2149 + }
2150 +
2151 + if (dt_needed && definition
2152 + && (h->elf_link_hash_flags
2153 + & ELF_LINK_HASH_REF_REGULAR) != 0)
2154 + {
2155 + bfd_size_type oldsize;
2156 + bfd_size_type strindex;
2157 +
2158 + if (! is_elf_hash_table (info))
2159 + goto error_free_vers;
2160 +
2161 + /* The symbol from a DT_NEEDED object is referenced from
2162 + the regular object to create a dynamic executable. We
2163 + have to make sure there is a DT_NEEDED entry for it. */
2164 +
2165 + dt_needed = false;
2166 + oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2167 + strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2168 + elf_dt_soname (abfd), false);
2169 + if (strindex == (bfd_size_type) -1)
2170 + goto error_free_vers;
2171 +
2172 + if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2173 + {
2174 + asection *sdyn;
2175 + Elf_External_Dyn *dyncon, *dynconend;
2176 +
2177 + sdyn = bfd_get_section_by_name (hash_table->dynobj,
2178 + ".dynamic");
2179 + BFD_ASSERT (sdyn != NULL);
2180 +
2181 + dyncon = (Elf_External_Dyn *) sdyn->contents;
2182 + dynconend = (Elf_External_Dyn *) (sdyn->contents +
2183 + sdyn->_raw_size);
2184 + for (; dyncon < dynconend; dyncon++)
2185 + {
2186 + Elf_Internal_Dyn dyn;
2187 +
2188 + elf_swap_dyn_in (hash_table->dynobj,
2189 + dyncon, &dyn);
2190 + BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2191 + dyn.d_un.d_val != strindex);
2192 + }
2193 + }
2194 +
2195 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2196 + goto error_free_vers;
2197 + }
2198 + }
2199 + }
2200 +
2201 + if (extversym != NULL)
2202 + {
2203 + free (extversym);
2204 + extversym = NULL;
2205 + }
2206 +
2207 + if (isymbuf != NULL)
2208 + free (isymbuf);
2209 + isymbuf = NULL;
2210 +
2211 + /* Now set the weakdefs field correctly for all the weak defined
2212 + symbols we found. The only way to do this is to search all the
2213 + symbols. Since we only need the information for non functions in
2214 + dynamic objects, that's the only time we actually put anything on
2215 + the list WEAKS. We need this information so that if a regular
2216 + object refers to a symbol defined weakly in a dynamic object, the
2217 + real symbol in the dynamic object is also put in the dynamic
2218 + symbols; we also must arrange for both symbols to point to the
2219 + same memory location. We could handle the general case of symbol
2220 + aliasing, but a general symbol alias can only be generated in
2221 + assembler code, handling it correctly would be very time
2222 + consuming, and other ELF linkers don't handle general aliasing
2223 + either. */
2224 + while (weaks != NULL)
2225 + {
2226 + struct elf_link_hash_entry *hlook;
2227 + asection *slook;
2228 + bfd_vma vlook;
2229 + struct elf_link_hash_entry **hpp;
2230 + struct elf_link_hash_entry **hppend;
2231 +
2232 + hlook = weaks;
2233 + weaks = hlook->weakdef;
2234 + hlook->weakdef = NULL;
2235 +
2236 + BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2237 + || hlook->root.type == bfd_link_hash_defweak
2238 + || hlook->root.type == bfd_link_hash_common
2239 + || hlook->root.type == bfd_link_hash_indirect);
2240 + slook = hlook->root.u.def.section;
2241 + vlook = hlook->root.u.def.value;
2242 +
2243 + hpp = elf_sym_hashes (abfd);
2244 + hppend = hpp + extsymcount;
2245 + for (; hpp < hppend; hpp++)
2246 + {
2247 + struct elf_link_hash_entry *h;
2248 +
2249 + h = *hpp;
2250 + if (h != NULL && h != hlook
2251 + && h->root.type == bfd_link_hash_defined
2252 + && h->root.u.def.section == slook
2253 + && h->root.u.def.value == vlook)
2254 + {
2255 + hlook->weakdef = h;
2256 +
2257 + /* If the weak definition is in the list of dynamic
2258 + symbols, make sure the real definition is put there
2259 + as well. */
2260 + if (hlook->dynindx != -1
2261 + && h->dynindx == -1)
2262 + {
2263 + if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2264 + goto error_return;
2265 + }
2266 +
2267 + /* If the real definition is in the list of dynamic
2268 + symbols, make sure the weak definition is put there
2269 + as well. If we don't do this, then the dynamic
2270 + loader might not merge the entries for the real
2271 + definition and the weak definition. */
2272 + if (h->dynindx != -1
2273 + && hlook->dynindx == -1)
2274 + {
2275 + if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2276 + goto error_return;
2277 + }
2278 + break;
2279 + }
2280 + }
2281 + }
2282 +
2283 + /* If this object is the same format as the output object, and it is
2284 + not a shared library, then let the backend look through the
2285 + relocs.
2286 +
2287 + This is required to build global offset table entries and to
2288 + arrange for dynamic relocs. It is not required for the
2289 + particular common case of linking non PIC code, even when linking
2290 + against shared libraries, but unfortunately there is no way of
2291 + knowing whether an object file has been compiled PIC or not.
2292 + Looking through the relocs is not particularly time consuming.
2293 + The problem is that we must either (1) keep the relocs in memory,
2294 + which causes the linker to require additional runtime memory or
2295 + (2) read the relocs twice from the input file, which wastes time.
2296 + This would be a good case for using mmap.
2297 +
2298 + I have no idea how to handle linking PIC code into a file of a
2299 + different format. It probably can't be done. */
2300 + check_relocs = get_elf_backend_data (abfd)->check_relocs;
2301 + if (! dynamic
2302 + && abfd->xvec == info->hash->creator
2303 + && check_relocs != NULL)
2304 + {
2305 + asection *o;
2306 +
2307 + for (o = abfd->sections; o != NULL; o = o->next)
2308 + {
2309 + Elf_Internal_Rela *internal_relocs;
2310 + boolean ok;
2311 +
2312 + if ((o->flags & SEC_RELOC) == 0
2313 + || o->reloc_count == 0
2314 + || ((info->strip == strip_all || info->strip == strip_debugger)
2315 + && (o->flags & SEC_DEBUGGING) != 0)
2316 + || bfd_is_abs_section (o->output_section))
2317 + continue;
2318 +
2319 + internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2320 + (abfd, o, (PTR) NULL,
2321 + (Elf_Internal_Rela *) NULL,
2322 + info->keep_memory));
2323 + if (internal_relocs == NULL)
2324 + goto error_return;
2325 +
2326 + ok = (*check_relocs) (abfd, info, o, internal_relocs);
2327 +
2328 + if (elf_section_data (o)->relocs != internal_relocs)
2329 + free (internal_relocs);
2330 +
2331 + if (! ok)
2332 + goto error_return;
2333 + }
2334 + }
2335 +
2336 + /* If this is a non-traditional, non-relocateable link, try to
2337 + optimize the handling of the .stab/.stabstr sections. */
2338 + if (! dynamic
2339 + && ! info->relocateable
2340 + && ! info->traditional_format
2341 + && info->hash->creator->flavour == bfd_target_elf_flavour
2342 + && is_elf_hash_table (info)
2343 + && (info->strip != strip_all && info->strip != strip_debugger))
2344 + {
2345 + asection *stab, *stabstr;
2346 +
2347 + stab = bfd_get_section_by_name (abfd, ".stab");
2348 + if (stab != NULL
2349 + && (stab->flags & SEC_MERGE) == 0
2350 + && !bfd_is_abs_section (stab->output_section))
2351 + {
2352 + stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2353 +
2354 + if (stabstr != NULL)
2355 + {
2356 + struct bfd_elf_section_data *secdata;
2357 +
2358 + secdata = elf_section_data (stab);
2359 + if (! _bfd_link_section_stabs (abfd,
2360 + & hash_table->stab_info,
2361 + stab, stabstr,
2362 + &secdata->sec_info))
2363 + goto error_return;
2364 + if (secdata->sec_info)
2365 + secdata->sec_info_type = ELF_INFO_TYPE_STABS;
2366 + }
2367 + }
2368 + }
2369 +
2370 + if (! info->relocateable && ! dynamic
2371 + && is_elf_hash_table (info))
2372 + {
2373 + asection *s;
2374 +
2375 + for (s = abfd->sections; s != NULL; s = s->next)
2376 + if ((s->flags & SEC_MERGE) != 0
2377 + && !bfd_is_abs_section (s->output_section))
2378 + {
2379 + struct bfd_elf_section_data *secdata;
2380 +
2381 + secdata = elf_section_data (s);
2382 + if (! _bfd_merge_section (abfd,
2383 + & hash_table->merge_info,
2384 + s, &secdata->sec_info))
2385 + goto error_return;
2386 + else if (secdata->sec_info)
2387 + secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2388 + }
2389 + }
2390 +
2391 + if (is_elf_hash_table (info))
2392 + {
2393 + /* Add this bfd to the loaded list. */
2394 + struct elf_link_loaded_list *n;
2395 +
2396 + n = ((struct elf_link_loaded_list *)
2397 + bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)));
2398 + if (n == NULL)
2399 + goto error_return;
2400 + n->abfd = abfd;
2401 + n->next = hash_table->loaded;
2402 + hash_table->loaded = n;
2403 + }
2404 +
2405 + return true;
2406 +
2407 + error_free_vers:
2408 + if (extversym != NULL)
2409 + free (extversym);
2410 + error_free_sym:
2411 + if (isymbuf != NULL)
2412 + free (isymbuf);
2413 + error_return:
2414 + return false;
2415 +}
2416 +
2417 +/* Create some sections which will be filled in with dynamic linking
2418 + information. ABFD is an input file which requires dynamic sections
2419 + to be created. The dynamic sections take up virtual memory space
2420 + when the final executable is run, so we need to create them before
2421 + addresses are assigned to the output sections. We work out the
2422 + actual contents and size of these sections later. */
2423 +
2424 +boolean
2425 +elf_link_create_dynamic_sections (abfd, info)
2426 + bfd *abfd;
2427 + struct bfd_link_info *info;
2428 +{
2429 + flagword flags;
2430 + register asection *s;
2431 + struct elf_link_hash_entry *h;
2432 + struct bfd_link_hash_entry *bh;
2433 + struct elf_backend_data *bed;
2434 +
2435 + if (! is_elf_hash_table (info))
2436 + return false;
2437 +
2438 + if (elf_hash_table (info)->dynamic_sections_created)
2439 + return true;
2440 +
2441 + /* Make sure that all dynamic sections use the same input BFD. */
2442 + if (elf_hash_table (info)->dynobj == NULL)
2443 + elf_hash_table (info)->dynobj = abfd;
2444 + else
2445 + abfd = elf_hash_table (info)->dynobj;
2446 +
2447 + /* Note that we set the SEC_IN_MEMORY flag for all of these
2448 + sections. */
2449 + flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2450 + | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2451 +
2452 + /* A dynamically linked executable has a .interp section, but a
2453 + shared library does not. */
2454 + if (! info->shared)
2455 + {
2456 + s = bfd_make_section (abfd, ".interp");
2457 + if (s == NULL
2458 + || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2459 + return false;
2460 + }
2461 +
2462 + if (! info->traditional_format
2463 + && info->hash->creator->flavour == bfd_target_elf_flavour)
2464 + {
2465 + s = bfd_make_section (abfd, ".eh_frame_hdr");
2466 + if (s == NULL
2467 + || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2468 + || ! bfd_set_section_alignment (abfd, s, 2))
2469 + return false;
2470 + }
2471 +
2472 + /* Create sections to hold version informations. These are removed
2473 + if they are not needed. */
2474 + s = bfd_make_section (abfd, ".gnu.version_d");
2475 + if (s == NULL
2476 + || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2477 + || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2478 + return false;
2479 +
2480 + s = bfd_make_section (abfd, ".gnu.version");
2481 + if (s == NULL
2482 + || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2483 + || ! bfd_set_section_alignment (abfd, s, 1))
2484 + return false;
2485 +
2486 + s = bfd_make_section (abfd, ".gnu.version_r");
2487 + if (s == NULL
2488 + || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2489 + || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2490 + return false;
2491 +
2492 + s = bfd_make_section (abfd, ".dynsym");
2493 + if (s == NULL
2494 + || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2495 + || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2496 + return false;
2497 +
2498 + s = bfd_make_section (abfd, ".dynstr");
2499 + if (s == NULL
2500 + || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2501 + return false;
2502 +
2503 + /* Create a strtab to hold the dynamic symbol names. */
2504 + if (elf_hash_table (info)->dynstr == NULL)
2505 + {
2506 + elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2507 + if (elf_hash_table (info)->dynstr == NULL)
2508 + return false;
2509 + }
2510 +
2511 + s = bfd_make_section (abfd, ".dynamic");
2512 + if (s == NULL
2513 + || ! bfd_set_section_flags (abfd, s, flags)
2514 + || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2515 + return false;
2516 +
2517 + /* The special symbol _DYNAMIC is always set to the start of the
2518 + .dynamic section. This call occurs before we have processed the
2519 + symbols for any dynamic object, so we don't have to worry about
2520 + overriding a dynamic definition. We could set _DYNAMIC in a
2521 + linker script, but we only want to define it if we are, in fact,
2522 + creating a .dynamic section. We don't want to define it if there
2523 + is no .dynamic section, since on some ELF platforms the start up
2524 + code examines it to decide how to initialize the process. */
2525 + bh = NULL;
2526 + if (! (_bfd_generic_link_add_one_symbol
2527 + (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2528 + (const char *) 0, false, get_elf_backend_data (abfd)->collect, &bh)))
2529 + return false;
2530 + h = (struct elf_link_hash_entry *) bh;
2531 + h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2532 + h->type = STT_OBJECT;
2533 +
2534 + if (info->shared
2535 + && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2536 + return false;
2537 +
2538 + bed = get_elf_backend_data (abfd);
2539 +
2540 + s = bfd_make_section (abfd, ".hash");
2541 + if (s == NULL
2542 + || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2543 + || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2544 + return false;
2545 + elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2546 +
2547 + /* Let the backend create the rest of the sections. This lets the
2548 + backend set the right flags. The backend will normally create
2549 + the .got and .plt sections. */
2550 + if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2551 + return false;
2552 +
2553 + elf_hash_table (info)->dynamic_sections_created = true;
2554 +
2555 + return true;
2556 +}
2557 +
2558 +/* Add an entry to the .dynamic table. */
2559 +
2560 +boolean
2561 +elf_add_dynamic_entry (info, tag, val)
2562 + struct bfd_link_info *info;
2563 + bfd_vma tag;
2564 + bfd_vma val;
2565 +{
2566 + Elf_Internal_Dyn dyn;
2567 + bfd *dynobj;
2568 + asection *s;
2569 + bfd_size_type newsize;
2570 + bfd_byte *newcontents;
2571 +
2572 + if (! is_elf_hash_table (info))
2573 + return false;
2574 +
2575 + dynobj = elf_hash_table (info)->dynobj;
2576 +
2577 + s = bfd_get_section_by_name (dynobj, ".dynamic");
2578 + BFD_ASSERT (s != NULL);
2579 +
2580 + newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2581 + newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2582 + if (newcontents == NULL)
2583 + return false;
2584 +
2585 + dyn.d_tag = tag;
2586 + dyn.d_un.d_val = val;
2587 + elf_swap_dyn_out (dynobj, &dyn,
2588 + (Elf_External_Dyn *) (newcontents + s->_raw_size));
2589 +
2590 + s->_raw_size = newsize;
2591 + s->contents = newcontents;
2592 +
2593 + return true;
2594 +}
2595 +
2596 +/* Read and swap the relocs from the section indicated by SHDR. This
2597 + may be either a REL or a RELA section. The relocations are
2598 + translated into RELA relocations and stored in INTERNAL_RELOCS,
2599 + which should have already been allocated to contain enough space.
2600 + The EXTERNAL_RELOCS are a buffer where the external form of the
2601 + relocations should be stored.
2602 +
2603 + Returns false if something goes wrong. */
2604 +
2605 +static boolean
2606 +elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2607 + internal_relocs)
2608 + bfd *abfd;
2609 + Elf_Internal_Shdr *shdr;
2610 + PTR external_relocs;
2611 + Elf_Internal_Rela *internal_relocs;
2612 +{
2613 + struct elf_backend_data *bed;
2614 + bfd_size_type amt;
2615 +
2616 + /* If there aren't any relocations, that's OK. */
2617 + if (!shdr)
2618 + return true;
2619 +
2620 + /* Position ourselves at the start of the section. */
2621 + if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2622 + return false;
2623 +
2624 + /* Read the relocations. */
2625 + if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2626 + return false;
2627 +
2628 + bed = get_elf_backend_data (abfd);
2629 +
2630 + /* Convert the external relocations to the internal format. */
2631 + if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2632 + {
2633 + Elf_External_Rel *erel;
2634 + Elf_External_Rel *erelend;
2635 + Elf_Internal_Rela *irela;
2636 + Elf_Internal_Rel *irel;
2637 +
2638 + erel = (Elf_External_Rel *) external_relocs;
2639 + erelend = erel + NUM_SHDR_ENTRIES (shdr);
2640 + irela = internal_relocs;
2641 + amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2642 + irel = bfd_alloc (abfd, amt);
2643 + for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2644 + {
2645 + unsigned int i;
2646 +
2647 + if (bed->s->swap_reloc_in)
2648 + (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2649 + else
2650 + elf_swap_reloc_in (abfd, erel, irel);
2651 +
2652 + for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2653 + {
2654 + irela[i].r_offset = irel[i].r_offset;
2655 + irela[i].r_info = irel[i].r_info;
2656 + irela[i].r_addend = 0;
2657 + }
2658 + }
2659 + }
2660 + else
2661 + {
2662 + Elf_External_Rela *erela;
2663 + Elf_External_Rela *erelaend;
2664 + Elf_Internal_Rela *irela;
2665 +
2666 + BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2667 +
2668 + erela = (Elf_External_Rela *) external_relocs;
2669 + erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2670 + irela = internal_relocs;
2671 + for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2672 + {
2673 + if (bed->s->swap_reloca_in)
2674 + (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2675 + else
2676 + elf_swap_reloca_in (abfd, erela, irela);
2677 + }
2678 + }
2679 +
2680 + return true;
2681 +}
2682 +
2683 +/* Read and swap the relocs for a section O. They may have been
2684 + cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2685 + not NULL, they are used as buffers to read into. They are known to
2686 + be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2687 + the return value is allocated using either malloc or bfd_alloc,
2688 + according to the KEEP_MEMORY argument. If O has two relocation
2689 + sections (both REL and RELA relocations), then the REL_HDR
2690 + relocations will appear first in INTERNAL_RELOCS, followed by the
2691 + REL_HDR2 relocations. */
2692 +
2693 +Elf_Internal_Rela *
2694 +NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2695 + keep_memory)
2696 + bfd *abfd;
2697 + asection *o;
2698 + PTR external_relocs;
2699 + Elf_Internal_Rela *internal_relocs;
2700 + boolean keep_memory;
2701 +{
2702 + Elf_Internal_Shdr *rel_hdr;
2703 + PTR alloc1 = NULL;
2704 + Elf_Internal_Rela *alloc2 = NULL;
2705 + struct elf_backend_data *bed = get_elf_backend_data (abfd);
2706 +
2707 + if (elf_section_data (o)->relocs != NULL)
2708 + return elf_section_data (o)->relocs;
2709 +
2710 + if (o->reloc_count == 0)
2711 + return NULL;
2712 +
2713 + rel_hdr = &elf_section_data (o)->rel_hdr;
2714 +
2715 + if (internal_relocs == NULL)
2716 + {
2717 + bfd_size_type size;
2718 +
2719 + size = o->reloc_count;
2720 + size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2721 + if (keep_memory)
2722 + internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2723 + else
2724 + internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2725 + if (internal_relocs == NULL)
2726 + goto error_return;
2727 + }
2728 +
2729 + if (external_relocs == NULL)
2730 + {
2731 + bfd_size_type size = rel_hdr->sh_size;
2732 +
2733 + if (elf_section_data (o)->rel_hdr2)
2734 + size += elf_section_data (o)->rel_hdr2->sh_size;
2735 + alloc1 = (PTR) bfd_malloc (size);
2736 + if (alloc1 == NULL)
2737 + goto error_return;
2738 + external_relocs = alloc1;
2739 + }
2740 +
2741 + if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2742 + external_relocs,
2743 + internal_relocs))
2744 + goto error_return;
2745 + if (!elf_link_read_relocs_from_section
2746 + (abfd,
2747 + elf_section_data (o)->rel_hdr2,
2748 + ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2749 + internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2750 + * bed->s->int_rels_per_ext_rel)))
2751 + goto error_return;
2752 +
2753 + /* Cache the results for next time, if we can. */
2754 + if (keep_memory)
2755 + elf_section_data (o)->relocs = internal_relocs;
2756 +
2757 + if (alloc1 != NULL)
2758 + free (alloc1);
2759 +
2760 + /* Don't free alloc2, since if it was allocated we are passing it
2761 + back (under the name of internal_relocs). */
2762 +
2763 + return internal_relocs;
2764 +
2765 + error_return:
2766 + if (alloc1 != NULL)
2767 + free (alloc1);
2768 + if (alloc2 != NULL)
2769 + free (alloc2);
2770 + return NULL;
2771 +}
2772 +
2773 +/* Record an assignment to a symbol made by a linker script. We need
2774 + this in case some dynamic object refers to this symbol. */
2775 +
2776 +boolean
2777 +NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2778 + bfd *output_bfd ATTRIBUTE_UNUSED;
2779 + struct bfd_link_info *info;
2780 + const char *name;
2781 + boolean provide;
2782 +{
2783 + struct elf_link_hash_entry *h;
2784 +
2785 + if (info->hash->creator->flavour != bfd_target_elf_flavour)
2786 + return true;
2787 +
2788 + h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2789 + if (h == NULL)
2790 + return false;
2791 +
2792 + if (h->root.type == bfd_link_hash_new)
2793 + h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2794 +
2795 + /* If this symbol is being provided by the linker script, and it is
2796 + currently defined by a dynamic object, but not by a regular
2797 + object, then mark it as undefined so that the generic linker will
2798 + force the correct value. */
2799 + if (provide
2800 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2801 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2802 + h->root.type = bfd_link_hash_undefined;
2803 +
2804 + /* If this symbol is not being provided by the linker script, and it is
2805 + currently defined by a dynamic object, but not by a regular object,
2806 + then clear out any version information because the symbol will not be
2807 + associated with the dynamic object any more. */
2808 + if (!provide
2809 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2810 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2811 + h->verinfo.verdef = NULL;
2812 +
2813 + h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2814 +
2815 + if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2816 + | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2817 + || info->shared)
2818 + && h->dynindx == -1)
2819 + {
2820 + if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2821 + return false;
2822 +
2823 + /* If this is a weak defined symbol, and we know a corresponding
2824 + real symbol from the same dynamic object, make sure the real
2825 + symbol is also made into a dynamic symbol. */
2826 + if (h->weakdef != NULL
2827 + && h->weakdef->dynindx == -1)
2828 + {
2829 + if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2830 + return false;
2831 + }
2832 + }
2833 +
2834 + return true;
2835 +}
2836 +
2837 +/* This structure is used to pass information to
2838 + elf_link_assign_sym_version. */
2839 +
2840 +struct elf_assign_sym_version_info
2841 +{
2842 + /* Output BFD. */
2843 + bfd *output_bfd;
2844 + /* General link information. */
2845 + struct bfd_link_info *info;
2846 + /* Version tree. */
2847 + struct bfd_elf_version_tree *verdefs;
2848 + /* Whether we had a failure. */
2849 + boolean failed;
2850 +};
2851 +
2852 +/* This structure is used to pass information to
2853 + elf_link_find_version_dependencies. */
2854 +
2855 +struct elf_find_verdep_info
2856 +{
2857 + /* Output BFD. */
2858 + bfd *output_bfd;
2859 + /* General link information. */
2860 + struct bfd_link_info *info;
2861 + /* The number of dependencies. */
2862 + unsigned int vers;
2863 + /* Whether we had a failure. */
2864 + boolean failed;
2865 +};
2866 +
2867 +/* Array used to determine the number of hash table buckets to use
2868 + based on the number of symbols there are. If there are fewer than
2869 + 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2870 + fewer than 37 we use 17 buckets, and so forth. We never use more
2871 + than 32771 buckets. */
2872 +
2873 +static const size_t elf_buckets[] =
2874 +{
2875 + 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2876 + 16411, 32771, 0
2877 +};
2878 +
2879 +/* Compute bucket count for hashing table. We do not use a static set
2880 + of possible tables sizes anymore. Instead we determine for all
2881 + possible reasonable sizes of the table the outcome (i.e., the
2882 + number of collisions etc) and choose the best solution. The
2883 + weighting functions are not too simple to allow the table to grow
2884 + without bounds. Instead one of the weighting factors is the size.
2885 + Therefore the result is always a good payoff between few collisions
2886 + (= short chain lengths) and table size. */
2887 +static size_t
2888 +compute_bucket_count (info)
2889 + struct bfd_link_info *info;
2890 +{
2891 + size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2892 + size_t best_size = 0;
2893 + unsigned long int *hashcodes;
2894 + unsigned long int *hashcodesp;
2895 + unsigned long int i;
2896 + bfd_size_type amt;
2897 +
2898 + /* Compute the hash values for all exported symbols. At the same
2899 + time store the values in an array so that we could use them for
2900 + optimizations. */
2901 + amt = dynsymcount;
2902 + amt *= sizeof (unsigned long int);
2903 + hashcodes = (unsigned long int *) bfd_malloc (amt);
2904 + if (hashcodes == NULL)
2905 + return 0;
2906 + hashcodesp = hashcodes;
2907 +
2908 + /* Put all hash values in HASHCODES. */
2909 + elf_link_hash_traverse (elf_hash_table (info),
2910 + elf_collect_hash_codes, &hashcodesp);
2911 +
2912 + /* We have a problem here. The following code to optimize the table
2913 + size requires an integer type with more the 32 bits. If
2914 + BFD_HOST_U_64_BIT is set we know about such a type. */
2915 +#ifdef BFD_HOST_U_64_BIT
2916 + if (info->optimize)
2917 + {
2918 + unsigned long int nsyms = hashcodesp - hashcodes;
2919 + size_t minsize;
2920 + size_t maxsize;
2921 + BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2922 + unsigned long int *counts ;
2923 +
2924 + /* Possible optimization parameters: if we have NSYMS symbols we say
2925 + that the hashing table must at least have NSYMS/4 and at most
2926 + 2*NSYMS buckets. */
2927 + minsize = nsyms / 4;
2928 + if (minsize == 0)
2929 + minsize = 1;
2930 + best_size = maxsize = nsyms * 2;
2931 +
2932 + /* Create array where we count the collisions in. We must use bfd_malloc
2933 + since the size could be large. */
2934 + amt = maxsize;
2935 + amt *= sizeof (unsigned long int);
2936 + counts = (unsigned long int *) bfd_malloc (amt);
2937 + if (counts == NULL)
2938 + {
2939 + free (hashcodes);
2940 + return 0;
2941 + }
2942 +
2943 + /* Compute the "optimal" size for the hash table. The criteria is a
2944 + minimal chain length. The minor criteria is (of course) the size
2945 + of the table. */
2946 + for (i = minsize; i < maxsize; ++i)
2947 + {
2948 + /* Walk through the array of hashcodes and count the collisions. */
2949 + BFD_HOST_U_64_BIT max;
2950 + unsigned long int j;
2951 + unsigned long int fact;
2952 +
2953 + memset (counts, '\0', i * sizeof (unsigned long int));
2954 +
2955 + /* Determine how often each hash bucket is used. */
2956 + for (j = 0; j < nsyms; ++j)
2957 + ++counts[hashcodes[j] % i];
2958 +
2959 + /* For the weight function we need some information about the
2960 + pagesize on the target. This is information need not be 100%
2961 + accurate. Since this information is not available (so far) we
2962 + define it here to a reasonable default value. If it is crucial
2963 + to have a better value some day simply define this value. */
2964 +# ifndef BFD_TARGET_PAGESIZE
2965 +# define BFD_TARGET_PAGESIZE (4096)
2966 +# endif
2967 +
2968 + /* We in any case need 2 + NSYMS entries for the size values and
2969 + the chains. */
2970 + max = (2 + nsyms) * (ARCH_SIZE / 8);
2971 +
2972 +# if 1
2973 + /* Variant 1: optimize for short chains. We add the squares
2974 + of all the chain lengths (which favous many small chain
2975 + over a few long chains). */
2976 + for (j = 0; j < i; ++j)
2977 + max += counts[j] * counts[j];
2978 +
2979 + /* This adds penalties for the overall size of the table. */
2980 + fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2981 + max *= fact * fact;
2982 +# else
2983 + /* Variant 2: Optimize a lot more for small table. Here we
2984 + also add squares of the size but we also add penalties for
2985 + empty slots (the +1 term). */
2986 + for (j = 0; j < i; ++j)
2987 + max += (1 + counts[j]) * (1 + counts[j]);
2988 +
2989 + /* The overall size of the table is considered, but not as
2990 + strong as in variant 1, where it is squared. */
2991 + fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2992 + max *= fact;
2993 +# endif
2994 +
2995 + /* Compare with current best results. */
2996 + if (max < best_chlen)
2997 + {
2998 + best_chlen = max;
2999 + best_size = i;
3000 + }
3001 + }
3002 +
3003 + free (counts);
3004 + }
3005 + else
3006 +#endif /* defined (BFD_HOST_U_64_BIT) */
3007 + {
3008 + /* This is the fallback solution if no 64bit type is available or if we
3009 + are not supposed to spend much time on optimizations. We select the
3010 + bucket count using a fixed set of numbers. */
3011 + for (i = 0; elf_buckets[i] != 0; i++)
3012 + {
3013 + best_size = elf_buckets[i];
3014 + if (dynsymcount < elf_buckets[i + 1])
3015 + break;
3016 + }
3017 + }
3018 +
3019 + /* Free the arrays we needed. */
3020 + free (hashcodes);
3021 +
3022 + return best_size;
3023 +}
3024 +
3025 +/* Set up the sizes and contents of the ELF dynamic sections. This is
3026 + called by the ELF linker emulation before_allocation routine. We
3027 + must set the sizes of the sections before the linker sets the
3028 + addresses of the various sections. */
3029 +
3030 +boolean
3031 +NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
3032 + filter_shlib,
3033 + auxiliary_filters, info, sinterpptr,
3034 + verdefs)
3035 + bfd *output_bfd;
3036 + const char *soname;
3037 + const char *rpath;
3038 + const char *filter_shlib;
3039 + const char * const *auxiliary_filters;
3040 + struct bfd_link_info *info;
3041 + asection **sinterpptr;
3042 + struct bfd_elf_version_tree *verdefs;
3043 +{
3044 + bfd_size_type soname_indx;
3045 + bfd *dynobj;
3046 + struct elf_backend_data *bed;
3047 + struct elf_assign_sym_version_info asvinfo;
3048 +
3049 + *sinterpptr = NULL;
3050 +
3051 + soname_indx = (bfd_size_type) -1;
3052 +
3053 + if (info->hash->creator->flavour != bfd_target_elf_flavour)
3054 + return true;
3055 +
3056 + if (! is_elf_hash_table (info))
3057 + return true;
3058 +
3059 + /* Any syms created from now on start with -1 in
3060 + got.refcount/offset and plt.refcount/offset. */
3061 + elf_hash_table (info)->init_refcount = -1;
3062 +
3063 + /* The backend may have to create some sections regardless of whether
3064 + we're dynamic or not. */
3065 + bed = get_elf_backend_data (output_bfd);
3066 + if (bed->elf_backend_always_size_sections
3067 + && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
3068 + return false;
3069 +
3070 + dynobj = elf_hash_table (info)->dynobj;
3071 +
3072 + /* If there were no dynamic objects in the link, there is nothing to
3073 + do here. */
3074 + if (dynobj == NULL)
3075 + return true;
3076 +
3077 + if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
3078 + return false;
3079 +
3080 + if (elf_hash_table (info)->dynamic_sections_created)
3081 + {
3082 + struct elf_info_failed eif;
3083 + struct elf_link_hash_entry *h;
3084 + asection *dynstr;
3085 + struct bfd_elf_version_tree *t;
3086 + struct bfd_elf_version_expr *d;
3087 + boolean all_defined;
3088 +
3089 + *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3090 + BFD_ASSERT (*sinterpptr != NULL || info->shared);
3091 +
3092 + if (soname != NULL)
3093 + {
3094 + soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3095 + soname, true);
3096 + if (soname_indx == (bfd_size_type) -1
3097 + || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3098 + soname_indx))
3099 + return false;
3100 + }
3101 +
3102 + if (info->symbolic)
3103 + {
3104 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3105 + (bfd_vma) 0))
3106 + return false;
3107 + info->flags |= DF_SYMBOLIC;
3108 + }
3109 +
3110 + if (rpath != NULL)
3111 + {
3112 + bfd_size_type indx;
3113 +
3114 + indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3115 + true);
3116 + if (info->new_dtags)
3117 + _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
3118 + if (indx == (bfd_size_type) -1
3119 + || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
3120 + || (info->new_dtags
3121 + && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3122 + indx)))
3123 + return false;
3124 + }
3125 +
3126 + if (filter_shlib != NULL)
3127 + {
3128 + bfd_size_type indx;
3129 +
3130 + indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3131 + filter_shlib, true);
3132 + if (indx == (bfd_size_type) -1
3133 + || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
3134 + return false;
3135 + }
3136 +
3137 + if (auxiliary_filters != NULL)
3138 + {
3139 + const char * const *p;
3140 +
3141 + for (p = auxiliary_filters; *p != NULL; p++)
3142 + {
3143 + bfd_size_type indx;
3144 +
3145 + indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3146 + *p, true);
3147 + if (indx == (bfd_size_type) -1
3148 + || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3149 + indx))
3150 + return false;
3151 + }
3152 + }
3153 +
3154 + eif.info = info;
3155 + eif.verdefs = verdefs;
3156 + eif.failed = false;
3157 +
3158 + /* If we are supposed to export all symbols into the dynamic symbol
3159 + table (this is not the normal case), then do so. */
3160 + if (info->export_dynamic)
3161 + {
3162 + elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3163 + (PTR) &eif);
3164 + if (eif.failed)
3165 + return false;
3166 + }
3167 +
3168 + /* Make all global versions with definiton. */
3169 + for (t = verdefs; t != NULL; t = t->next)
3170 + for (d = t->globals; d != NULL; d = d->next)
3171 + if (!d->symver && strchr (d->pattern, '*') == NULL)
3172 + {
3173 + const char *verstr, *name;
3174 + size_t namelen, verlen, newlen;
3175 + char *newname, *p;
3176 + struct elf_link_hash_entry *newh;
3177 +
3178 + name = d->pattern;
3179 + namelen = strlen (name);
3180 + verstr = t->name;
3181 + verlen = strlen (verstr);
3182 + newlen = namelen + verlen + 3;
3183 +
3184 + newname = (char *) bfd_malloc ((bfd_size_type) newlen);
3185 + if (newname == NULL)
3186 + return false;
3187 + memcpy (newname, name, namelen);
3188 +
3189 + /* Check the hidden versioned definition. */
3190 + p = newname + namelen;
3191 + *p++ = ELF_VER_CHR;
3192 + memcpy (p, verstr, verlen + 1);
3193 + newh = elf_link_hash_lookup (elf_hash_table (info),
3194 + newname, false, false,
3195 + false);
3196 + if (newh == NULL
3197 + || (newh->root.type != bfd_link_hash_defined
3198 + && newh->root.type != bfd_link_hash_defweak))
3199 + {
3200 + /* Check the default versioned definition. */
3201 + *p++ = ELF_VER_CHR;
3202 + memcpy (p, verstr, verlen + 1);
3203 + newh = elf_link_hash_lookup (elf_hash_table (info),
3204 + newname, false, false,
3205 + false);
3206 + }
3207 + free (newname);
3208 +
3209 + /* Mark this version if there is a definition and it is
3210 + not defined in a shared object. */
3211 + if (newh != NULL
3212 + && ((newh->elf_link_hash_flags
3213 + & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
3214 + && (newh->root.type == bfd_link_hash_defined
3215 + || newh->root.type == bfd_link_hash_defweak))
3216 + d->symver = 1;
3217 + }
3218 +
3219 + /* Attach all the symbols to their version information. */
3220 + asvinfo.output_bfd = output_bfd;
3221 + asvinfo.info = info;
3222 + asvinfo.verdefs = verdefs;
3223 + asvinfo.failed = false;
3224 +
3225 + elf_link_hash_traverse (elf_hash_table (info),
3226 + elf_link_assign_sym_version,
3227 + (PTR) &asvinfo);
3228 + if (asvinfo.failed)
3229 + return false;
3230 +
3231 + if (!info->allow_undefined_version)
3232 + {
3233 + /* Check if all global versions have a definiton. */
3234 + all_defined = true;
3235 + for (t = verdefs; t != NULL; t = t->next)
3236 + for (d = t->globals; d != NULL; d = d->next)
3237 + if (!d->symver && !d->script
3238 + && strchr (d->pattern, '*') == NULL)
3239 + {
3240 + (*_bfd_error_handler)
3241 + (_("%s: undefined version: %s"),
3242 + d->pattern, t->name);
3243 + all_defined = false;
3244 + }
3245 +
3246 + if (!all_defined)
3247 + {
3248 + bfd_set_error (bfd_error_bad_value);
3249 + return false;
3250 + }
3251 + }
3252 +
3253 + /* Find all symbols which were defined in a dynamic object and make
3254 + the backend pick a reasonable value for them. */
3255 + elf_link_hash_traverse (elf_hash_table (info),
3256 + elf_adjust_dynamic_symbol,
3257 + (PTR) &eif);
3258 + if (eif.failed)
3259 + return false;
3260 +
3261 + /* Add some entries to the .dynamic section. We fill in some of the
3262 + values later, in elf_bfd_final_link, but we must add the entries
3263 + now so that we know the final size of the .dynamic section. */
3264 +
3265 + /* If there are initialization and/or finalization functions to
3266 + call then add the corresponding DT_INIT/DT_FINI entries. */
3267 + h = (info->init_function
3268 + ? elf_link_hash_lookup (elf_hash_table (info),
3269 + info->init_function, false,
3270 + false, false)
3271 + : NULL);
3272 + if (h != NULL
3273 + && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3274 + | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3275 + {
3276 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3277 + return false;
3278 + }
3279 + h = (info->fini_function
3280 + ? elf_link_hash_lookup (elf_hash_table (info),
3281 + info->fini_function, false,
3282 + false, false)
3283 + : NULL);
3284 + if (h != NULL
3285 + && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3286 + | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3287 + {
3288 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3289 + return false;
3290 + }
3291 +
3292 + if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
3293 + {
3294 + /* DT_PREINIT_ARRAY is not allowed in shared library. */
3295 + if (info->shared)
3296 + {
3297 + bfd *sub;
3298 + asection *o;
3299 +
3300 + for (sub = info->input_bfds; sub != NULL;
3301 + sub = sub->link_next)
3302 + for (o = sub->sections; o != NULL; o = o->next)
3303 + if (elf_section_data (o)->this_hdr.sh_type
3304 + == SHT_PREINIT_ARRAY)
3305 + {
3306 + (*_bfd_error_handler)
3307 + (_("%s: .preinit_array section is not allowed in DSO"),
3308 + bfd_archive_filename (sub));
3309 + break;
3310 + }
3311 +
3312 + bfd_set_error (bfd_error_nonrepresentable_section);
3313 + return false;
3314 + }
3315 +
3316 + if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
3317 + (bfd_vma) 0)
3318 + || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
3319 + (bfd_vma) 0))
3320 + return false;
3321 + }
3322 + if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
3323 + {
3324 + if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
3325 + (bfd_vma) 0)
3326 + || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
3327 + (bfd_vma) 0))
3328 + return false;
3329 + }
3330 + if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
3331 + {
3332 + if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
3333 + (bfd_vma) 0)
3334 + || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
3335 + (bfd_vma) 0))
3336 + return false;
3337 + }
3338 +
3339 + dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3340 + /* If .dynstr is excluded from the link, we don't want any of
3341 + these tags. Strictly, we should be checking each section
3342 + individually; This quick check covers for the case where
3343 + someone does a /DISCARD/ : { *(*) }. */
3344 + if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3345 + {
3346 + bfd_size_type strsize;
3347 +
3348 + strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3349 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3350 + || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3351 + || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3352 + || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3353 + || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3354 + (bfd_vma) sizeof (Elf_External_Sym)))
3355 + return false;
3356 + }
3357 + }
3358 +
3359 + /* The backend must work out the sizes of all the other dynamic
3360 + sections. */
3361 + if (bed->elf_backend_size_dynamic_sections
3362 + && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3363 + return false;
3364 +
3365 + if (elf_hash_table (info)->dynamic_sections_created)
3366 + {
3367 + bfd_size_type dynsymcount;
3368 + asection *s;
3369 + size_t bucketcount = 0;
3370 + size_t hash_entry_size;
3371 + unsigned int dtagcount;
3372 +
3373 + /* Set up the version definition section. */
3374 + s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3375 + BFD_ASSERT (s != NULL);
3376 +
3377 + /* We may have created additional version definitions if we are
3378 + just linking a regular application. */
3379 + verdefs = asvinfo.verdefs;
3380 +
3381 + /* Skip anonymous version tag. */
3382 + if (verdefs != NULL && verdefs->vernum == 0)
3383 + verdefs = verdefs->next;
3384 +
3385 + if (verdefs == NULL)
3386 + _bfd_strip_section_from_output (info, s);
3387 + else
3388 + {
3389 + unsigned int cdefs;
3390 + bfd_size_type size;
3391 + struct bfd_elf_version_tree *t;
3392 + bfd_byte *p;
3393 + Elf_Internal_Verdef def;
3394 + Elf_Internal_Verdaux defaux;
3395 +
3396 + cdefs = 0;
3397 + size = 0;
3398 +
3399 + /* Make space for the base version. */
3400 + size += sizeof (Elf_External_Verdef);
3401 + size += sizeof (Elf_External_Verdaux);
3402 + ++cdefs;
3403 +
3404 + for (t = verdefs; t != NULL; t = t->next)
3405 + {
3406 + struct bfd_elf_version_deps *n;
3407 +
3408 + size += sizeof (Elf_External_Verdef);
3409 + size += sizeof (Elf_External_Verdaux);
3410 + ++cdefs;
3411 +
3412 + for (n = t->deps; n != NULL; n = n->next)
3413 + size += sizeof (Elf_External_Verdaux);
3414 + }
3415 +
3416 + s->_raw_size = size;
3417 + s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3418 + if (s->contents == NULL && s->_raw_size != 0)
3419 + return false;
3420 +
3421 + /* Fill in the version definition section. */
3422 +
3423 + p = s->contents;
3424 +
3425 + def.vd_version = VER_DEF_CURRENT;
3426 + def.vd_flags = VER_FLG_BASE;
3427 + def.vd_ndx = 1;
3428 + def.vd_cnt = 1;
3429 + def.vd_aux = sizeof (Elf_External_Verdef);
3430 + def.vd_next = (sizeof (Elf_External_Verdef)
3431 + + sizeof (Elf_External_Verdaux));
3432 +
3433 + if (soname_indx != (bfd_size_type) -1)
3434 + {
3435 + _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3436 + soname_indx);
3437 + def.vd_hash = bfd_elf_hash (soname);
3438 + defaux.vda_name = soname_indx;
3439 + }
3440 + else
3441 + {
3442 + const char *name;
3443 + bfd_size_type indx;
3444 +
3445 + name = basename (output_bfd->filename);
3446 + def.vd_hash = bfd_elf_hash (name);
3447 + indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3448 + name, false);
3449 + if (indx == (bfd_size_type) -1)
3450 + return false;
3451 + defaux.vda_name = indx;
3452 + }
3453 + defaux.vda_next = 0;
3454 +
3455 + _bfd_elf_swap_verdef_out (output_bfd, &def,
3456 + (Elf_External_Verdef *) p);
3457 + p += sizeof (Elf_External_Verdef);
3458 + _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3459 + (Elf_External_Verdaux *) p);
3460 + p += sizeof (Elf_External_Verdaux);
3461 +
3462 + for (t = verdefs; t != NULL; t = t->next)
3463 + {
3464 + unsigned int cdeps;
3465 + struct bfd_elf_version_deps *n;
3466 + struct elf_link_hash_entry *h;
3467 + struct bfd_link_hash_entry *bh;
3468 +
3469 + cdeps = 0;
3470 + for (n = t->deps; n != NULL; n = n->next)
3471 + ++cdeps;
3472 +
3473 + /* Add a symbol representing this version. */
3474 + bh = NULL;
3475 + if (! (_bfd_generic_link_add_one_symbol
3476 + (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3477 + (bfd_vma) 0, (const char *) NULL, false,
3478 + get_elf_backend_data (dynobj)->collect, &bh)))
3479 + return false;
3480 + h = (struct elf_link_hash_entry *) bh;
3481 + h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3482 + h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3483 + h->type = STT_OBJECT;
3484 + h->verinfo.vertree = t;
3485 +
3486 + if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3487 + return false;
3488 +
3489 + def.vd_version = VER_DEF_CURRENT;
3490 + def.vd_flags = 0;
3491 + if (t->globals == NULL && t->locals == NULL && ! t->used)
3492 + def.vd_flags |= VER_FLG_WEAK;
3493 + def.vd_ndx = t->vernum + 1;
3494 + def.vd_cnt = cdeps + 1;
3495 + def.vd_hash = bfd_elf_hash (t->name);
3496 + def.vd_aux = sizeof (Elf_External_Verdef);
3497 + if (t->next != NULL)
3498 + def.vd_next = (sizeof (Elf_External_Verdef)
3499 + + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3500 + else
3501 + def.vd_next = 0;
3502 +
3503 + _bfd_elf_swap_verdef_out (output_bfd, &def,
3504 + (Elf_External_Verdef *) p);
3505 + p += sizeof (Elf_External_Verdef);
3506 +
3507 + defaux.vda_name = h->dynstr_index;
3508 + _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3509 + h->dynstr_index);
3510 + if (t->deps == NULL)
3511 + defaux.vda_next = 0;
3512 + else
3513 + defaux.vda_next = sizeof (Elf_External_Verdaux);
3514 + t->name_indx = defaux.vda_name;
3515 +
3516 + _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3517 + (Elf_External_Verdaux *) p);
3518 + p += sizeof (Elf_External_Verdaux);
3519 +
3520 + for (n = t->deps; n != NULL; n = n->next)
3521 + {
3522 + if (n->version_needed == NULL)
3523 + {
3524 + /* This can happen if there was an error in the
3525 + version script. */
3526 + defaux.vda_name = 0;
3527 + }
3528 + else
3529 + {
3530 + defaux.vda_name = n->version_needed->name_indx;
3531 + _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3532 + defaux.vda_name);
3533 + }
3534 + if (n->next == NULL)
3535 + defaux.vda_next = 0;
3536 + else
3537 + defaux.vda_next = sizeof (Elf_External_Verdaux);
3538 +
3539 + _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3540 + (Elf_External_Verdaux *) p);
3541 + p += sizeof (Elf_External_Verdaux);
3542 + }
3543 + }
3544 +
3545 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3546 + || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3547 + (bfd_vma) cdefs))
3548 + return false;
3549 +
3550 + elf_tdata (output_bfd)->cverdefs = cdefs;
3551 + }
3552 +
3553 + if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
3554 + {
3555 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3556 + return false;
3557 + }
3558 +
3559 + if (info->flags_1)
3560 + {
3561 + if (! info->shared)
3562 + info->flags_1 &= ~ (DF_1_INITFIRST
3563 + | DF_1_NODELETE
3564 + | DF_1_NOOPEN);
3565 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3566 + info->flags_1))
3567 + return false;
3568 + }
3569 +
3570 + /* Work out the size of the version reference section. */
3571 +
3572 + s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3573 + BFD_ASSERT (s != NULL);
3574 + {
3575 + struct elf_find_verdep_info sinfo;
3576 +
3577 + sinfo.output_bfd = output_bfd;
3578 + sinfo.info = info;
3579 + sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3580 + if (sinfo.vers == 0)
3581 + sinfo.vers = 1;
3582 + sinfo.failed = false;
3583 +
3584 + elf_link_hash_traverse (elf_hash_table (info),
3585 + elf_link_find_version_dependencies,
3586 + (PTR) &sinfo);
3587 +
3588 + if (elf_tdata (output_bfd)->verref == NULL)
3589 + _bfd_strip_section_from_output (info, s);
3590 + else
3591 + {
3592 + Elf_Internal_Verneed *t;
3593 + unsigned int size;
3594 + unsigned int crefs;
3595 + bfd_byte *p;
3596 +
3597 + /* Build the version definition section. */
3598 + size = 0;
3599 + crefs = 0;
3600 + for (t = elf_tdata (output_bfd)->verref;
3601 + t != NULL;
3602 + t = t->vn_nextref)
3603 + {
3604 + Elf_Internal_Vernaux *a;
3605 +
3606 + size += sizeof (Elf_External_Verneed);
3607 + ++crefs;
3608 + for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3609 + size += sizeof (Elf_External_Vernaux);
3610 + }
3611 +
3612 + s->_raw_size = size;
3613 + s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3614 + if (s->contents == NULL)
3615 + return false;
3616 +
3617 + p = s->contents;
3618 + for (t = elf_tdata (output_bfd)->verref;
3619 + t != NULL;
3620 + t = t->vn_nextref)
3621 + {
3622 + unsigned int caux;
3623 + Elf_Internal_Vernaux *a;
3624 + bfd_size_type indx;
3625 +
3626 + caux = 0;
3627 + for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3628 + ++caux;
3629 +
3630 + t->vn_version = VER_NEED_CURRENT;
3631 + t->vn_cnt = caux;
3632 + indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3633 + elf_dt_name (t->vn_bfd) != NULL
3634 + ? elf_dt_name (t->vn_bfd)
3635 + : basename (t->vn_bfd->filename),
3636 + false);
3637 + if (indx == (bfd_size_type) -1)
3638 + return false;
3639 + t->vn_file = indx;
3640 + t->vn_aux = sizeof (Elf_External_Verneed);
3641 + if (t->vn_nextref == NULL)
3642 + t->vn_next = 0;
3643 + else
3644 + t->vn_next = (sizeof (Elf_External_Verneed)
3645 + + caux * sizeof (Elf_External_Vernaux));
3646 +
3647 + _bfd_elf_swap_verneed_out (output_bfd, t,
3648 + (Elf_External_Verneed *) p);
3649 + p += sizeof (Elf_External_Verneed);
3650 +
3651 + for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3652 + {
3653 + a->vna_hash = bfd_elf_hash (a->vna_nodename);
3654 + indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3655 + a->vna_nodename, false);
3656 + if (indx == (bfd_size_type) -1)
3657 + return false;
3658 + a->vna_name = indx;
3659 + if (a->vna_nextptr == NULL)
3660 + a->vna_next = 0;
3661 + else
3662 + a->vna_next = sizeof (Elf_External_Vernaux);
3663 +
3664 + _bfd_elf_swap_vernaux_out (output_bfd, a,
3665 + (Elf_External_Vernaux *) p);
3666 + p += sizeof (Elf_External_Vernaux);
3667 + }
3668 + }
3669 +
3670 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3671 + (bfd_vma) 0)
3672 + || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3673 + (bfd_vma) crefs))
3674 + return false;
3675 +
3676 + elf_tdata (output_bfd)->cverrefs = crefs;
3677 + }
3678 + }
3679 +
3680 + /* Assign dynsym indicies. In a shared library we generate a
3681 + section symbol for each output section, which come first.
3682 + Next come all of the back-end allocated local dynamic syms,
3683 + followed by the rest of the global symbols. */
3684 +
3685 + dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3686 +
3687 + /* Work out the size of the symbol version section. */
3688 + s = bfd_get_section_by_name (dynobj, ".gnu.version");
3689 + BFD_ASSERT (s != NULL);
3690 + if (dynsymcount == 0
3691 + || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3692 + {
3693 + _bfd_strip_section_from_output (info, s);
3694 + /* The DYNSYMCOUNT might have changed if we were going to
3695 + output a dynamic symbol table entry for S. */
3696 + dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3697 + }
3698 + else
3699 + {
3700 + s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3701 + s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3702 + if (s->contents == NULL)
3703 + return false;
3704 +
3705 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3706 + return false;
3707 + }
3708 +
3709 + /* Set the size of the .dynsym and .hash sections. We counted
3710 + the number of dynamic symbols in elf_link_add_object_symbols.
3711 + We will build the contents of .dynsym and .hash when we build
3712 + the final symbol table, because until then we do not know the
3713 + correct value to give the symbols. We built the .dynstr
3714 + section as we went along in elf_link_add_object_symbols. */
3715 + s = bfd_get_section_by_name (dynobj, ".dynsym");
3716 + BFD_ASSERT (s != NULL);
3717 + s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3718 + s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3719 + if (s->contents == NULL && s->_raw_size != 0)
3720 + return false;
3721 +
3722 + if (dynsymcount != 0)
3723 + {
3724 + Elf_Internal_Sym isym;
3725 +
3726 + /* The first entry in .dynsym is a dummy symbol. */
3727 + isym.st_value = 0;
3728 + isym.st_size = 0;
3729 + isym.st_name = 0;
3730 + isym.st_info = 0;
3731 + isym.st_other = 0;
3732 + isym.st_shndx = 0;
3733 + elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
3734 + }
3735 +
3736 + /* Compute the size of the hashing table. As a side effect this
3737 + computes the hash values for all the names we export. */
3738 + bucketcount = compute_bucket_count (info);
3739 +
3740 + s = bfd_get_section_by_name (dynobj, ".hash");
3741 + BFD_ASSERT (s != NULL);
3742 + hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3743 + s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3744 + s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3745 + if (s->contents == NULL)
3746 + return false;
3747 +
3748 + bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3749 + s->contents);
3750 + bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3751 + s->contents + hash_entry_size);
3752 +
3753 + elf_hash_table (info)->bucketcount = bucketcount;
3754 +
3755 + s = bfd_get_section_by_name (dynobj, ".dynstr");
3756 + BFD_ASSERT (s != NULL);
3757 +
3758 + elf_finalize_dynstr (output_bfd, info);
3759 +
3760 + s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3761 +
3762 + for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3763 + if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3764 + return false;
3765 + }
3766 +
3767 + return true;
3768 +}
3769 +
3770 +/* This function is used to adjust offsets into .dynstr for
3771 + dynamic symbols. This is called via elf_link_hash_traverse. */
3772 +
3773 +static boolean elf_adjust_dynstr_offsets
3774 +PARAMS ((struct elf_link_hash_entry *, PTR));
3775 +
3776 +static boolean
3777 +elf_adjust_dynstr_offsets (h, data)
3778 + struct elf_link_hash_entry *h;
3779 + PTR data;
3780 +{
3781 + struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3782 +
3783 + if (h->root.type == bfd_link_hash_warning)
3784 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
3785 +
3786 + if (h->dynindx != -1)
3787 + h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3788 + return true;
3789 +}
3790 +
3791 +/* Assign string offsets in .dynstr, update all structures referencing
3792 + them. */
3793 +
3794 +static boolean
3795 +elf_finalize_dynstr (output_bfd, info)
3796 + bfd *output_bfd;
3797 + struct bfd_link_info *info;
3798 +{
3799 + struct elf_link_local_dynamic_entry *entry;
3800 + struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3801 + bfd *dynobj = elf_hash_table (info)->dynobj;
3802 + asection *sdyn;
3803 + bfd_size_type size;
3804 + Elf_External_Dyn *dyncon, *dynconend;
3805 +
3806 + _bfd_elf_strtab_finalize (dynstr);
3807 + size = _bfd_elf_strtab_size (dynstr);
3808 +
3809 + /* Update all .dynamic entries referencing .dynstr strings. */
3810 + sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3811 + BFD_ASSERT (sdyn != NULL);
3812 +
3813 + dyncon = (Elf_External_Dyn *) sdyn->contents;
3814 + dynconend = (Elf_External_Dyn *) (sdyn->contents +
3815 + sdyn->_raw_size);
3816 + for (; dyncon < dynconend; dyncon++)
3817 + {
3818 + Elf_Internal_Dyn dyn;
3819 +
3820 + elf_swap_dyn_in (dynobj, dyncon, & dyn);
3821 + switch (dyn.d_tag)
3822 + {
3823 + case DT_STRSZ:
3824 + dyn.d_un.d_val = size;
3825 + elf_swap_dyn_out (dynobj, & dyn, dyncon);
3826 + break;
3827 + case DT_NEEDED:
3828 + case DT_SONAME:
3829 + case DT_RPATH:
3830 + case DT_RUNPATH:
3831 + case DT_FILTER:
3832 + case DT_AUXILIARY:
3833 + dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3834 + elf_swap_dyn_out (dynobj, & dyn, dyncon);
3835 + break;
3836 + default:
3837 + break;
3838 + }
3839 + }
3840 +
3841 + /* Now update local dynamic symbols. */
3842 + for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3843 + entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3844 + entry->isym.st_name);
3845 +
3846 + /* And the rest of dynamic symbols. */
3847 + elf_link_hash_traverse (elf_hash_table (info),
3848 + elf_adjust_dynstr_offsets, dynstr);
3849 +
3850 + /* Adjust version definitions. */
3851 + if (elf_tdata (output_bfd)->cverdefs)
3852 + {
3853 + asection *s;
3854 + bfd_byte *p;
3855 + bfd_size_type i;
3856 + Elf_Internal_Verdef def;
3857 + Elf_Internal_Verdaux defaux;
3858 +
3859 + s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3860 + p = (bfd_byte *) s->contents;
3861 + do
3862 + {
3863 + _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3864 + &def);
3865 + p += sizeof (Elf_External_Verdef);
3866 + for (i = 0; i < def.vd_cnt; ++i)
3867 + {
3868 + _bfd_elf_swap_verdaux_in (output_bfd,
3869 + (Elf_External_Verdaux *) p, &defaux);
3870 + defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3871 + defaux.vda_name);
3872 + _bfd_elf_swap_verdaux_out (output_bfd,
3873 + &defaux, (Elf_External_Verdaux *) p);
3874 + p += sizeof (Elf_External_Verdaux);
3875 + }
3876 + }
3877 + while (def.vd_next);
3878 + }
3879 +
3880 + /* Adjust version references. */
3881 + if (elf_tdata (output_bfd)->verref)
3882 + {
3883 + asection *s;
3884 + bfd_byte *p;
3885 + bfd_size_type i;
3886 + Elf_Internal_Verneed need;
3887 + Elf_Internal_Vernaux needaux;
3888 +
3889 + s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3890 + p = (bfd_byte *) s->contents;
3891 + do
3892 + {
3893 + _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3894 + &need);
3895 + need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3896 + _bfd_elf_swap_verneed_out (output_bfd, &need,
3897 + (Elf_External_Verneed *) p);
3898 + p += sizeof (Elf_External_Verneed);
3899 + for (i = 0; i < need.vn_cnt; ++i)
3900 + {
3901 + _bfd_elf_swap_vernaux_in (output_bfd,
3902 + (Elf_External_Vernaux *) p, &needaux);
3903 + needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3904 + needaux.vna_name);
3905 + _bfd_elf_swap_vernaux_out (output_bfd,
3906 + &needaux,
3907 + (Elf_External_Vernaux *) p);
3908 + p += sizeof (Elf_External_Vernaux);
3909 + }
3910 + }
3911 + while (need.vn_next);
3912 + }
3913 +
3914 + return true;
3915 +}
3916 +
3917 +/* Fix up the flags for a symbol. This handles various cases which
3918 + can only be fixed after all the input files are seen. This is
3919 + currently called by both adjust_dynamic_symbol and
3920 + assign_sym_version, which is unnecessary but perhaps more robust in
3921 + the face of future changes. */
3922 +
3923 +static boolean
3924 +elf_fix_symbol_flags (h, eif)
3925 + struct elf_link_hash_entry *h;
3926 + struct elf_info_failed *eif;
3927 +{
3928 + /* If this symbol was mentioned in a non-ELF file, try to set
3929 + DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3930 + permit a non-ELF file to correctly refer to a symbol defined in
3931 + an ELF dynamic object. */
3932 + if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3933 + {
3934 + while (h->root.type == bfd_link_hash_indirect)
3935 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
3936 +
3937 + if (h->root.type != bfd_link_hash_defined
3938 + && h->root.type != bfd_link_hash_defweak)
3939 + h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3940 + | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3941 + else
3942 + {
3943 + if (h->root.u.def.section->owner != NULL
3944 + && (bfd_get_flavour (h->root.u.def.section->owner)
3945 + == bfd_target_elf_flavour))
3946 + h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3947 + | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3948 + else
3949 + h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3950 + }
3951 +
3952 + if (h->dynindx == -1
3953 + && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3954 + || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3955 + {
3956 + if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3957 + {
3958 + eif->failed = true;
3959 + return false;
3960 + }
3961 + }
3962 + }
3963 + else
3964 + {
3965 + /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3966 + was first seen in a non-ELF file. Fortunately, if the symbol
3967 + was first seen in an ELF file, we're probably OK unless the
3968 + symbol was defined in a non-ELF file. Catch that case here.
3969 + FIXME: We're still in trouble if the symbol was first seen in
3970 + a dynamic object, and then later in a non-ELF regular object. */
3971 + if ((h->root.type == bfd_link_hash_defined
3972 + || h->root.type == bfd_link_hash_defweak)
3973 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3974 + && (h->root.u.def.section->owner != NULL
3975 + ? (bfd_get_flavour (h->root.u.def.section->owner)
3976 + != bfd_target_elf_flavour)
3977 + : (bfd_is_abs_section (h->root.u.def.section)
3978 + && (h->elf_link_hash_flags
3979 + & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3980 + h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3981 + }
3982 +
3983 + /* If this is a final link, and the symbol was defined as a common
3984 + symbol in a regular object file, and there was no definition in
3985 + any dynamic object, then the linker will have allocated space for
3986 + the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3987 + flag will not have been set. */
3988 + if (h->root.type == bfd_link_hash_defined
3989 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3990 + && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3991 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3992 + && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3993 + h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3994 +
3995 + /* If -Bsymbolic was used (which means to bind references to global
3996 + symbols to the definition within the shared object), and this
3997 + symbol was defined in a regular object, then it actually doesn't
3998 + need a PLT entry, and we can accomplish that by forcing it local.
3999 + Likewise, if the symbol has hidden or internal visibility.
4000 + FIXME: It might be that we also do not need a PLT for other
4001 + non-hidden visibilities, but we would have to tell that to the
4002 + backend specifically; we can't just clear PLT-related data here. */
4003 + if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
4004 + && eif->info->shared
4005 + && is_elf_hash_table (eif->info)
4006 + && (eif->info->symbolic
4007 + || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4008 + || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
4009 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
4010 + {
4011 + struct elf_backend_data *bed;
4012 + boolean force_local;
4013 +
4014 + bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
4015 +
4016 + force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4017 + || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
4018 + (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
4019 + }
4020 +
4021 + /* If this is a weak defined symbol in a dynamic object, and we know
4022 + the real definition in the dynamic object, copy interesting flags
4023 + over to the real definition. */
4024 + if (h->weakdef != NULL)
4025 + {
4026 + struct elf_link_hash_entry *weakdef;
4027 +
4028 + weakdef = h->weakdef;
4029 + if (h->root.type == bfd_link_hash_indirect)
4030 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
4031 +
4032 + BFD_ASSERT (h->root.type == bfd_link_hash_defined
4033 + || h->root.type == bfd_link_hash_defweak);
4034 + BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
4035 + || weakdef->root.type == bfd_link_hash_defweak);
4036 + BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
4037 +
4038 + /* If the real definition is defined by a regular object file,
4039 + don't do anything special. See the longer description in
4040 + elf_adjust_dynamic_symbol, below. */
4041 + if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
4042 + h->weakdef = NULL;
4043 + else
4044 + {
4045 + struct elf_backend_data *bed;
4046 +
4047 + bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
4048 + (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
4049 + }
4050 + }
4051 +
4052 + return true;
4053 +}
4054 +
4055 +/* Make the backend pick a good value for a dynamic symbol. This is
4056 + called via elf_link_hash_traverse, and also calls itself
4057 + recursively. */
4058 +
4059 +static boolean
4060 +elf_adjust_dynamic_symbol (h, data)
4061 + struct elf_link_hash_entry *h;
4062 + PTR data;
4063 +{
4064 + struct elf_info_failed *eif = (struct elf_info_failed *) data;
4065 + bfd *dynobj;
4066 + struct elf_backend_data *bed;
4067 +
4068 + if (h->root.type == bfd_link_hash_warning)
4069 + {
4070 + h->plt.offset = (bfd_vma) -1;
4071 + h->got.offset = (bfd_vma) -1;
4072 +
4073 + /* When warning symbols are created, they **replace** the "real"
4074 + entry in the hash table, thus we never get to see the real
4075 + symbol in a hash traversal. So look at it now. */
4076 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
4077 + }
4078 +
4079 + /* Ignore indirect symbols. These are added by the versioning code. */
4080 + if (h->root.type == bfd_link_hash_indirect)
4081 + return true;
4082 +
4083 + if (! is_elf_hash_table (eif->info))
4084 + return false;
4085 +
4086 + /* Fix the symbol flags. */
4087 + if (! elf_fix_symbol_flags (h, eif))
4088 + return false;
4089 +
4090 + /* If this symbol does not require a PLT entry, and it is not
4091 + defined by a dynamic object, or is not referenced by a regular
4092 + object, ignore it. We do have to handle a weak defined symbol,
4093 + even if no regular object refers to it, if we decided to add it
4094 + to the dynamic symbol table. FIXME: Do we normally need to worry
4095 + about symbols which are defined by one dynamic object and
4096 + referenced by another one? */
4097 + if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
4098 + && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4099 + || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4100 + || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4101 + && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
4102 + {
4103 + h->plt.offset = (bfd_vma) -1;
4104 + return true;
4105 + }
4106 +
4107 + /* If we've already adjusted this symbol, don't do it again. This
4108 + can happen via a recursive call. */
4109 + if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
4110 + return true;
4111 +
4112 + /* Don't look at this symbol again. Note that we must set this
4113 + after checking the above conditions, because we may look at a
4114 + symbol once, decide not to do anything, and then get called
4115 + recursively later after REF_REGULAR is set below. */
4116 + h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
4117 +
4118 + /* If this is a weak definition, and we know a real definition, and
4119 + the real symbol is not itself defined by a regular object file,
4120 + then get a good value for the real definition. We handle the
4121 + real symbol first, for the convenience of the backend routine.
4122 +
4123 + Note that there is a confusing case here. If the real definition
4124 + is defined by a regular object file, we don't get the real symbol
4125 + from the dynamic object, but we do get the weak symbol. If the
4126 + processor backend uses a COPY reloc, then if some routine in the
4127 + dynamic object changes the real symbol, we will not see that
4128 + change in the corresponding weak symbol. This is the way other
4129 + ELF linkers work as well, and seems to be a result of the shared
4130 + library model.
4131 +
4132 + I will clarify this issue. Most SVR4 shared libraries define the
4133 + variable _timezone and define timezone as a weak synonym. The
4134 + tzset call changes _timezone. If you write
4135 + extern int timezone;
4136 + int _timezone = 5;
4137 + int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4138 + you might expect that, since timezone is a synonym for _timezone,
4139 + the same number will print both times. However, if the processor
4140 + backend uses a COPY reloc, then actually timezone will be copied
4141 + into your process image, and, since you define _timezone
4142 + yourself, _timezone will not. Thus timezone and _timezone will
4143 + wind up at different memory locations. The tzset call will set
4144 + _timezone, leaving timezone unchanged. */
4145 +
4146 + if (h->weakdef != NULL)
4147 + {
4148 + /* If we get to this point, we know there is an implicit
4149 + reference by a regular object file via the weak symbol H.
4150 + FIXME: Is this really true? What if the traversal finds
4151 + H->WEAKDEF before it finds H? */
4152 + h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4153 +
4154 + if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4155 + return false;
4156 + }
4157 +
4158 + /* If a symbol has no type and no size and does not require a PLT
4159 + entry, then we are probably about to do the wrong thing here: we
4160 + are probably going to create a COPY reloc for an empty object.
4161 + This case can arise when a shared object is built with assembly
4162 + code, and the assembly code fails to set the symbol type. */
4163 + if (h->size == 0
4164 + && h->type == STT_NOTYPE
4165 + && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4166 + (*_bfd_error_handler)
4167 + (_("warning: type and size of dynamic symbol `%s' are not defined"),
4168 + h->root.root.string);
4169 +
4170 + dynobj = elf_hash_table (eif->info)->dynobj;
4171 + bed = get_elf_backend_data (dynobj);
4172 + if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4173 + {
4174 + eif->failed = true;
4175 + return false;
4176 + }
4177 +
4178 + return true;
4179 +}
4180 +
4181 +/* This routine is used to export all defined symbols into the dynamic
4182 + symbol table. It is called via elf_link_hash_traverse. */
4183 +
4184 +static boolean
4185 +elf_export_symbol (h, data)
4186 + struct elf_link_hash_entry *h;
4187 + PTR data;
4188 +{
4189 + struct elf_info_failed *eif = (struct elf_info_failed *) data;
4190 +
4191 + /* Ignore indirect symbols. These are added by the versioning code. */
4192 + if (h->root.type == bfd_link_hash_indirect)
4193 + return true;
4194 +
4195 + if (h->root.type == bfd_link_hash_warning)
4196 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
4197 +
4198 + if (h->dynindx == -1
4199 + && (h->elf_link_hash_flags
4200 + & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4201 + {
4202 + struct bfd_elf_version_tree *t;
4203 + struct bfd_elf_version_expr *d;
4204 +
4205 + for (t = eif->verdefs; t != NULL; t = t->next)
4206 + {
4207 + if (t->globals != NULL)
4208 + {
4209 + for (d = t->globals; d != NULL; d = d->next)
4210 + {
4211 + if ((*d->match) (d, h->root.root.string))
4212 + goto doit;
4213 + }
4214 + }
4215 +
4216 + if (t->locals != NULL)
4217 + {
4218 + for (d = t->locals ; d != NULL; d = d->next)
4219 + {
4220 + if ((*d->match) (d, h->root.root.string))
4221 + return true;
4222 + }
4223 + }
4224 + }
4225 +
4226 + if (!eif->verdefs)
4227 + {
4228 + doit:
4229 + if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4230 + {
4231 + eif->failed = true;
4232 + return false;
4233 + }
4234 + }
4235 + }
4236 +
4237 + return true;
4238 +}
4239 +
4240 +/* Look through the symbols which are defined in other shared
4241 + libraries and referenced here. Update the list of version
4242 + dependencies. This will be put into the .gnu.version_r section.
4243 + This function is called via elf_link_hash_traverse. */
4244 +
4245 +static boolean
4246 +elf_link_find_version_dependencies (h, data)
4247 + struct elf_link_hash_entry *h;
4248 + PTR data;
4249 +{
4250 + struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4251 + Elf_Internal_Verneed *t;
4252 + Elf_Internal_Vernaux *a;
4253 + bfd_size_type amt;
4254 +
4255 + if (h->root.type == bfd_link_hash_warning)
4256 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
4257 +
4258 + /* We only care about symbols defined in shared objects with version
4259 + information. */
4260 + if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4261 + || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4262 + || h->dynindx == -1
4263 + || h->verinfo.verdef == NULL)
4264 + return true;
4265 +
4266 + /* See if we already know about this version. */
4267 + for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4268 + {
4269 + if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4270 + continue;
4271 +
4272 + for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4273 + if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4274 + return true;
4275 +
4276 + break;
4277 + }
4278 +
4279 + /* This is a new version. Add it to tree we are building. */
4280 +
4281 + if (t == NULL)
4282 + {
4283 + amt = sizeof *t;
4284 + t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4285 + if (t == NULL)
4286 + {
4287 + rinfo->failed = true;
4288 + return false;
4289 + }
4290 +
4291 + t->vn_bfd = h->verinfo.verdef->vd_bfd;
4292 + t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4293 + elf_tdata (rinfo->output_bfd)->verref = t;
4294 + }
4295 +
4296 + amt = sizeof *a;
4297 + a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4298 +
4299 + /* Note that we are copying a string pointer here, and testing it
4300 + above. If bfd_elf_string_from_elf_section is ever changed to
4301 + discard the string data when low in memory, this will have to be
4302 + fixed. */
4303 + a->vna_nodename = h->verinfo.verdef->vd_nodename;
4304 +
4305 + a->vna_flags = h->verinfo.verdef->vd_flags;
4306 + a->vna_nextptr = t->vn_auxptr;
4307 +
4308 + h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4309 + ++rinfo->vers;
4310 +
4311 + a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4312 +
4313 + t->vn_auxptr = a;
4314 +
4315 + return true;
4316 +}
4317 +
4318 +/* Figure out appropriate versions for all the symbols. We may not
4319 + have the version number script until we have read all of the input
4320 + files, so until that point we don't know which symbols should be
4321 + local. This function is called via elf_link_hash_traverse. */
4322 +
4323 +static boolean
4324 +elf_link_assign_sym_version (h, data)
4325 + struct elf_link_hash_entry *h;
4326 + PTR data;
4327 +{
4328 + struct elf_assign_sym_version_info *sinfo;
4329 + struct bfd_link_info *info;
4330 + struct elf_backend_data *bed;
4331 + struct elf_info_failed eif;
4332 + char *p;
4333 + bfd_size_type amt;
4334 +
4335 + sinfo = (struct elf_assign_sym_version_info *) data;
4336 + info = sinfo->info;
4337 +
4338 + if (h->root.type == bfd_link_hash_warning)
4339 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
4340 +
4341 + /* Fix the symbol flags. */
4342 + eif.failed = false;
4343 + eif.info = info;
4344 + if (! elf_fix_symbol_flags (h, &eif))
4345 + {
4346 + if (eif.failed)
4347 + sinfo->failed = true;
4348 + return false;
4349 + }
4350 +
4351 + /* We only need version numbers for symbols defined in regular
4352 + objects. */
4353 + if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4354 + return true;
4355 +
4356 + bed = get_elf_backend_data (sinfo->output_bfd);
4357 + p = strchr (h->root.root.string, ELF_VER_CHR);
4358 + if (p != NULL && h->verinfo.vertree == NULL)
4359 + {
4360 + struct bfd_elf_version_tree *t;
4361 + boolean hidden;
4362 +
4363 + hidden = true;
4364 +
4365 + /* There are two consecutive ELF_VER_CHR characters if this is
4366 + not a hidden symbol. */
4367 + ++p;
4368 + if (*p == ELF_VER_CHR)
4369 + {
4370 + hidden = false;
4371 + ++p;
4372 + }
4373 +
4374 + /* If there is no version string, we can just return out. */
4375 + if (*p == '\0')
4376 + {
4377 + if (hidden)
4378 + h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4379 + return true;
4380 + }
4381 +
4382 + /* Look for the version. If we find it, it is no longer weak. */
4383 + for (t = sinfo->verdefs; t != NULL; t = t->next)
4384 + {
4385 + if (strcmp (t->name, p) == 0)
4386 + {
4387 + size_t len;
4388 + char *alc;
4389 + struct bfd_elf_version_expr *d;
4390 +
4391 + len = p - h->root.root.string;
4392 + alc = bfd_malloc ((bfd_size_type) len);
4393 + if (alc == NULL)
4394 + return false;
4395 + memcpy (alc, h->root.root.string, len - 1);
4396 + alc[len - 1] = '\0';
4397 + if (alc[len - 2] == ELF_VER_CHR)
4398 + alc[len - 2] = '\0';
4399 +
4400 + h->verinfo.vertree = t;
4401 + t->used = true;
4402 + d = NULL;
4403 +
4404 + if (t->globals != NULL)
4405 + {
4406 + for (d = t->globals; d != NULL; d = d->next)
4407 + if ((*d->match) (d, alc))
4408 + break;
4409 + }
4410 +
4411 + /* See if there is anything to force this symbol to
4412 + local scope. */
4413 + if (d == NULL && t->locals != NULL)
4414 + {
4415 + for (d = t->locals; d != NULL; d = d->next)
4416 + {
4417 + if ((*d->match) (d, alc))
4418 + {
4419 + if (h->dynindx != -1
4420 + && info->shared
4421 + && ! info->export_dynamic)
4422 + {
4423 + (*bed->elf_backend_hide_symbol) (info, h, true);
4424 + }
4425 +
4426 + break;
4427 + }
4428 + }
4429 + }
4430 +
4431 + free (alc);
4432 + break;
4433 + }
4434 + }
4435 +
4436 + /* If we are building an application, we need to create a
4437 + version node for this version. */
4438 + if (t == NULL && ! info->shared)
4439 + {
4440 + struct bfd_elf_version_tree **pp;
4441 + int version_index;
4442 +
4443 + /* If we aren't going to export this symbol, we don't need
4444 + to worry about it. */
4445 + if (h->dynindx == -1)
4446 + return true;
4447 +
4448 + amt = sizeof *t;
4449 + t = ((struct bfd_elf_version_tree *)
4450 + bfd_alloc (sinfo->output_bfd, amt));
4451 + if (t == NULL)
4452 + {
4453 + sinfo->failed = true;
4454 + return false;
4455 + }
4456 +
4457 + t->next = NULL;
4458 + t->name = p;
4459 + t->globals = NULL;
4460 + t->locals = NULL;
4461 + t->deps = NULL;
4462 + t->name_indx = (unsigned int) -1;
4463 + t->used = true;
4464 +
4465 + version_index = 1;
4466 + /* Don't count anonymous version tag. */
4467 + if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4468 + version_index = 0;
4469 + for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4470 + ++version_index;
4471 + t->vernum = version_index;
4472 +
4473 + *pp = t;
4474 +
4475 + h->verinfo.vertree = t;
4476 + }
4477 + else if (t == NULL)
4478 + {
4479 + /* We could not find the version for a symbol when
4480 + generating a shared archive. Return an error. */
4481 + (*_bfd_error_handler)
4482 + (_("%s: undefined versioned symbol name %s"),
4483 + bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4484 + bfd_set_error (bfd_error_bad_value);
4485 + sinfo->failed = true;
4486 + return false;
4487 + }
4488 +
4489 + if (hidden)
4490 + h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4491 + }
4492 +
4493 + /* If we don't have a version for this symbol, see if we can find
4494 + something. */
4495 + if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4496 + {
4497 + struct bfd_elf_version_tree *t;
4498 + struct bfd_elf_version_tree *local_ver;
4499 + struct bfd_elf_version_expr *d;
4500 +
4501 + /* See if can find what version this symbol is in. If the
4502 + symbol is supposed to be local, then don't actually register
4503 + it. */
4504 + local_ver = NULL;
4505 + for (t = sinfo->verdefs; t != NULL; t = t->next)
4506 + {
4507 + if (t->globals != NULL)
4508 + {
4509 + boolean matched;
4510 +
4511 + matched = false;
4512 + for (d = t->globals; d != NULL; d = d->next)
4513 + {
4514 + if ((*d->match) (d, h->root.root.string))
4515 + {
4516 + if (d->symver)
4517 + matched = true;
4518 + else
4519 + {
4520 + /* There is a version without definition. Make
4521 + the symbol the default definition for this
4522 + version. */
4523 + h->verinfo.vertree = t;
4524 + local_ver = NULL;
4525 + d->script = 1;
4526 + break;
4527 + }
4528 + }
4529 + }
4530 +
4531 + if (d != NULL)
4532 + break;
4533 + else if (matched)
4534 + /* There is no undefined version for this symbol. Hide the
4535 + default one. */
4536 + (*bed->elf_backend_hide_symbol) (info, h, true);
4537 + }
4538 +
4539 + if (t->locals != NULL)
4540 + {
4541 + for (d = t->locals; d != NULL; d = d->next)
4542 + {
4543 + /* If the match is "*", keep looking for a more
4544 + explicit, perhaps even global, match. */
4545 + if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4546 + local_ver = t;
4547 + else if ((*d->match) (d, h->root.root.string))
4548 + {
4549 + local_ver = t;
4550 + break;
4551 + }
4552 + }
4553 +
4554 + if (d != NULL)
4555 + break;
4556 + }
4557 + }
4558 +
4559 + if (local_ver != NULL)
4560 + {
4561 + h->verinfo.vertree = local_ver;
4562 + if (h->dynindx != -1
4563 + && info->shared
4564 + && ! info->export_dynamic)
4565 + {
4566 + (*bed->elf_backend_hide_symbol) (info, h, true);
4567 + }
4568 + }
4569 + }
4570 +
4571 + return true;
4572 +}
4573 +
4574 +/* Final phase of ELF linker. */
4575 +
4576 +/* A structure we use to avoid passing large numbers of arguments. */
4577 +
4578 +struct elf_final_link_info
4579 +{
4580 + /* General link information. */
4581 + struct bfd_link_info *info;
4582 + /* Output BFD. */
4583 + bfd *output_bfd;
4584 + /* Symbol string table. */
4585 + struct bfd_strtab_hash *symstrtab;
4586 + /* .dynsym section. */
4587 + asection *dynsym_sec;
4588 + /* .hash section. */
4589 + asection *hash_sec;
4590 + /* symbol version section (.gnu.version). */
4591 + asection *symver_sec;
4592 + /* first SHF_TLS section (if any). */
4593 + asection *first_tls_sec;
4594 + /* Buffer large enough to hold contents of any section. */
4595 + bfd_byte *contents;
4596 + /* Buffer large enough to hold external relocs of any section. */
4597 + PTR external_relocs;
4598 + /* Buffer large enough to hold internal relocs of any section. */
4599 + Elf_Internal_Rela *internal_relocs;
4600 + /* Buffer large enough to hold external local symbols of any input
4601 + BFD. */
4602 + Elf_External_Sym *external_syms;
4603 + /* And a buffer for symbol section indices. */
4604 + Elf_External_Sym_Shndx *locsym_shndx;
4605 + /* Buffer large enough to hold internal local symbols of any input
4606 + BFD. */
4607 + Elf_Internal_Sym *internal_syms;
4608 + /* Array large enough to hold a symbol index for each local symbol
4609 + of any input BFD. */
4610 + long *indices;
4611 + /* Array large enough to hold a section pointer for each local
4612 + symbol of any input BFD. */
4613 + asection **sections;
4614 + /* Buffer to hold swapped out symbols. */
4615 + Elf_External_Sym *symbuf;
4616 + /* And one for symbol section indices. */
4617 + Elf_External_Sym_Shndx *symshndxbuf;
4618 + /* Number of swapped out symbols in buffer. */
4619 + size_t symbuf_count;
4620 + /* Number of symbols which fit in symbuf. */
4621 + size_t symbuf_size;
4622 +};
4623 +
4624 +static boolean elf_link_output_sym
4625 + PARAMS ((struct elf_final_link_info *, const char *,
4626 + Elf_Internal_Sym *, asection *));
4627 +static boolean elf_link_flush_output_syms
4628 + PARAMS ((struct elf_final_link_info *));
4629 +static boolean elf_link_output_extsym
4630 + PARAMS ((struct elf_link_hash_entry *, PTR));
4631 +static boolean elf_link_sec_merge_syms
4632 + PARAMS ((struct elf_link_hash_entry *, PTR));
4633 +static boolean elf_link_check_versioned_symbol
4634 + PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
4635 +static boolean elf_link_input_bfd
4636 + PARAMS ((struct elf_final_link_info *, bfd *));
4637 +static boolean elf_reloc_link_order
4638 + PARAMS ((bfd *, struct bfd_link_info *, asection *,
4639 + struct bfd_link_order *));
4640 +
4641 +/* This struct is used to pass information to elf_link_output_extsym. */
4642 +
4643 +struct elf_outext_info
4644 +{
4645 + boolean failed;
4646 + boolean localsyms;
4647 + struct elf_final_link_info *finfo;
4648 +};
4649 +
4650 +/* Compute the size of, and allocate space for, REL_HDR which is the
4651 + section header for a section containing relocations for O. */
4652 +
4653 +static boolean
4654 +elf_link_size_reloc_section (abfd, rel_hdr, o)
4655 + bfd *abfd;
4656 + Elf_Internal_Shdr *rel_hdr;
4657 + asection *o;
4658 +{
4659 + bfd_size_type reloc_count;
4660 + bfd_size_type num_rel_hashes;
4661 +
4662 + /* Figure out how many relocations there will be. */
4663 + if (rel_hdr == &elf_section_data (o)->rel_hdr)
4664 + reloc_count = elf_section_data (o)->rel_count;
4665 + else
4666 + reloc_count = elf_section_data (o)->rel_count2;
4667 +
4668 + num_rel_hashes = o->reloc_count;
4669 + if (num_rel_hashes < reloc_count)
4670 + num_rel_hashes = reloc_count;
4671 +
4672 + /* That allows us to calculate the size of the section. */
4673 + rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4674 +
4675 + /* The contents field must last into write_object_contents, so we
4676 + allocate it with bfd_alloc rather than malloc. Also since we
4677 + cannot be sure that the contents will actually be filled in,
4678 + we zero the allocated space. */
4679 + rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4680 + if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4681 + return false;
4682 +
4683 + /* We only allocate one set of hash entries, so we only do it the
4684 + first time we are called. */
4685 + if (elf_section_data (o)->rel_hashes == NULL
4686 + && num_rel_hashes)
4687 + {
4688 + struct elf_link_hash_entry **p;
4689 +
4690 + p = ((struct elf_link_hash_entry **)
4691 + bfd_zmalloc (num_rel_hashes
4692 + * sizeof (struct elf_link_hash_entry *)));
4693 + if (p == NULL)
4694 + return false;
4695 +
4696 + elf_section_data (o)->rel_hashes = p;
4697 + }
4698 +
4699 + return true;
4700 +}
4701 +
4702 +/* When performing a relocateable link, the input relocations are
4703 + preserved. But, if they reference global symbols, the indices
4704 + referenced must be updated. Update all the relocations in
4705 + REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4706 +
4707 +static void
4708 +elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4709 + bfd *abfd;
4710 + Elf_Internal_Shdr *rel_hdr;
4711 + unsigned int count;
4712 + struct elf_link_hash_entry **rel_hash;
4713 +{
4714 + unsigned int i;
4715 + struct elf_backend_data *bed = get_elf_backend_data (abfd);
4716 + Elf_Internal_Rel *irel;
4717 + Elf_Internal_Rela *irela;
4718 + bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4719 +
4720 + irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4721 + if (irel == NULL)
4722 + {
4723 + (*_bfd_error_handler) (_("Error: out of memory"));
4724 + abort ();
4725 + }
4726 +
4727 + amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4728 + irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4729 + if (irela == NULL)
4730 + {
4731 + (*_bfd_error_handler) (_("Error: out of memory"));
4732 + abort ();
4733 + }
4734 +
4735 + for (i = 0; i < count; i++, rel_hash++)
4736 + {
4737 + if (*rel_hash == NULL)
4738 + continue;
4739 +
4740 + BFD_ASSERT ((*rel_hash)->indx >= 0);
4741 +
4742 + if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4743 + {
4744 + Elf_External_Rel *erel;
4745 + unsigned int j;
4746 +
4747 + erel = (Elf_External_Rel *) rel_hdr->contents + i;
4748 + if (bed->s->swap_reloc_in)
4749 + (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4750 + else
4751 + elf_swap_reloc_in (abfd, erel, irel);
4752 +
4753 + for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4754 + irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4755 + ELF_R_TYPE (irel[j].r_info));
4756 +
4757 + if (bed->s->swap_reloc_out)
4758 + (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4759 + else
4760 + elf_swap_reloc_out (abfd, irel, erel);
4761 + }
4762 + else
4763 + {
4764 + Elf_External_Rela *erela;
4765 + unsigned int j;
4766 +
4767 + BFD_ASSERT (rel_hdr->sh_entsize
4768 + == sizeof (Elf_External_Rela));
4769 +
4770 + erela = (Elf_External_Rela *) rel_hdr->contents + i;
4771 + if (bed->s->swap_reloca_in)
4772 + (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4773 + else
4774 + elf_swap_reloca_in (abfd, erela, irela);
4775 +
4776 + for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4777 + irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4778 + ELF_R_TYPE (irela[j].r_info));
4779 +
4780 + if (bed->s->swap_reloca_out)
4781 + (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4782 + else
4783 + elf_swap_reloca_out (abfd, irela, erela);
4784 + }
4785 + }
4786 +
4787 + free (irel);
4788 + free (irela);
4789 +}
4790 +
4791 +struct elf_link_sort_rela
4792 +{
4793 + bfd_vma offset;
4794 + enum elf_reloc_type_class type;
4795 + union
4796 + {
4797 + Elf_Internal_Rel rel;
4798 + Elf_Internal_Rela rela;
4799 + } u;
4800 +};
4801 +
4802 +static int
4803 +elf_link_sort_cmp1 (A, B)
4804 + const PTR A;
4805 + const PTR B;
4806 +{
4807 + struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4808 + struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4809 + int relativea, relativeb;
4810 +
4811 + relativea = a->type == reloc_class_relative;
4812 + relativeb = b->type == reloc_class_relative;
4813 +
4814 + if (relativea < relativeb)
4815 + return 1;
4816 + if (relativea > relativeb)
4817 + return -1;
4818 + if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4819 + return -1;
4820 + if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4821 + return 1;
4822 + if (a->u.rel.r_offset < b->u.rel.r_offset)
4823 + return -1;
4824 + if (a->u.rel.r_offset > b->u.rel.r_offset)
4825 + return 1;
4826 + return 0;
4827 +}
4828 +
4829 +static int
4830 +elf_link_sort_cmp2 (A, B)
4831 + const PTR A;
4832 + const PTR B;
4833 +{
4834 + struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4835 + struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4836 + int copya, copyb;
4837 +
4838 + if (a->offset < b->offset)
4839 + return -1;
4840 + if (a->offset > b->offset)
4841 + return 1;
4842 + copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4843 + copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4844 + if (copya < copyb)
4845 + return -1;
4846 + if (copya > copyb)
4847 + return 1;
4848 + if (a->u.rel.r_offset < b->u.rel.r_offset)
4849 + return -1;
4850 + if (a->u.rel.r_offset > b->u.rel.r_offset)
4851 + return 1;
4852 + return 0;
4853 +}
4854 +
4855 +static size_t
4856 +elf_link_sort_relocs (abfd, info, psec)
4857 + bfd *abfd;
4858 + struct bfd_link_info *info;
4859 + asection **psec;
4860 +{
4861 + bfd *dynobj = elf_hash_table (info)->dynobj;
4862 + asection *reldyn, *o;
4863 + boolean rel = false;
4864 + bfd_size_type count, size;
4865 + size_t i, j, ret;
4866 + struct elf_link_sort_rela *rela;
4867 + struct elf_backend_data *bed = get_elf_backend_data (abfd);
4868 +
4869 + reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4870 + if (reldyn == NULL || reldyn->_raw_size == 0)
4871 + {
4872 + reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4873 + if (reldyn == NULL || reldyn->_raw_size == 0)
4874 + return 0;
4875 + rel = true;
4876 + count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4877 + }
4878 + else
4879 + count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4880 +
4881 + size = 0;
4882 + for (o = dynobj->sections; o != NULL; o = o->next)
4883 + if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4884 + == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4885 + && o->output_section == reldyn)
4886 + size += o->_raw_size;
4887 +
4888 + if (size != reldyn->_raw_size)
4889 + return 0;
4890 +
4891 + rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
4892 + if (rela == NULL)
4893 + {
4894 + (*info->callbacks->warning)
4895 + (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4896 + (bfd_vma) 0);
4897 + return 0;
4898 + }
4899 +
4900 + for (o = dynobj->sections; o != NULL; o = o->next)
4901 + if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4902 + == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4903 + && o->output_section == reldyn)
4904 + {
4905 + if (rel)
4906 + {
4907 + Elf_External_Rel *erel, *erelend;
4908 + struct elf_link_sort_rela *s;
4909 +
4910 + erel = (Elf_External_Rel *) o->contents;
4911 + erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4912 + s = rela + o->output_offset / sizeof (Elf_External_Rel);
4913 + for (; erel < erelend; erel++, s++)
4914 + {
4915 + if (bed->s->swap_reloc_in)
4916 + (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4917 + else
4918 + elf_swap_reloc_in (abfd, erel, &s->u.rel);
4919 +
4920 + s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4921 + }
4922 + }
4923 + else
4924 + {
4925 + Elf_External_Rela *erela, *erelaend;
4926 + struct elf_link_sort_rela *s;
4927 +
4928 + erela = (Elf_External_Rela *) o->contents;
4929 + erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4930 + s = rela + o->output_offset / sizeof (Elf_External_Rela);
4931 + for (; erela < erelaend; erela++, s++)
4932 + {
4933 + if (bed->s->swap_reloca_in)
4934 + (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4935 + &s->u.rela);
4936 + else
4937 + elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4938 +
4939 + s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4940 + }
4941 + }
4942 + }
4943 +
4944 + qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
4945 + for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4946 + ;
4947 + for (i = ret, j = ret; i < count; i++)
4948 + {
4949 + if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4950 + j = i;
4951 + rela[i].offset = rela[j].u.rel.r_offset;
4952 + }
4953 + qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
4954 +
4955 + for (o = dynobj->sections; o != NULL; o = o->next)
4956 + if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4957 + == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4958 + && o->output_section == reldyn)
4959 + {
4960 + if (rel)
4961 + {
4962 + Elf_External_Rel *erel, *erelend;
4963 + struct elf_link_sort_rela *s;
4964 +
4965 + erel = (Elf_External_Rel *) o->contents;
4966 + erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4967 + s = rela + o->output_offset / sizeof (Elf_External_Rel);
4968 + for (; erel < erelend; erel++, s++)
4969 + {
4970 + if (bed->s->swap_reloc_out)
4971 + (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4972 + (bfd_byte *) erel);
4973 + else
4974 + elf_swap_reloc_out (abfd, &s->u.rel, erel);
4975 + }
4976 + }
4977 + else
4978 + {
4979 + Elf_External_Rela *erela, *erelaend;
4980 + struct elf_link_sort_rela *s;
4981 +
4982 + erela = (Elf_External_Rela *) o->contents;
4983 + erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4984 + s = rela + o->output_offset / sizeof (Elf_External_Rela);
4985 + for (; erela < erelaend; erela++, s++)
4986 + {
4987 + if (bed->s->swap_reloca_out)
4988 + (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4989 + (bfd_byte *) erela);
4990 + else
4991 + elf_swap_reloca_out (dynobj, &s->u.rela, erela);
4992 + }
4993 + }
4994 + }
4995 +
4996 + free (rela);
4997 + *psec = reldyn;
4998 + return ret;
4999 +}
5000 +
5001 +/* Do the final step of an ELF link. */
5002 +
5003 +boolean
5004 +elf_bfd_final_link (abfd, info)
5005 + bfd *abfd;
5006 + struct bfd_link_info *info;
5007 +{
5008 + boolean dynamic;
5009 + boolean emit_relocs;
5010 + bfd *dynobj;
5011 + struct elf_final_link_info finfo;
5012 + register asection *o;
5013 + register struct bfd_link_order *p;
5014 + register bfd *sub;
5015 + bfd_size_type max_contents_size;
5016 + bfd_size_type max_external_reloc_size;
5017 + bfd_size_type max_internal_reloc_count;
5018 + bfd_size_type max_sym_count;
5019 + bfd_size_type max_sym_shndx_count;
5020 + file_ptr off;
5021 + Elf_Internal_Sym elfsym;
5022 + unsigned int i;
5023 + Elf_Internal_Shdr *symtab_hdr;
5024 + Elf_Internal_Shdr *symstrtab_hdr;
5025 + struct elf_backend_data *bed = get_elf_backend_data (abfd);
5026 + struct elf_outext_info eoinfo;
5027 + boolean merged;
5028 + size_t relativecount = 0;
5029 + asection *reldyn = 0;
5030 + bfd_size_type amt;
5031 +
5032 + if (! is_elf_hash_table (info))
5033 + return false;
5034 +
5035 + if (info->shared)
5036 + abfd->flags |= DYNAMIC;
5037 +
5038 + dynamic = elf_hash_table (info)->dynamic_sections_created;
5039 + dynobj = elf_hash_table (info)->dynobj;
5040 +
5041 + emit_relocs = (info->relocateable
5042 + || info->emitrelocations
5043 + || bed->elf_backend_emit_relocs);
5044 +
5045 + finfo.info = info;
5046 + finfo.output_bfd = abfd;
5047 + finfo.symstrtab = elf_stringtab_init ();
5048 + if (finfo.symstrtab == NULL)
5049 + return false;
5050 +
5051 + if (! dynamic)
5052 + {
5053 + finfo.dynsym_sec = NULL;
5054 + finfo.hash_sec = NULL;
5055 + finfo.symver_sec = NULL;
5056 + }
5057 + else
5058 + {
5059 + finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
5060 + finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
5061 + BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
5062 + finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
5063 + /* Note that it is OK if symver_sec is NULL. */
5064 + }
5065 +
5066 + finfo.contents = NULL;
5067 + finfo.external_relocs = NULL;
5068 + finfo.internal_relocs = NULL;
5069 + finfo.external_syms = NULL;
5070 + finfo.locsym_shndx = NULL;
5071 + finfo.internal_syms = NULL;
5072 + finfo.indices = NULL;
5073 + finfo.sections = NULL;
5074 + finfo.symbuf = NULL;
5075 + finfo.symshndxbuf = NULL;
5076 + finfo.symbuf_count = 0;
5077 + finfo.first_tls_sec = NULL;
5078 + for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5079 + if ((o->flags & SEC_THREAD_LOCAL) != 0
5080 + && (o->flags & SEC_LOAD) != 0)
5081 + {
5082 + finfo.first_tls_sec = o;
5083 + break;
5084 + }
5085 +
5086 + /* Count up the number of relocations we will output for each output
5087 + section, so that we know the sizes of the reloc sections. We
5088 + also figure out some maximum sizes. */
5089 + max_contents_size = 0;
5090 + max_external_reloc_size = 0;
5091 + max_internal_reloc_count = 0;
5092 + max_sym_count = 0;
5093 + max_sym_shndx_count = 0;
5094 + merged = false;
5095 + for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5096 + {
5097 + o->reloc_count = 0;
5098 +
5099 + for (p = o->link_order_head; p != NULL; p = p->next)
5100 + {
5101 + if (p->type == bfd_section_reloc_link_order
5102 + || p->type == bfd_symbol_reloc_link_order)
5103 + ++o->reloc_count;
5104 + else if (p->type == bfd_indirect_link_order)
5105 + {
5106 + asection *sec;
5107 +
5108 + sec = p->u.indirect.section;
5109 +
5110 + /* Mark all sections which are to be included in the
5111 + link. This will normally be every section. We need
5112 + to do this so that we can identify any sections which
5113 + the linker has decided to not include. */
5114 + sec->linker_mark = true;
5115 +
5116 + if (sec->flags & SEC_MERGE)
5117 + merged = true;
5118 +
5119 + if (info->relocateable || info->emitrelocations)
5120 + o->reloc_count += sec->reloc_count;
5121 + else if (bed->elf_backend_count_relocs)
5122 + {
5123 + Elf_Internal_Rela * relocs;
5124 +
5125 + relocs = (NAME(_bfd_elf,link_read_relocs)
5126 + (abfd, sec, (PTR) NULL,
5127 + (Elf_Internal_Rela *) NULL, info->keep_memory));
5128 +
5129 + o->reloc_count
5130 + += (*bed->elf_backend_count_relocs) (sec, relocs);
5131 +
5132 + if (elf_section_data (o)->relocs != relocs)
5133 + free (relocs);
5134 + }
5135 +
5136 + if (sec->_raw_size > max_contents_size)
5137 + max_contents_size = sec->_raw_size;
5138 + if (sec->_cooked_size > max_contents_size)
5139 + max_contents_size = sec->_cooked_size;
5140 +
5141 + /* We are interested in just local symbols, not all
5142 + symbols. */
5143 + if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5144 + && (sec->owner->flags & DYNAMIC) == 0)
5145 + {
5146 + size_t sym_count;
5147 +
5148 + if (elf_bad_symtab (sec->owner))
5149 + sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
5150 + / sizeof (Elf_External_Sym));
5151 + else
5152 + sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5153 +
5154 + if (sym_count > max_sym_count)
5155 + max_sym_count = sym_count;
5156 +
5157 + if (sym_count > max_sym_shndx_count
5158 + && elf_symtab_shndx (sec->owner) != 0)
5159 + max_sym_shndx_count = sym_count;
5160 +
5161 + if ((sec->flags & SEC_RELOC) != 0)
5162 + {
5163 + size_t ext_size;
5164 +
5165 + ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5166 + if (ext_size > max_external_reloc_size)
5167 + max_external_reloc_size = ext_size;
5168 + if (sec->reloc_count > max_internal_reloc_count)
5169 + max_internal_reloc_count = sec->reloc_count;
5170 + }
5171 + }
5172 + }
5173 + }
5174 +
5175 + if (o->reloc_count > 0)
5176 + o->flags |= SEC_RELOC;
5177 + else
5178 + {
5179 + /* Explicitly clear the SEC_RELOC flag. The linker tends to
5180 + set it (this is probably a bug) and if it is set
5181 + assign_section_numbers will create a reloc section. */
5182 + o->flags &=~ SEC_RELOC;
5183 + }
5184 +
5185 + /* If the SEC_ALLOC flag is not set, force the section VMA to
5186 + zero. This is done in elf_fake_sections as well, but forcing
5187 + the VMA to 0 here will ensure that relocs against these
5188 + sections are handled correctly. */
5189 + if ((o->flags & SEC_ALLOC) == 0
5190 + && ! o->user_set_vma)
5191 + o->vma = 0;
5192 + }
5193 +
5194 + if (! info->relocateable && merged)
5195 + elf_link_hash_traverse (elf_hash_table (info),
5196 + elf_link_sec_merge_syms, (PTR) abfd);
5197 +
5198 + /* Figure out the file positions for everything but the symbol table
5199 + and the relocs. We set symcount to force assign_section_numbers
5200 + to create a symbol table. */
5201 + bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5202 + BFD_ASSERT (! abfd->output_has_begun);
5203 + if (! _bfd_elf_compute_section_file_positions (abfd, info))
5204 + goto error_return;
5205 +
5206 + /* Figure out how many relocations we will have in each section.
5207 + Just using RELOC_COUNT isn't good enough since that doesn't
5208 + maintain a separate value for REL vs. RELA relocations. */
5209 + if (emit_relocs)
5210 + for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5211 + for (o = sub->sections; o != NULL; o = o->next)
5212 + {
5213 + asection *output_section;
5214 +
5215 + if (! o->linker_mark)
5216 + {
5217 + /* This section was omitted from the link. */
5218 + continue;
5219 + }
5220 +
5221 + output_section = o->output_section;
5222 +
5223 + if (output_section != NULL
5224 + && (o->flags & SEC_RELOC) != 0)
5225 + {
5226 + struct bfd_elf_section_data *esdi
5227 + = elf_section_data (o);
5228 + struct bfd_elf_section_data *esdo
5229 + = elf_section_data (output_section);
5230 + unsigned int *rel_count;
5231 + unsigned int *rel_count2;
5232 + bfd_size_type entsize;
5233 + bfd_size_type entsize2;
5234 +
5235 + /* We must be careful to add the relocations from the
5236 + input section to the right output count. */
5237 + entsize = esdi->rel_hdr.sh_entsize;
5238 + entsize2 = esdi->rel_hdr2 ? esdi->rel_hdr2->sh_entsize : 0;
5239 + BFD_ASSERT ((entsize == sizeof (Elf_External_Rel)
5240 + || entsize == sizeof (Elf_External_Rela))
5241 + && entsize2 != entsize
5242 + && (entsize2 == 0
5243 + || entsize2 == sizeof (Elf_External_Rel)
5244 + || entsize2 == sizeof (Elf_External_Rela)));
5245 + if (entsize == esdo->rel_hdr.sh_entsize)
5246 + {
5247 + rel_count = &esdo->rel_count;
5248 + rel_count2 = &esdo->rel_count2;
5249 + }
5250 + else
5251 + {
5252 + rel_count = &esdo->rel_count2;
5253 + rel_count2 = &esdo->rel_count;
5254 + }
5255 +
5256 + *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
5257 + if (esdi->rel_hdr2)
5258 + *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
5259 + output_section->flags |= SEC_RELOC;
5260 + }
5261 + }
5262 +
5263 + /* That created the reloc sections. Set their sizes, and assign
5264 + them file positions, and allocate some buffers. */
5265 + for (o = abfd->sections; o != NULL; o = o->next)
5266 + {
5267 + if ((o->flags & SEC_RELOC) != 0)
5268 + {
5269 + if (!elf_link_size_reloc_section (abfd,
5270 + &elf_section_data (o)->rel_hdr,
5271 + o))
5272 + goto error_return;
5273 +
5274 + if (elf_section_data (o)->rel_hdr2
5275 + && !elf_link_size_reloc_section (abfd,
5276 + elf_section_data (o)->rel_hdr2,
5277 + o))
5278 + goto error_return;
5279 + }
5280 +
5281 + /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5282 + to count upwards while actually outputting the relocations. */
5283 + elf_section_data (o)->rel_count = 0;
5284 + elf_section_data (o)->rel_count2 = 0;
5285 + }
5286 +
5287 + _bfd_elf_assign_file_positions_for_relocs (abfd);
5288 +
5289 + /* We have now assigned file positions for all the sections except
5290 + .symtab and .strtab. We start the .symtab section at the current
5291 + file position, and write directly to it. We build the .strtab
5292 + section in memory. */
5293 + bfd_get_symcount (abfd) = 0;
5294 + symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5295 + /* sh_name is set in prep_headers. */
5296 + symtab_hdr->sh_type = SHT_SYMTAB;
5297 + symtab_hdr->sh_flags = 0;
5298 + symtab_hdr->sh_addr = 0;
5299 + symtab_hdr->sh_size = 0;
5300 + symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5301 + /* sh_link is set in assign_section_numbers. */
5302 + /* sh_info is set below. */
5303 + /* sh_offset is set just below. */
5304 + symtab_hdr->sh_addralign = bed->s->file_align;
5305 +
5306 + off = elf_tdata (abfd)->next_file_pos;
5307 + off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5308 +
5309 + /* Note that at this point elf_tdata (abfd)->next_file_pos is
5310 + incorrect. We do not yet know the size of the .symtab section.
5311 + We correct next_file_pos below, after we do know the size. */
5312 +
5313 + /* Allocate a buffer to hold swapped out symbols. This is to avoid
5314 + continuously seeking to the right position in the file. */
5315 + if (! info->keep_memory || max_sym_count < 20)
5316 + finfo.symbuf_size = 20;
5317 + else
5318 + finfo.symbuf_size = max_sym_count;
5319 + amt = finfo.symbuf_size;
5320 + amt *= sizeof (Elf_External_Sym);
5321 + finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
5322 + if (finfo.symbuf == NULL)
5323 + goto error_return;
5324 + if (elf_numsections (abfd) > SHN_LORESERVE)
5325 + {
5326 + amt = finfo.symbuf_size;
5327 + amt *= sizeof (Elf_External_Sym_Shndx);
5328 + finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5329 + if (finfo.symshndxbuf == NULL)
5330 + goto error_return;
5331 + }
5332 +
5333 + /* Start writing out the symbol table. The first symbol is always a
5334 + dummy symbol. */
5335 + if (info->strip != strip_all
5336 + || emit_relocs)
5337 + {
5338 + elfsym.st_value = 0;
5339 + elfsym.st_size = 0;
5340 + elfsym.st_info = 0;
5341 + elfsym.st_other = 0;
5342 + elfsym.st_shndx = SHN_UNDEF;
5343 + if (! elf_link_output_sym (&finfo, (const char *) NULL,
5344 + &elfsym, bfd_und_section_ptr))
5345 + goto error_return;
5346 + }
5347 +
5348 +#if 0
5349 + /* Some standard ELF linkers do this, but we don't because it causes
5350 + bootstrap comparison failures. */
5351 + /* Output a file symbol for the output file as the second symbol.
5352 + We output this even if we are discarding local symbols, although
5353 + I'm not sure if this is correct. */
5354 + elfsym.st_value = 0;
5355 + elfsym.st_size = 0;
5356 + elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5357 + elfsym.st_other = 0;
5358 + elfsym.st_shndx = SHN_ABS;
5359 + if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5360 + &elfsym, bfd_abs_section_ptr))
5361 + goto error_return;
5362 +#endif
5363 +
5364 + /* Output a symbol for each section. We output these even if we are
5365 + discarding local symbols, since they are used for relocs. These
5366 + symbols have no names. We store the index of each one in the
5367 + index field of the section, so that we can find it again when
5368 + outputting relocs. */
5369 + if (info->strip != strip_all
5370 + || emit_relocs)
5371 + {
5372 + elfsym.st_size = 0;
5373 + elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5374 + elfsym.st_other = 0;
5375 + for (i = 1; i < elf_numsections (abfd); i++)
5376 + {
5377 + o = section_from_elf_index (abfd, i);
5378 + if (o != NULL)
5379 + o->target_index = bfd_get_symcount (abfd);
5380 + elfsym.st_shndx = i;
5381 + if (info->relocateable || o == NULL)
5382 + elfsym.st_value = 0;
5383 + else
5384 + elfsym.st_value = o->vma;
5385 + if (! elf_link_output_sym (&finfo, (const char *) NULL,
5386 + &elfsym, o))
5387 + goto error_return;
5388 + if (i == SHN_LORESERVE)
5389 + i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5390 + }
5391 + }
5392 +
5393 + /* Allocate some memory to hold information read in from the input
5394 + files. */
5395 + if (max_contents_size != 0)
5396 + {
5397 + finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5398 + if (finfo.contents == NULL)
5399 + goto error_return;
5400 + }
5401 +
5402 + if (max_external_reloc_size != 0)
5403 + {
5404 + finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5405 + if (finfo.external_relocs == NULL)
5406 + goto error_return;
5407 + }
5408 +
5409 + if (max_internal_reloc_count != 0)
5410 + {
5411 + amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5412 + amt *= sizeof (Elf_Internal_Rela);
5413 + finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5414 + if (finfo.internal_relocs == NULL)
5415 + goto error_return;
5416 + }
5417 +
5418 + if (max_sym_count != 0)
5419 + {
5420 + amt = max_sym_count * sizeof (Elf_External_Sym);
5421 + finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5422 + if (finfo.external_syms == NULL)
5423 + goto error_return;
5424 +
5425 + amt = max_sym_count * sizeof (Elf_Internal_Sym);
5426 + finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5427 + if (finfo.internal_syms == NULL)
5428 + goto error_return;
5429 +
5430 + amt = max_sym_count * sizeof (long);
5431 + finfo.indices = (long *) bfd_malloc (amt);
5432 + if (finfo.indices == NULL)
5433 + goto error_return;
5434 +
5435 + amt = max_sym_count * sizeof (asection *);
5436 + finfo.sections = (asection **) bfd_malloc (amt);
5437 + if (finfo.sections == NULL)
5438 + goto error_return;
5439 + }
5440 +
5441 + if (max_sym_shndx_count != 0)
5442 + {
5443 + amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5444 + finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5445 + if (finfo.locsym_shndx == NULL)
5446 + goto error_return;
5447 + }
5448 +
5449 + if (finfo.first_tls_sec)
5450 + {
5451 + unsigned int align = 0;
5452 + bfd_vma base = finfo.first_tls_sec->vma, end = 0;
5453 + asection *sec;
5454 +
5455 + for (sec = finfo.first_tls_sec;
5456 + sec && (sec->flags & SEC_THREAD_LOCAL);
5457 + sec = sec->next)
5458 + {
5459 + bfd_vma size = sec->_raw_size;
5460 +
5461 + if (bfd_get_section_alignment (abfd, sec) > align)
5462 + align = bfd_get_section_alignment (abfd, sec);
5463 + if (sec->_raw_size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
5464 + {
5465 + struct bfd_link_order *o;
5466 +
5467 + size = 0;
5468 + for (o = sec->link_order_head; o != NULL; o = o->next)
5469 + if (size < o->offset + o->size)
5470 + size = o->offset + o->size;
5471 + }
5472 + end = sec->vma + size;
5473 + }
5474 + elf_hash_table (info)->tls_segment
5475 + = bfd_zalloc (abfd, sizeof (struct elf_link_tls_segment));
5476 + if (elf_hash_table (info)->tls_segment == NULL)
5477 + goto error_return;
5478 + elf_hash_table (info)->tls_segment->start = base;
5479 + elf_hash_table (info)->tls_segment->size = end - base;
5480 + elf_hash_table (info)->tls_segment->align = align;
5481 + }
5482 +
5483 + /* Since ELF permits relocations to be against local symbols, we
5484 + must have the local symbols available when we do the relocations.
5485 + Since we would rather only read the local symbols once, and we
5486 + would rather not keep them in memory, we handle all the
5487 + relocations for a single input file at the same time.
5488 +
5489 + Unfortunately, there is no way to know the total number of local
5490 + symbols until we have seen all of them, and the local symbol
5491 + indices precede the global symbol indices. This means that when
5492 + we are generating relocateable output, and we see a reloc against
5493 + a global symbol, we can not know the symbol index until we have
5494 + finished examining all the local symbols to see which ones we are
5495 + going to output. To deal with this, we keep the relocations in
5496 + memory, and don't output them until the end of the link. This is
5497 + an unfortunate waste of memory, but I don't see a good way around
5498 + it. Fortunately, it only happens when performing a relocateable
5499 + link, which is not the common case. FIXME: If keep_memory is set
5500 + we could write the relocs out and then read them again; I don't
5501 + know how bad the memory loss will be. */
5502 +
5503 + for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5504 + sub->output_has_begun = false;
5505 + for (o = abfd->sections; o != NULL; o = o->next)
5506 + {
5507 + for (p = o->link_order_head; p != NULL; p = p->next)
5508 + {
5509 + if (p->type == bfd_indirect_link_order
5510 + && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
5511 + == bfd_target_elf_flavour)
5512 + && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
5513 + {
5514 + if (! sub->output_has_begun)
5515 + {
5516 + if (! elf_link_input_bfd (&finfo, sub))
5517 + goto error_return;
5518 + sub->output_has_begun = true;
5519 + }
5520 + }
5521 + else if (p->type == bfd_section_reloc_link_order
5522 + || p->type == bfd_symbol_reloc_link_order)
5523 + {
5524 + if (! elf_reloc_link_order (abfd, info, o, p))
5525 + goto error_return;
5526 + }
5527 + else
5528 + {
5529 + if (! _bfd_default_link_order (abfd, info, o, p))
5530 + goto error_return;
5531 + }
5532 + }
5533 + }
5534 +
5535 + /* Output any global symbols that got converted to local in a
5536 + version script or due to symbol visibility. We do this in a
5537 + separate step since ELF requires all local symbols to appear
5538 + prior to any global symbols. FIXME: We should only do this if
5539 + some global symbols were, in fact, converted to become local.
5540 + FIXME: Will this work correctly with the Irix 5 linker? */
5541 + eoinfo.failed = false;
5542 + eoinfo.finfo = &finfo;
5543 + eoinfo.localsyms = true;
5544 + elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5545 + (PTR) &eoinfo);
5546 + if (eoinfo.failed)
5547 + return false;
5548 +
5549 + /* That wrote out all the local symbols. Finish up the symbol table
5550 + with the global symbols. Even if we want to strip everything we
5551 + can, we still need to deal with those global symbols that got
5552 + converted to local in a version script. */
5553 +
5554 + /* The sh_info field records the index of the first non local symbol. */
5555 + symtab_hdr->sh_info = bfd_get_symcount (abfd);
5556 +
5557 + if (dynamic
5558 + && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5559 + {
5560 + Elf_Internal_Sym sym;
5561 + Elf_External_Sym *dynsym =
5562 + (Elf_External_Sym *) finfo.dynsym_sec->contents;
5563 + long last_local = 0;
5564 +
5565 + /* Write out the section symbols for the output sections. */
5566 + if (info->shared)
5567 + {
5568 + asection *s;
5569 +
5570 + sym.st_size = 0;
5571 + sym.st_name = 0;
5572 + sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5573 + sym.st_other = 0;
5574 +
5575 + for (s = abfd->sections; s != NULL; s = s->next)
5576 + {
5577 + int indx;
5578 + Elf_External_Sym *dest;
5579 +
5580 + indx = elf_section_data (s)->this_idx;
5581 + BFD_ASSERT (indx > 0);
5582 + sym.st_shndx = indx;
5583 + sym.st_value = s->vma;
5584 + dest = dynsym + elf_section_data (s)->dynindx;
5585 + elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5586 + }
5587 +
5588 + last_local = bfd_count_sections (abfd);
5589 + }
5590 +
5591 + /* Write out the local dynsyms. */
5592 + if (elf_hash_table (info)->dynlocal)
5593 + {
5594 + struct elf_link_local_dynamic_entry *e;
5595 + for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5596 + {
5597 + asection *s;
5598 + Elf_External_Sym *dest;
5599 +
5600 + sym.st_size = e->isym.st_size;
5601 + sym.st_other = e->isym.st_other;
5602 +
5603 + /* Copy the internal symbol as is.
5604 + Note that we saved a word of storage and overwrote
5605 + the original st_name with the dynstr_index. */
5606 + sym = e->isym;
5607 +
5608 + if (e->isym.st_shndx != SHN_UNDEF
5609 + && (e->isym.st_shndx < SHN_LORESERVE
5610 + || e->isym.st_shndx > SHN_HIRESERVE))
5611 + {
5612 + s = bfd_section_from_elf_index (e->input_bfd,
5613 + e->isym.st_shndx);
5614 +
5615 + sym.st_shndx =
5616 + elf_section_data (s->output_section)->this_idx;
5617 + sym.st_value = (s->output_section->vma
5618 + + s->output_offset
5619 + + e->isym.st_value);
5620 + }
5621 +
5622 + if (last_local < e->dynindx)
5623 + last_local = e->dynindx;
5624 +
5625 + dest = dynsym + e->dynindx;
5626 + elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5627 + }
5628 + }
5629 +
5630 + elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5631 + last_local + 1;
5632 + }
5633 +
5634 + /* We get the global symbols from the hash table. */
5635 + eoinfo.failed = false;
5636 + eoinfo.localsyms = false;
5637 + eoinfo.finfo = &finfo;
5638 + elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5639 + (PTR) &eoinfo);
5640 + if (eoinfo.failed)
5641 + return false;
5642 +
5643 + /* If backend needs to output some symbols not present in the hash
5644 + table, do it now. */
5645 + if (bed->elf_backend_output_arch_syms)
5646 + {
5647 + typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5648 + Elf_Internal_Sym *,
5649 + asection *));
5650 +
5651 + if (! ((*bed->elf_backend_output_arch_syms)
5652 + (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5653 + return false;
5654 + }
5655 +
5656 + /* Flush all symbols to the file. */
5657 + if (! elf_link_flush_output_syms (&finfo))
5658 + return false;
5659 +
5660 + /* Now we know the size of the symtab section. */
5661 + off += symtab_hdr->sh_size;
5662 +
5663 + /* Finish up and write out the symbol string table (.strtab)
5664 + section. */
5665 + symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5666 + /* sh_name was set in prep_headers. */
5667 + symstrtab_hdr->sh_type = SHT_STRTAB;
5668 + symstrtab_hdr->sh_flags = 0;
5669 + symstrtab_hdr->sh_addr = 0;
5670 + symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5671 + symstrtab_hdr->sh_entsize = 0;
5672 + symstrtab_hdr->sh_link = 0;
5673 + symstrtab_hdr->sh_info = 0;
5674 + /* sh_offset is set just below. */
5675 + symstrtab_hdr->sh_addralign = 1;
5676 +
5677 + off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5678 + elf_tdata (abfd)->next_file_pos = off;
5679 +
5680 + if (bfd_get_symcount (abfd) > 0)
5681 + {
5682 + if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5683 + || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5684 + return false;
5685 + }
5686 +
5687 + /* Adjust the relocs to have the correct symbol indices. */
5688 + for (o = abfd->sections; o != NULL; o = o->next)
5689 + {
5690 + if ((o->flags & SEC_RELOC) == 0)
5691 + continue;
5692 +
5693 + elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5694 + elf_section_data (o)->rel_count,
5695 + elf_section_data (o)->rel_hashes);
5696 + if (elf_section_data (o)->rel_hdr2 != NULL)
5697 + elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5698 + elf_section_data (o)->rel_count2,
5699 + (elf_section_data (o)->rel_hashes
5700 + + elf_section_data (o)->rel_count));
5701 +
5702 + /* Set the reloc_count field to 0 to prevent write_relocs from
5703 + trying to swap the relocs out itself. */
5704 + o->reloc_count = 0;
5705 + }
5706 +
5707 + if (dynamic && info->combreloc && dynobj != NULL)
5708 + relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5709 +
5710 + /* If we are linking against a dynamic object, or generating a
5711 + shared library, finish up the dynamic linking information. */
5712 + if (dynamic)
5713 + {
5714 + Elf_External_Dyn *dyncon, *dynconend;
5715 +
5716 + /* Fix up .dynamic entries. */
5717 + o = bfd_get_section_by_name (dynobj, ".dynamic");
5718 + BFD_ASSERT (o != NULL);
5719 +
5720 + dyncon = (Elf_External_Dyn *) o->contents;
5721 + dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5722 + for (; dyncon < dynconend; dyncon++)
5723 + {
5724 + Elf_Internal_Dyn dyn;
5725 + const char *name;
5726 + unsigned int type;
5727 +
5728 + elf_swap_dyn_in (dynobj, dyncon, &dyn);
5729 +
5730 + switch (dyn.d_tag)
5731 + {
5732 + default:
5733 + break;
5734 + case DT_NULL:
5735 + if (relativecount > 0 && dyncon + 1 < dynconend)
5736 + {
5737 + switch (elf_section_data (reldyn)->this_hdr.sh_type)
5738 + {
5739 + case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5740 + case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5741 + default: break;
5742 + }
5743 + if (dyn.d_tag != DT_NULL)
5744 + {
5745 + dyn.d_un.d_val = relativecount;
5746 + elf_swap_dyn_out (dynobj, &dyn, dyncon);
5747 + relativecount = 0;
5748 + }
5749 + }
5750 + break;
5751 + case DT_INIT:
5752 + name = info->init_function;
5753 + goto get_sym;
5754 + case DT_FINI:
5755 + name = info->fini_function;
5756 + get_sym:
5757 + {
5758 + struct elf_link_hash_entry *h;
5759 +
5760 + h = elf_link_hash_lookup (elf_hash_table (info), name,
5761 + false, false, true);
5762 + if (h != NULL
5763 + && (h->root.type == bfd_link_hash_defined
5764 + || h->root.type == bfd_link_hash_defweak))
5765 + {
5766 + dyn.d_un.d_val = h->root.u.def.value;
5767 + o = h->root.u.def.section;
5768 + if (o->output_section != NULL)
5769 + dyn.d_un.d_val += (o->output_section->vma
5770 + + o->output_offset);
5771 + else
5772 + {
5773 + /* The symbol is imported from another shared
5774 + library and does not apply to this one. */
5775 + dyn.d_un.d_val = 0;
5776 + }
5777 +
5778 + elf_swap_dyn_out (dynobj, &dyn, dyncon);
5779 + }
5780 + }
5781 + break;
5782 +
5783 + case DT_PREINIT_ARRAYSZ:
5784 + name = ".preinit_array";
5785 + goto get_size;
5786 + case DT_INIT_ARRAYSZ:
5787 + name = ".init_array";
5788 + goto get_size;
5789 + case DT_FINI_ARRAYSZ:
5790 + name = ".fini_array";
5791 + get_size:
5792 + o = bfd_get_section_by_name (abfd, name);
5793 + if (o == NULL)
5794 + {
5795 + (*_bfd_error_handler)
5796 + (_("%s: could not find output section %s"),
5797 + bfd_get_filename (abfd), name);
5798 + goto error_return;
5799 + }
5800 + if (o->_raw_size == 0)
5801 + (*_bfd_error_handler)
5802 + (_("warning: %s section has zero size"), name);
5803 + dyn.d_un.d_val = o->_raw_size;
5804 + elf_swap_dyn_out (dynobj, &dyn, dyncon);
5805 + break;
5806 +
5807 + case DT_PREINIT_ARRAY:
5808 + name = ".preinit_array";
5809 + goto get_vma;
5810 + case DT_INIT_ARRAY:
5811 + name = ".init_array";
5812 + goto get_vma;
5813 + case DT_FINI_ARRAY:
5814 + name = ".fini_array";
5815 + goto get_vma;
5816 +
5817 + case DT_HASH:
5818 + name = ".hash";
5819 + goto get_vma;
5820 + case DT_STRTAB:
5821 + name = ".dynstr";
5822 + goto get_vma;
5823 + case DT_SYMTAB:
5824 + name = ".dynsym";
5825 + goto get_vma;
5826 + case DT_VERDEF:
5827 + name = ".gnu.version_d";
5828 + goto get_vma;
5829 + case DT_VERNEED:
5830 + name = ".gnu.version_r";
5831 + goto get_vma;
5832 + case DT_VERSYM:
5833 + name = ".gnu.version";
5834 + get_vma:
5835 + o = bfd_get_section_by_name (abfd, name);
5836 + if (o == NULL)
5837 + {
5838 + (*_bfd_error_handler)
5839 + (_("%s: could not find output section %s"),
5840 + bfd_get_filename (abfd), name);
5841 + goto error_return;
5842 + }
5843 + dyn.d_un.d_ptr = o->vma;
5844 + elf_swap_dyn_out (dynobj, &dyn, dyncon);
5845 + break;
5846 +
5847 + case DT_REL:
5848 + case DT_RELA:
5849 + case DT_RELSZ:
5850 + case DT_RELASZ:
5851 + if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5852 + type = SHT_REL;
5853 + else
5854 + type = SHT_RELA;
5855 + dyn.d_un.d_val = 0;
5856 + for (i = 1; i < elf_numsections (abfd); i++)
5857 + {
5858 + Elf_Internal_Shdr *hdr;
5859 +
5860 + hdr = elf_elfsections (abfd)[i];
5861 + if (hdr->sh_type == type
5862 + && (hdr->sh_flags & SHF_ALLOC) != 0)
5863 + {
5864 + if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5865 + dyn.d_un.d_val += hdr->sh_size;
5866 + else
5867 + {
5868 + if (dyn.d_un.d_val == 0
5869 + || hdr->sh_addr < dyn.d_un.d_val)
5870 + dyn.d_un.d_val = hdr->sh_addr;
5871 + }
5872 + }
5873 + }
5874 + elf_swap_dyn_out (dynobj, &dyn, dyncon);
5875 + break;
5876 + }
5877 + }
5878 + }
5879 +
5880 + /* If we have created any dynamic sections, then output them. */
5881 + if (dynobj != NULL)
5882 + {
5883 + if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5884 + goto error_return;
5885 +
5886 + for (o = dynobj->sections; o != NULL; o = o->next)
5887 + {
5888 + if ((o->flags & SEC_HAS_CONTENTS) == 0
5889 + || o->_raw_size == 0
5890 + || o->output_section == bfd_abs_section_ptr)
5891 + continue;
5892 + if ((o->flags & SEC_LINKER_CREATED) == 0)
5893 + {
5894 + /* At this point, we are only interested in sections
5895 + created by elf_link_create_dynamic_sections. */
5896 + continue;
5897 + }
5898 + if ((elf_section_data (o->output_section)->this_hdr.sh_type
5899 + != SHT_STRTAB)
5900 + || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5901 + {
5902 + if (! bfd_set_section_contents (abfd, o->output_section,
5903 + o->contents,
5904 + (file_ptr) o->output_offset,
5905 + o->_raw_size))
5906 + goto error_return;
5907 + }
5908 + else
5909 + {
5910 + /* The contents of the .dynstr section are actually in a
5911 + stringtab. */
5912 + off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5913 + if (bfd_seek (abfd, off, SEEK_SET) != 0
5914 + || ! _bfd_elf_strtab_emit (abfd,
5915 + elf_hash_table (info)->dynstr))
5916 + goto error_return;
5917 + }
5918 + }
5919 + }
5920 +
5921 + if (info->relocateable)
5922 + {
5923 + boolean failed = false;
5924 +
5925 + bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
5926 + if (failed)
5927 + goto error_return;
5928 + }
5929 +
5930 + /* If we have optimized stabs strings, output them. */
5931 + if (elf_hash_table (info)->stab_info != NULL)
5932 + {
5933 + if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5934 + goto error_return;
5935 + }
5936 +
5937 + if (info->eh_frame_hdr && elf_hash_table (info)->dynobj)
5938 + {
5939 + o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5940 + ".eh_frame_hdr");
5941 + if (o
5942 + && (elf_section_data (o)->sec_info_type
5943 + == ELF_INFO_TYPE_EH_FRAME_HDR))
5944 + {
5945 + if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5946 + goto error_return;
5947 + }
5948 + }
5949 +
5950 + if (finfo.symstrtab != NULL)
5951 + _bfd_stringtab_free (finfo.symstrtab);
5952 + if (finfo.contents != NULL)
5953 + free (finfo.contents);
5954 + if (finfo.external_relocs != NULL)
5955 + free (finfo.external_relocs);
5956 + if (finfo.internal_relocs != NULL)
5957 + free (finfo.internal_relocs);
5958 + if (finfo.external_syms != NULL)
5959 + free (finfo.external_syms);
5960 + if (finfo.locsym_shndx != NULL)
5961 + free (finfo.locsym_shndx);
5962 + if (finfo.internal_syms != NULL)
5963 + free (finfo.internal_syms);
5964 + if (finfo.indices != NULL)
5965 + free (finfo.indices);
5966 + if (finfo.sections != NULL)
5967 + free (finfo.sections);
5968 + if (finfo.symbuf != NULL)
5969 + free (finfo.symbuf);
5970 + if (finfo.symshndxbuf != NULL)
5971 + free (finfo.symbuf);
5972 + for (o = abfd->sections; o != NULL; o = o->next)
5973 + {
5974 + if ((o->flags & SEC_RELOC) != 0
5975 + && elf_section_data (o)->rel_hashes != NULL)
5976 + free (elf_section_data (o)->rel_hashes);
5977 + }
5978 +
5979 + elf_tdata (abfd)->linker = true;
5980 +
5981 + return true;
5982 +
5983 + error_return:
5984 + if (finfo.symstrtab != NULL)
5985 + _bfd_stringtab_free (finfo.symstrtab);
5986 + if (finfo.contents != NULL)
5987 + free (finfo.contents);
5988 + if (finfo.external_relocs != NULL)
5989 + free (finfo.external_relocs);
5990 + if (finfo.internal_relocs != NULL)
5991 + free (finfo.internal_relocs);
5992 + if (finfo.external_syms != NULL)
5993 + free (finfo.external_syms);
5994 + if (finfo.locsym_shndx != NULL)
5995 + free (finfo.locsym_shndx);
5996 + if (finfo.internal_syms != NULL)
5997 + free (finfo.internal_syms);
5998 + if (finfo.indices != NULL)
5999 + free (finfo.indices);
6000 + if (finfo.sections != NULL)
6001 + free (finfo.sections);
6002 + if (finfo.symbuf != NULL)
6003 + free (finfo.symbuf);
6004 + if (finfo.symshndxbuf != NULL)
6005 + free (finfo.symbuf);
6006 + for (o = abfd->sections; o != NULL; o = o->next)
6007 + {
6008 + if ((o->flags & SEC_RELOC) != 0
6009 + && elf_section_data (o)->rel_hashes != NULL)
6010 + free (elf_section_data (o)->rel_hashes);
6011 + }
6012 +
6013 + return false;
6014 +}
6015 +
6016 +/* Add a symbol to the output symbol table. */
6017 +
6018 +static boolean
6019 +elf_link_output_sym (finfo, name, elfsym, input_sec)
6020 + struct elf_final_link_info *finfo;
6021 + const char *name;
6022 + Elf_Internal_Sym *elfsym;
6023 + asection *input_sec;
6024 +{
6025 + Elf_External_Sym *dest;
6026 + Elf_External_Sym_Shndx *destshndx;
6027 +
6028 + boolean (*output_symbol_hook) PARAMS ((bfd *,
6029 + struct bfd_link_info *info,
6030 + const char *,
6031 + Elf_Internal_Sym *,
6032 + asection *));
6033 +
6034 + output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
6035 + elf_backend_link_output_symbol_hook;
6036 + if (output_symbol_hook != NULL)
6037 + {
6038 + if (! ((*output_symbol_hook)
6039 + (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
6040 + return false;
6041 + }
6042 +
6043 + if (name == (const char *) NULL || *name == '\0')
6044 + elfsym->st_name = 0;
6045 + else if (input_sec->flags & SEC_EXCLUDE)
6046 + elfsym->st_name = 0;
6047 + else
6048 + {
6049 + elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6050 + name, true, false);
6051 + if (elfsym->st_name == (unsigned long) -1)
6052 + return false;
6053 + }
6054 +
6055 + if (finfo->symbuf_count >= finfo->symbuf_size)
6056 + {
6057 + if (! elf_link_flush_output_syms (finfo))
6058 + return false;
6059 + }
6060 +
6061 + dest = finfo->symbuf + finfo->symbuf_count;
6062 + destshndx = finfo->symshndxbuf;
6063 + if (destshndx != NULL)
6064 + destshndx += finfo->symbuf_count;
6065 + elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
6066 + ++finfo->symbuf_count;
6067 +
6068 + ++ bfd_get_symcount (finfo->output_bfd);
6069 +
6070 + return true;
6071 +}
6072 +
6073 +/* Flush the output symbols to the file. */
6074 +
6075 +static boolean
6076 +elf_link_flush_output_syms (finfo)
6077 + struct elf_final_link_info *finfo;
6078 +{
6079 + if (finfo->symbuf_count > 0)
6080 + {
6081 + Elf_Internal_Shdr *hdr;
6082 + file_ptr pos;
6083 + bfd_size_type amt;
6084 +
6085 + hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6086 + pos = hdr->sh_offset + hdr->sh_size;
6087 + amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
6088 + if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6089 + || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
6090 + return false;
6091 +
6092 + hdr->sh_size += amt;
6093 +
6094 + if (finfo->symshndxbuf != NULL)
6095 + {
6096 + hdr = &elf_tdata (finfo->output_bfd)->symtab_shndx_hdr;
6097 + pos = hdr->sh_offset + hdr->sh_size;
6098 + amt = finfo->symbuf_count * sizeof (Elf_External_Sym_Shndx);
6099 + if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6100 + || (bfd_bwrite ((PTR) finfo->symshndxbuf, amt, finfo->output_bfd)
6101 + != amt))
6102 + return false;
6103 +
6104 + hdr->sh_size += amt;
6105 + }
6106 +
6107 + finfo->symbuf_count = 0;
6108 + }
6109 +
6110 + return true;
6111 +}
6112 +
6113 +/* Adjust all external symbols pointing into SEC_MERGE sections
6114 + to reflect the object merging within the sections. */
6115 +
6116 +static boolean
6117 +elf_link_sec_merge_syms (h, data)
6118 + struct elf_link_hash_entry *h;
6119 + PTR data;
6120 +{
6121 + asection *sec;
6122 +
6123 + if (h->root.type == bfd_link_hash_warning)
6124 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
6125 +
6126 + if ((h->root.type == bfd_link_hash_defined
6127 + || h->root.type == bfd_link_hash_defweak)
6128 + && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
6129 + && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
6130 + {
6131 + bfd *output_bfd = (bfd *) data;
6132 +
6133 + h->root.u.def.value =
6134 + _bfd_merged_section_offset (output_bfd,
6135 + &h->root.u.def.section,
6136 + elf_section_data (sec)->sec_info,
6137 + h->root.u.def.value, (bfd_vma) 0);
6138 + }
6139 +
6140 + return true;
6141 +}
6142 +
6143 +/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6144 + allowing an unsatisfied unversioned symbol in the DSO to match a
6145 + versioned symbol that would normally require an explicit version. */
6146 +
6147 +static boolean
6148 +elf_link_check_versioned_symbol (info, h)
6149 + struct bfd_link_info *info;
6150 + struct elf_link_hash_entry *h;
6151 +{
6152 + bfd *undef_bfd = h->root.u.undef.abfd;
6153 + struct elf_link_loaded_list *loaded;
6154 +
6155 + if ((undef_bfd->flags & DYNAMIC) == 0
6156 + || info->hash->creator->flavour != bfd_target_elf_flavour
6157 + || elf_dt_soname (h->root.u.undef.abfd) == NULL)
6158 + return false;
6159 +
6160 + for (loaded = elf_hash_table (info)->loaded;
6161 + loaded != NULL;
6162 + loaded = loaded->next)
6163 + {
6164 + bfd *input;
6165 + Elf_Internal_Shdr *hdr;
6166 + bfd_size_type symcount;
6167 + bfd_size_type extsymcount;
6168 + bfd_size_type extsymoff;
6169 + Elf_Internal_Shdr *versymhdr;
6170 + Elf_Internal_Sym *isym;
6171 + Elf_Internal_Sym *isymend;
6172 + Elf_Internal_Sym *isymbuf;
6173 + Elf_External_Versym *ever;
6174 + Elf_External_Versym *extversym;
6175 +
6176 + input = loaded->abfd;
6177 +
6178 + /* We check each DSO for a possible hidden versioned definition. */
6179 + if (input == undef_bfd
6180 + || (input->flags & DYNAMIC) == 0
6181 + || elf_dynversym (input) == 0)
6182 + continue;
6183 +
6184 + hdr = &elf_tdata (input)->dynsymtab_hdr;
6185 +
6186 + symcount = hdr->sh_size / sizeof (Elf_External_Sym);
6187 + if (elf_bad_symtab (input))
6188 + {
6189 + extsymcount = symcount;
6190 + extsymoff = 0;
6191 + }
6192 + else
6193 + {
6194 + extsymcount = symcount - hdr->sh_info;
6195 + extsymoff = hdr->sh_info;
6196 + }
6197 +
6198 + if (extsymcount == 0)
6199 + continue;
6200 +
6201 + isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6202 + NULL, NULL, NULL);
6203 + if (isymbuf == NULL)
6204 + return false;
6205 +
6206 + /* Read in any version definitions. */
6207 + versymhdr = &elf_tdata (input)->dynversym_hdr;
6208 + extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
6209 + if (extversym == NULL)
6210 + goto error_ret;
6211 +
6212 + if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6213 + || (bfd_bread ((PTR) extversym, versymhdr->sh_size, input)
6214 + != versymhdr->sh_size))
6215 + {
6216 + free (extversym);
6217 + error_ret:
6218 + free (isymbuf);
6219 + return false;
6220 + }
6221 +
6222 + ever = extversym + extsymoff;
6223 + isymend = isymbuf + extsymcount;
6224 + for (isym = isymbuf; isym < isymend; isym++, ever++)
6225 + {
6226 + const char *name;
6227 + Elf_Internal_Versym iver;
6228 +
6229 + if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6230 + || isym->st_shndx == SHN_UNDEF)
6231 + continue;
6232 +
6233 + name = bfd_elf_string_from_elf_section (input,
6234 + hdr->sh_link,
6235 + isym->st_name);
6236 + if (strcmp (name, h->root.root.string) != 0)
6237 + continue;
6238 +
6239 + _bfd_elf_swap_versym_in (input, ever, &iver);
6240 +
6241 + if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6242 + {
6243 + /* If we have a non-hidden versioned sym, then it should
6244 + have provided a definition for the undefined sym. */
6245 + abort ();
6246 + }
6247 +
6248 + if ((iver.vs_vers & VERSYM_VERSION) == 2)
6249 + {
6250 + /* This is the oldest (default) sym. We can use it. */
6251 + free (extversym);
6252 + free (isymbuf);
6253 + return true;
6254 + }
6255 + }
6256 +
6257 + free (extversym);
6258 + free (isymbuf);
6259 + }
6260 +
6261 + return false;
6262 +}
6263 +
6264 +/* Add an external symbol to the symbol table. This is called from
6265 + the hash table traversal routine. When generating a shared object,
6266 + we go through the symbol table twice. The first time we output
6267 + anything that might have been forced to local scope in a version
6268 + script. The second time we output the symbols that are still
6269 + global symbols. */
6270 +
6271 +static boolean
6272 +elf_link_output_extsym (h, data)
6273 + struct elf_link_hash_entry *h;
6274 + PTR data;
6275 +{
6276 + struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
6277 + struct elf_final_link_info *finfo = eoinfo->finfo;
6278 + boolean strip;
6279 + Elf_Internal_Sym sym;
6280 + asection *input_sec;
6281 +
6282 + if (h->root.type == bfd_link_hash_warning)
6283 + {
6284 + h = (struct elf_link_hash_entry *) h->root.u.i.link;
6285 + if (h->root.type == bfd_link_hash_new)
6286 + return true;
6287 + }
6288 +
6289 + /* Decide whether to output this symbol in this pass. */
6290 + if (eoinfo->localsyms)
6291 + {
6292 + if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6293 + return true;
6294 + }
6295 + else
6296 + {
6297 + if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6298 + return true;
6299 + }
6300 +
6301 + /* If we are not creating a shared library, and this symbol is
6302 + referenced by a shared library but is not defined anywhere, then
6303 + warn that it is undefined. If we do not do this, the runtime
6304 + linker will complain that the symbol is undefined when the
6305 + program is run. We don't have to worry about symbols that are
6306 + referenced by regular files, because we will already have issued
6307 + warnings for them. */
6308 + if (! finfo->info->relocateable
6309 + && ! finfo->info->allow_shlib_undefined
6310 + && ! finfo->info->shared
6311 + && h->root.type == bfd_link_hash_undefined
6312 + && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
6313 + && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
6314 + && ! elf_link_check_versioned_symbol (finfo->info, h))
6315 + {
6316 + if (! ((*finfo->info->callbacks->undefined_symbol)
6317 + (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6318 + (asection *) NULL, (bfd_vma) 0, true)))
6319 + {
6320 + eoinfo->failed = true;
6321 + return false;
6322 + }
6323 + }
6324 +
6325 + /* We don't want to output symbols that have never been mentioned by
6326 + a regular file, or that we have been told to strip. However, if
6327 + h->indx is set to -2, the symbol is used by a reloc and we must
6328 + output it. */
6329 + if (h->indx == -2)
6330 + strip = false;
6331 + else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6332 + || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6333 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6334 + && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6335 + strip = true;
6336 + else if (finfo->info->strip == strip_all
6337 + || (finfo->info->strip == strip_some
6338 + && bfd_hash_lookup (finfo->info->keep_hash,
6339 + h->root.root.string,
6340 + false, false) == NULL))
6341 + strip = true;
6342 + else
6343 + strip = false;
6344 +
6345 + /* If we're stripping it, and it's not a dynamic symbol, there's
6346 + nothing else to do unless it is a forced local symbol. */
6347 + if (strip
6348 + && h->dynindx == -1
6349 + && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6350 + return true;
6351 +
6352 + sym.st_value = 0;
6353 + sym.st_size = h->size;
6354 + sym.st_other = h->other;
6355 + if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6356 + sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6357 + else if (h->root.type == bfd_link_hash_undefweak
6358 + || h->root.type == bfd_link_hash_defweak)
6359 + sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6360 + else
6361 + sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6362 +
6363 + switch (h->root.type)
6364 + {
6365 + default:
6366 + case bfd_link_hash_new:
6367 + case bfd_link_hash_warning:
6368 + abort ();
6369 + return false;
6370 +
6371 + case bfd_link_hash_undefined:
6372 + case bfd_link_hash_undefweak:
6373 + input_sec = bfd_und_section_ptr;
6374 + sym.st_shndx = SHN_UNDEF;
6375 + break;
6376 +
6377 + case bfd_link_hash_defined:
6378 + case bfd_link_hash_defweak:
6379 + {
6380 + input_sec = h->root.u.def.section;
6381 + if (input_sec->output_section != NULL)
6382 + {
6383 + sym.st_shndx =
6384 + _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6385 + input_sec->output_section);
6386 + if (sym.st_shndx == SHN_BAD)
6387 + {
6388 + (*_bfd_error_handler)
6389 + (_("%s: could not find output section %s for input section %s"),
6390 + bfd_get_filename (finfo->output_bfd),
6391 + input_sec->output_section->name,
6392 + input_sec->name);
6393 + eoinfo->failed = true;
6394 + return false;
6395 + }
6396 +
6397 + /* ELF symbols in relocateable files are section relative,
6398 + but in nonrelocateable files they are virtual
6399 + addresses. */
6400 + sym.st_value = h->root.u.def.value + input_sec->output_offset;
6401 + if (! finfo->info->relocateable)
6402 + {
6403 + sym.st_value += input_sec->output_section->vma;
6404 + if (h->type == STT_TLS)
6405 + {
6406 + /* STT_TLS symbols are relative to PT_TLS segment
6407 + base. */
6408 + BFD_ASSERT (finfo->first_tls_sec != NULL);
6409 + sym.st_value -= finfo->first_tls_sec->vma;
6410 + }
6411 + }
6412 + }
6413 + else
6414 + {
6415 + BFD_ASSERT (input_sec->owner == NULL
6416 + || (input_sec->owner->flags & DYNAMIC) != 0);
6417 + sym.st_shndx = SHN_UNDEF;
6418 + input_sec = bfd_und_section_ptr;
6419 + }
6420 + }
6421 + break;
6422 +
6423 + case bfd_link_hash_common:
6424 + input_sec = h->root.u.c.p->section;
6425 + sym.st_shndx = SHN_COMMON;
6426 + sym.st_value = 1 << h->root.u.c.p->alignment_power;
6427 + break;
6428 +
6429 + case bfd_link_hash_indirect:
6430 + /* These symbols are created by symbol versioning. They point
6431 + to the decorated version of the name. For example, if the
6432 + symbol foo@@GNU_1.2 is the default, which should be used when
6433 + foo is used with no version, then we add an indirect symbol
6434 + foo which points to foo@@GNU_1.2. We ignore these symbols,
6435 + since the indirected symbol is already in the hash table. */
6436 + return true;
6437 + }
6438 +
6439 + /* Give the processor backend a chance to tweak the symbol value,
6440 + and also to finish up anything that needs to be done for this
6441 + symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6442 + forced local syms when non-shared is due to a historical quirk. */
6443 + if ((h->dynindx != -1
6444 + || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6445 + && (finfo->info->shared
6446 + || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6447 + && elf_hash_table (finfo->info)->dynamic_sections_created)
6448 + {
6449 + struct elf_backend_data *bed;
6450 +
6451 + bed = get_elf_backend_data (finfo->output_bfd);
6452 + if (! ((*bed->elf_backend_finish_dynamic_symbol)
6453 + (finfo->output_bfd, finfo->info, h, &sym)))
6454 + {
6455 + eoinfo->failed = true;
6456 + return false;
6457 + }
6458 + }
6459 +
6460 + /* If we are marking the symbol as undefined, and there are no
6461 + non-weak references to this symbol from a regular object, then
6462 + mark the symbol as weak undefined; if there are non-weak
6463 + references, mark the symbol as strong. We can't do this earlier,
6464 + because it might not be marked as undefined until the
6465 + finish_dynamic_symbol routine gets through with it. */
6466 + if (sym.st_shndx == SHN_UNDEF
6467 + && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6468 + && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6469 + || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6470 + {
6471 + int bindtype;
6472 +
6473 + if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6474 + bindtype = STB_GLOBAL;
6475 + else
6476 + bindtype = STB_WEAK;
6477 + sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6478 + }
6479 +
6480 + /* If a symbol is not defined locally, we clear the visibility field. */
6481 + if (! finfo->info->relocateable
6482 + && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6483 + sym.st_other &= ~ ELF_ST_VISIBILITY (-1);
6484 +
6485 + /* If this symbol should be put in the .dynsym section, then put it
6486 + there now. We already know the symbol index. We also fill in
6487 + the entry in the .hash section. */
6488 + if (h->dynindx != -1
6489 + && elf_hash_table (finfo->info)->dynamic_sections_created)
6490 + {
6491 + size_t bucketcount;
6492 + size_t bucket;
6493 + size_t hash_entry_size;
6494 + bfd_byte *bucketpos;
6495 + bfd_vma chain;
6496 + Elf_External_Sym *esym;
6497 +
6498 + sym.st_name = h->dynstr_index;
6499 + esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
6500 + elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
6501 +
6502 + bucketcount = elf_hash_table (finfo->info)->bucketcount;
6503 + bucket = h->elf_hash_value % bucketcount;
6504 + hash_entry_size
6505 + = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6506 + bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6507 + + (bucket + 2) * hash_entry_size);
6508 + chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6509 + bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6510 + bucketpos);
6511 + bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6512 + ((bfd_byte *) finfo->hash_sec->contents
6513 + + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6514 +
6515 + if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6516 + {
6517 + Elf_Internal_Versym iversym;
6518 + Elf_External_Versym *eversym;
6519 +
6520 + if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6521 + {
6522 + if (h->verinfo.verdef == NULL)
6523 + iversym.vs_vers = 0;
6524 + else
6525 + iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6526 + }
6527 + else
6528 + {
6529 + if (h->verinfo.vertree == NULL)
6530 + iversym.vs_vers = 1;
6531 + else
6532 + iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6533 + }
6534 +
6535 + if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6536 + iversym.vs_vers |= VERSYM_HIDDEN;
6537 +
6538 + eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6539 + eversym += h->dynindx;
6540 + _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6541 + }
6542 + }
6543 +
6544 + /* If we're stripping it, then it was just a dynamic symbol, and
6545 + there's nothing else to do. */
6546 + if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6547 + return true;
6548 +