* gas/i860/dir-intel03-err.l: Update for junk at end line becoming
[deliverable/binutils-gdb.git] / bfd / elflink.h
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 /* ELF linker code. */
22
23 static bfd_boolean elf_section_ignore_discarded_relocs (asection *);
24
25 /* Final phase of ELF linker. */
26
27 /* A structure we use to avoid passing large numbers of arguments. */
28
29 struct elf_final_link_info
30 {
31 /* General link information. */
32 struct bfd_link_info *info;
33 /* Output BFD. */
34 bfd *output_bfd;
35 /* Symbol string table. */
36 struct bfd_strtab_hash *symstrtab;
37 /* .dynsym section. */
38 asection *dynsym_sec;
39 /* .hash section. */
40 asection *hash_sec;
41 /* symbol version section (.gnu.version). */
42 asection *symver_sec;
43 /* Buffer large enough to hold contents of any section. */
44 bfd_byte *contents;
45 /* Buffer large enough to hold external relocs of any section. */
46 void *external_relocs;
47 /* Buffer large enough to hold internal relocs of any section. */
48 Elf_Internal_Rela *internal_relocs;
49 /* Buffer large enough to hold external local symbols of any input
50 BFD. */
51 bfd_byte *external_syms;
52 /* And a buffer for symbol section indices. */
53 Elf_External_Sym_Shndx *locsym_shndx;
54 /* Buffer large enough to hold internal local symbols of any input
55 BFD. */
56 Elf_Internal_Sym *internal_syms;
57 /* Array large enough to hold a symbol index for each local symbol
58 of any input BFD. */
59 long *indices;
60 /* Array large enough to hold a section pointer for each local
61 symbol of any input BFD. */
62 asection **sections;
63 /* Buffer to hold swapped out symbols. */
64 bfd_byte *symbuf;
65 /* And one for symbol section indices. */
66 Elf_External_Sym_Shndx *symshndxbuf;
67 /* Number of swapped out symbols in buffer. */
68 size_t symbuf_count;
69 /* Number of symbols which fit in symbuf. */
70 size_t symbuf_size;
71 /* And same for symshndxbuf. */
72 size_t shndxbuf_size;
73 };
74
75 static bfd_boolean elf_link_output_sym
76 (struct elf_final_link_info *, const char *, Elf_Internal_Sym *, asection *,
77 struct elf_link_hash_entry *);
78 static bfd_boolean elf_link_flush_output_syms
79 (struct elf_final_link_info *, const struct elf_backend_data *);
80 static bfd_boolean elf_link_output_extsym
81 (struct elf_link_hash_entry *, void *);
82 static bfd_boolean elf_link_input_bfd
83 (struct elf_final_link_info *, bfd *);
84 static bfd_boolean elf_reloc_link_order
85 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
86
87 /* This struct is used to pass information to elf_link_output_extsym. */
88
89 struct elf_outext_info
90 {
91 bfd_boolean failed;
92 bfd_boolean localsyms;
93 struct elf_final_link_info *finfo;
94 };
95
96 /* When performing a relocatable link, the input relocations are
97 preserved. But, if they reference global symbols, the indices
98 referenced must be updated. Update all the relocations in
99 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
100
101 static void
102 elf_link_adjust_relocs (bfd *abfd,
103 Elf_Internal_Shdr *rel_hdr,
104 unsigned int count,
105 struct elf_link_hash_entry **rel_hash)
106 {
107 unsigned int i;
108 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
109 bfd_byte *erela;
110 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
111 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
112 bfd_vma r_type_mask;
113 int r_sym_shift;
114
115 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
116 {
117 swap_in = bed->s->swap_reloc_in;
118 swap_out = bed->s->swap_reloc_out;
119 }
120 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
121 {
122 swap_in = bed->s->swap_reloca_in;
123 swap_out = bed->s->swap_reloca_out;
124 }
125 else
126 abort ();
127
128 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
129 abort ();
130
131 if (bed->s->arch_size == 32)
132 {
133 r_type_mask = 0xff;
134 r_sym_shift = 8;
135 }
136 else
137 {
138 r_type_mask = 0xffffffff;
139 r_sym_shift = 32;
140 }
141
142 erela = rel_hdr->contents;
143 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
144 {
145 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
146 unsigned int j;
147
148 if (*rel_hash == NULL)
149 continue;
150
151 BFD_ASSERT ((*rel_hash)->indx >= 0);
152
153 (*swap_in) (abfd, erela, irela);
154 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
155 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
156 | (irela[j].r_info & r_type_mask));
157 (*swap_out) (abfd, irela, erela);
158 }
159 }
160
161 struct elf_link_sort_rela
162 {
163 union {
164 bfd_vma offset;
165 bfd_vma sym_mask;
166 } u;
167 enum elf_reloc_type_class type;
168 /* We use this as an array of size int_rels_per_ext_rel. */
169 Elf_Internal_Rela rela[1];
170 };
171
172 static int
173 elf_link_sort_cmp1 (const void *A, const void *B)
174 {
175 const struct elf_link_sort_rela *a = A;
176 const struct elf_link_sort_rela *b = B;
177 int relativea, relativeb;
178
179 relativea = a->type == reloc_class_relative;
180 relativeb = b->type == reloc_class_relative;
181
182 if (relativea < relativeb)
183 return 1;
184 if (relativea > relativeb)
185 return -1;
186 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
187 return -1;
188 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
189 return 1;
190 if (a->rela->r_offset < b->rela->r_offset)
191 return -1;
192 if (a->rela->r_offset > b->rela->r_offset)
193 return 1;
194 return 0;
195 }
196
197 static int
198 elf_link_sort_cmp2 (const void *A, const void *B)
199 {
200 const struct elf_link_sort_rela *a = A;
201 const struct elf_link_sort_rela *b = B;
202 int copya, copyb;
203
204 if (a->u.offset < b->u.offset)
205 return -1;
206 if (a->u.offset > b->u.offset)
207 return 1;
208 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
209 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
210 if (copya < copyb)
211 return -1;
212 if (copya > copyb)
213 return 1;
214 if (a->rela->r_offset < b->rela->r_offset)
215 return -1;
216 if (a->rela->r_offset > b->rela->r_offset)
217 return 1;
218 return 0;
219 }
220
221 static size_t
222 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
223 {
224 asection *reldyn;
225 bfd_size_type count, size;
226 size_t i, ret, sort_elt, ext_size;
227 bfd_byte *sort, *s_non_relative, *p;
228 struct elf_link_sort_rela *sq;
229 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
230 int i2e = bed->s->int_rels_per_ext_rel;
231 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
232 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
233 struct bfd_link_order *lo;
234 bfd_vma r_sym_mask;
235
236 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
237 if (reldyn == NULL || reldyn->_raw_size == 0)
238 {
239 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
240 if (reldyn == NULL || reldyn->_raw_size == 0)
241 return 0;
242 ext_size = bed->s->sizeof_rel;
243 swap_in = bed->s->swap_reloc_in;
244 swap_out = bed->s->swap_reloc_out;
245 }
246 else
247 {
248 ext_size = bed->s->sizeof_rela;
249 swap_in = bed->s->swap_reloca_in;
250 swap_out = bed->s->swap_reloca_out;
251 }
252 count = reldyn->_raw_size / ext_size;
253
254 size = 0;
255 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
256 if (lo->type == bfd_indirect_link_order)
257 {
258 asection *o = lo->u.indirect.section;
259 size += o->_raw_size;
260 }
261
262 if (size != reldyn->_raw_size)
263 return 0;
264
265 sort_elt = (sizeof (struct elf_link_sort_rela)
266 + (i2e - 1) * sizeof (Elf_Internal_Rela));
267 sort = bfd_zmalloc (sort_elt * count);
268 if (sort == NULL)
269 {
270 (*info->callbacks->warning)
271 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
272 return 0;
273 }
274
275 if (bed->s->arch_size == 32)
276 r_sym_mask = ~(bfd_vma) 0xff;
277 else
278 r_sym_mask = ~(bfd_vma) 0xffffffff;
279
280 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
281 if (lo->type == bfd_indirect_link_order)
282 {
283 bfd_byte *erel, *erelend;
284 asection *o = lo->u.indirect.section;
285
286 erel = o->contents;
287 erelend = o->contents + o->_raw_size;
288 p = sort + o->output_offset / ext_size * sort_elt;
289 while (erel < erelend)
290 {
291 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
292 (*swap_in) (abfd, erel, s->rela);
293 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
294 s->u.sym_mask = r_sym_mask;
295 p += sort_elt;
296 erel += ext_size;
297 }
298 }
299
300 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
301
302 for (i = 0, p = sort; i < count; i++, p += sort_elt)
303 {
304 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
305 if (s->type != reloc_class_relative)
306 break;
307 }
308 ret = i;
309 s_non_relative = p;
310
311 sq = (struct elf_link_sort_rela *) s_non_relative;
312 for (; i < count; i++, p += sort_elt)
313 {
314 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
315 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
316 sq = sp;
317 sp->u.offset = sq->rela->r_offset;
318 }
319
320 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
321
322 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
323 if (lo->type == bfd_indirect_link_order)
324 {
325 bfd_byte *erel, *erelend;
326 asection *o = lo->u.indirect.section;
327
328 erel = o->contents;
329 erelend = o->contents + o->_raw_size;
330 p = sort + o->output_offset / ext_size * sort_elt;
331 while (erel < erelend)
332 {
333 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
334 (*swap_out) (abfd, s->rela, erel);
335 p += sort_elt;
336 erel += ext_size;
337 }
338 }
339
340 free (sort);
341 *psec = reldyn;
342 return ret;
343 }
344
345 /* Do the final step of an ELF link. */
346
347 bfd_boolean
348 elf_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
349 {
350 bfd_boolean dynamic;
351 bfd_boolean emit_relocs;
352 bfd *dynobj;
353 struct elf_final_link_info finfo;
354 register asection *o;
355 register struct bfd_link_order *p;
356 register bfd *sub;
357 bfd_size_type max_contents_size;
358 bfd_size_type max_external_reloc_size;
359 bfd_size_type max_internal_reloc_count;
360 bfd_size_type max_sym_count;
361 bfd_size_type max_sym_shndx_count;
362 file_ptr off;
363 Elf_Internal_Sym elfsym;
364 unsigned int i;
365 Elf_Internal_Shdr *symtab_hdr;
366 Elf_Internal_Shdr *symtab_shndx_hdr;
367 Elf_Internal_Shdr *symstrtab_hdr;
368 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
369 struct elf_outext_info eoinfo;
370 bfd_boolean merged;
371 size_t relativecount = 0;
372 asection *reldyn = 0;
373 bfd_size_type amt;
374
375 if (! is_elf_hash_table (info->hash))
376 return FALSE;
377
378 if (info->shared)
379 abfd->flags |= DYNAMIC;
380
381 dynamic = elf_hash_table (info)->dynamic_sections_created;
382 dynobj = elf_hash_table (info)->dynobj;
383
384 emit_relocs = (info->relocatable
385 || info->emitrelocations
386 || bed->elf_backend_emit_relocs);
387
388 finfo.info = info;
389 finfo.output_bfd = abfd;
390 finfo.symstrtab = _bfd_elf_stringtab_init ();
391 if (finfo.symstrtab == NULL)
392 return FALSE;
393
394 if (! dynamic)
395 {
396 finfo.dynsym_sec = NULL;
397 finfo.hash_sec = NULL;
398 finfo.symver_sec = NULL;
399 }
400 else
401 {
402 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
403 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
404 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
405 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
406 /* Note that it is OK if symver_sec is NULL. */
407 }
408
409 finfo.contents = NULL;
410 finfo.external_relocs = NULL;
411 finfo.internal_relocs = NULL;
412 finfo.external_syms = NULL;
413 finfo.locsym_shndx = NULL;
414 finfo.internal_syms = NULL;
415 finfo.indices = NULL;
416 finfo.sections = NULL;
417 finfo.symbuf = NULL;
418 finfo.symshndxbuf = NULL;
419 finfo.symbuf_count = 0;
420 finfo.shndxbuf_size = 0;
421
422 /* Count up the number of relocations we will output for each output
423 section, so that we know the sizes of the reloc sections. We
424 also figure out some maximum sizes. */
425 max_contents_size = 0;
426 max_external_reloc_size = 0;
427 max_internal_reloc_count = 0;
428 max_sym_count = 0;
429 max_sym_shndx_count = 0;
430 merged = FALSE;
431 for (o = abfd->sections; o != NULL; o = o->next)
432 {
433 struct bfd_elf_section_data *esdo = elf_section_data (o);
434 o->reloc_count = 0;
435
436 for (p = o->link_order_head; p != NULL; p = p->next)
437 {
438 unsigned int reloc_count = 0;
439 struct bfd_elf_section_data *esdi = NULL;
440 unsigned int *rel_count1;
441
442 if (p->type == bfd_section_reloc_link_order
443 || p->type == bfd_symbol_reloc_link_order)
444 reloc_count = 1;
445 else if (p->type == bfd_indirect_link_order)
446 {
447 asection *sec;
448
449 sec = p->u.indirect.section;
450 esdi = elf_section_data (sec);
451
452 /* Mark all sections which are to be included in the
453 link. This will normally be every section. We need
454 to do this so that we can identify any sections which
455 the linker has decided to not include. */
456 sec->linker_mark = TRUE;
457
458 if (sec->flags & SEC_MERGE)
459 merged = TRUE;
460
461 if (info->relocatable || info->emitrelocations)
462 reloc_count = sec->reloc_count;
463 else if (bed->elf_backend_count_relocs)
464 {
465 Elf_Internal_Rela * relocs;
466
467 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
468 info->keep_memory);
469
470 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
471
472 if (elf_section_data (o)->relocs != relocs)
473 free (relocs);
474 }
475
476 if (sec->_raw_size > max_contents_size)
477 max_contents_size = sec->_raw_size;
478 if (sec->_cooked_size > max_contents_size)
479 max_contents_size = sec->_cooked_size;
480
481 /* We are interested in just local symbols, not all
482 symbols. */
483 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
484 && (sec->owner->flags & DYNAMIC) == 0)
485 {
486 size_t sym_count;
487
488 if (elf_bad_symtab (sec->owner))
489 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
490 / bed->s->sizeof_sym);
491 else
492 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
493
494 if (sym_count > max_sym_count)
495 max_sym_count = sym_count;
496
497 if (sym_count > max_sym_shndx_count
498 && elf_symtab_shndx (sec->owner) != 0)
499 max_sym_shndx_count = sym_count;
500
501 if ((sec->flags & SEC_RELOC) != 0)
502 {
503 size_t ext_size;
504
505 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
506 if (ext_size > max_external_reloc_size)
507 max_external_reloc_size = ext_size;
508 if (sec->reloc_count > max_internal_reloc_count)
509 max_internal_reloc_count = sec->reloc_count;
510 }
511 }
512 }
513
514 if (reloc_count == 0)
515 continue;
516
517 o->reloc_count += reloc_count;
518
519 /* MIPS may have a mix of REL and RELA relocs on sections.
520 To support this curious ABI we keep reloc counts in
521 elf_section_data too. We must be careful to add the
522 relocations from the input section to the right output
523 count. FIXME: Get rid of one count. We have
524 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
525 rel_count1 = &esdo->rel_count;
526 if (esdi != NULL)
527 {
528 bfd_boolean same_size;
529 bfd_size_type entsize1;
530
531 entsize1 = esdi->rel_hdr.sh_entsize;
532 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
533 || entsize1 == bed->s->sizeof_rela);
534 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
535
536 if (!same_size)
537 rel_count1 = &esdo->rel_count2;
538
539 if (esdi->rel_hdr2 != NULL)
540 {
541 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
542 unsigned int alt_count;
543 unsigned int *rel_count2;
544
545 BFD_ASSERT (entsize2 != entsize1
546 && (entsize2 == bed->s->sizeof_rel
547 || entsize2 == bed->s->sizeof_rela));
548
549 rel_count2 = &esdo->rel_count2;
550 if (!same_size)
551 rel_count2 = &esdo->rel_count;
552
553 /* The following is probably too simplistic if the
554 backend counts output relocs unusually. */
555 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
556 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
557 *rel_count2 += alt_count;
558 reloc_count -= alt_count;
559 }
560 }
561 *rel_count1 += reloc_count;
562 }
563
564 if (o->reloc_count > 0)
565 o->flags |= SEC_RELOC;
566 else
567 {
568 /* Explicitly clear the SEC_RELOC flag. The linker tends to
569 set it (this is probably a bug) and if it is set
570 assign_section_numbers will create a reloc section. */
571 o->flags &=~ SEC_RELOC;
572 }
573
574 /* If the SEC_ALLOC flag is not set, force the section VMA to
575 zero. This is done in elf_fake_sections as well, but forcing
576 the VMA to 0 here will ensure that relocs against these
577 sections are handled correctly. */
578 if ((o->flags & SEC_ALLOC) == 0
579 && ! o->user_set_vma)
580 o->vma = 0;
581 }
582
583 if (! info->relocatable && merged)
584 elf_link_hash_traverse (elf_hash_table (info),
585 _bfd_elf_link_sec_merge_syms, abfd);
586
587 /* Figure out the file positions for everything but the symbol table
588 and the relocs. We set symcount to force assign_section_numbers
589 to create a symbol table. */
590 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
591 BFD_ASSERT (! abfd->output_has_begun);
592 if (! _bfd_elf_compute_section_file_positions (abfd, info))
593 goto error_return;
594
595 /* That created the reloc sections. Set their sizes, and assign
596 them file positions, and allocate some buffers. */
597 for (o = abfd->sections; o != NULL; o = o->next)
598 {
599 if ((o->flags & SEC_RELOC) != 0)
600 {
601 if (!(_bfd_elf_link_size_reloc_section
602 (abfd, &elf_section_data (o)->rel_hdr, o)))
603 goto error_return;
604
605 if (elf_section_data (o)->rel_hdr2
606 && !(_bfd_elf_link_size_reloc_section
607 (abfd, elf_section_data (o)->rel_hdr2, o)))
608 goto error_return;
609 }
610
611 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
612 to count upwards while actually outputting the relocations. */
613 elf_section_data (o)->rel_count = 0;
614 elf_section_data (o)->rel_count2 = 0;
615 }
616
617 _bfd_elf_assign_file_positions_for_relocs (abfd);
618
619 /* We have now assigned file positions for all the sections except
620 .symtab and .strtab. We start the .symtab section at the current
621 file position, and write directly to it. We build the .strtab
622 section in memory. */
623 bfd_get_symcount (abfd) = 0;
624 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
625 /* sh_name is set in prep_headers. */
626 symtab_hdr->sh_type = SHT_SYMTAB;
627 /* sh_flags, sh_addr and sh_size all start off zero. */
628 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
629 /* sh_link is set in assign_section_numbers. */
630 /* sh_info is set below. */
631 /* sh_offset is set just below. */
632 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
633
634 off = elf_tdata (abfd)->next_file_pos;
635 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
636
637 /* Note that at this point elf_tdata (abfd)->next_file_pos is
638 incorrect. We do not yet know the size of the .symtab section.
639 We correct next_file_pos below, after we do know the size. */
640
641 /* Allocate a buffer to hold swapped out symbols. This is to avoid
642 continuously seeking to the right position in the file. */
643 if (! info->keep_memory || max_sym_count < 20)
644 finfo.symbuf_size = 20;
645 else
646 finfo.symbuf_size = max_sym_count;
647 amt = finfo.symbuf_size;
648 amt *= bed->s->sizeof_sym;
649 finfo.symbuf = bfd_malloc (amt);
650 if (finfo.symbuf == NULL)
651 goto error_return;
652 if (elf_numsections (abfd) > SHN_LORESERVE)
653 {
654 /* Wild guess at number of output symbols. realloc'd as needed. */
655 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
656 finfo.shndxbuf_size = amt;
657 amt *= sizeof (Elf_External_Sym_Shndx);
658 finfo.symshndxbuf = bfd_zmalloc (amt);
659 if (finfo.symshndxbuf == NULL)
660 goto error_return;
661 }
662
663 /* Start writing out the symbol table. The first symbol is always a
664 dummy symbol. */
665 if (info->strip != strip_all
666 || emit_relocs)
667 {
668 elfsym.st_value = 0;
669 elfsym.st_size = 0;
670 elfsym.st_info = 0;
671 elfsym.st_other = 0;
672 elfsym.st_shndx = SHN_UNDEF;
673 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
674 NULL))
675 goto error_return;
676 }
677
678 #if 0
679 /* Some standard ELF linkers do this, but we don't because it causes
680 bootstrap comparison failures. */
681 /* Output a file symbol for the output file as the second symbol.
682 We output this even if we are discarding local symbols, although
683 I'm not sure if this is correct. */
684 elfsym.st_value = 0;
685 elfsym.st_size = 0;
686 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
687 elfsym.st_other = 0;
688 elfsym.st_shndx = SHN_ABS;
689 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
690 &elfsym, bfd_abs_section_ptr, NULL))
691 goto error_return;
692 #endif
693
694 /* Output a symbol for each section. We output these even if we are
695 discarding local symbols, since they are used for relocs. These
696 symbols have no names. We store the index of each one in the
697 index field of the section, so that we can find it again when
698 outputting relocs. */
699 if (info->strip != strip_all
700 || emit_relocs)
701 {
702 elfsym.st_size = 0;
703 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
704 elfsym.st_other = 0;
705 for (i = 1; i < elf_numsections (abfd); i++)
706 {
707 o = bfd_section_from_elf_index (abfd, i);
708 if (o != NULL)
709 o->target_index = bfd_get_symcount (abfd);
710 elfsym.st_shndx = i;
711 if (info->relocatable || o == NULL)
712 elfsym.st_value = 0;
713 else
714 elfsym.st_value = o->vma;
715 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
716 goto error_return;
717 if (i == SHN_LORESERVE - 1)
718 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
719 }
720 }
721
722 /* Allocate some memory to hold information read in from the input
723 files. */
724 if (max_contents_size != 0)
725 {
726 finfo.contents = bfd_malloc (max_contents_size);
727 if (finfo.contents == NULL)
728 goto error_return;
729 }
730
731 if (max_external_reloc_size != 0)
732 {
733 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
734 if (finfo.external_relocs == NULL)
735 goto error_return;
736 }
737
738 if (max_internal_reloc_count != 0)
739 {
740 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
741 amt *= sizeof (Elf_Internal_Rela);
742 finfo.internal_relocs = bfd_malloc (amt);
743 if (finfo.internal_relocs == NULL)
744 goto error_return;
745 }
746
747 if (max_sym_count != 0)
748 {
749 amt = max_sym_count * bed->s->sizeof_sym;
750 finfo.external_syms = bfd_malloc (amt);
751 if (finfo.external_syms == NULL)
752 goto error_return;
753
754 amt = max_sym_count * sizeof (Elf_Internal_Sym);
755 finfo.internal_syms = bfd_malloc (amt);
756 if (finfo.internal_syms == NULL)
757 goto error_return;
758
759 amt = max_sym_count * sizeof (long);
760 finfo.indices = bfd_malloc (amt);
761 if (finfo.indices == NULL)
762 goto error_return;
763
764 amt = max_sym_count * sizeof (asection *);
765 finfo.sections = bfd_malloc (amt);
766 if (finfo.sections == NULL)
767 goto error_return;
768 }
769
770 if (max_sym_shndx_count != 0)
771 {
772 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
773 finfo.locsym_shndx = bfd_malloc (amt);
774 if (finfo.locsym_shndx == NULL)
775 goto error_return;
776 }
777
778 if (elf_hash_table (info)->tls_sec)
779 {
780 bfd_vma base, end = 0;
781 asection *sec;
782
783 for (sec = elf_hash_table (info)->tls_sec;
784 sec && (sec->flags & SEC_THREAD_LOCAL);
785 sec = sec->next)
786 {
787 bfd_vma size = sec->_raw_size;
788
789 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
790 {
791 struct bfd_link_order *o;
792
793 for (o = sec->link_order_head; o != NULL; o = o->next)
794 if (size < o->offset + o->size)
795 size = o->offset + o->size;
796 }
797 end = sec->vma + size;
798 }
799 base = elf_hash_table (info)->tls_sec->vma;
800 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
801 elf_hash_table (info)->tls_size = end - base;
802 }
803
804 /* Since ELF permits relocations to be against local symbols, we
805 must have the local symbols available when we do the relocations.
806 Since we would rather only read the local symbols once, and we
807 would rather not keep them in memory, we handle all the
808 relocations for a single input file at the same time.
809
810 Unfortunately, there is no way to know the total number of local
811 symbols until we have seen all of them, and the local symbol
812 indices precede the global symbol indices. This means that when
813 we are generating relocatable output, and we see a reloc against
814 a global symbol, we can not know the symbol index until we have
815 finished examining all the local symbols to see which ones we are
816 going to output. To deal with this, we keep the relocations in
817 memory, and don't output them until the end of the link. This is
818 an unfortunate waste of memory, but I don't see a good way around
819 it. Fortunately, it only happens when performing a relocatable
820 link, which is not the common case. FIXME: If keep_memory is set
821 we could write the relocs out and then read them again; I don't
822 know how bad the memory loss will be. */
823
824 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
825 sub->output_has_begun = FALSE;
826 for (o = abfd->sections; o != NULL; o = o->next)
827 {
828 for (p = o->link_order_head; p != NULL; p = p->next)
829 {
830 if (p->type == bfd_indirect_link_order
831 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
832 == bfd_target_elf_flavour)
833 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
834 {
835 if (! sub->output_has_begun)
836 {
837 if (! elf_link_input_bfd (&finfo, sub))
838 goto error_return;
839 sub->output_has_begun = TRUE;
840 }
841 }
842 else if (p->type == bfd_section_reloc_link_order
843 || p->type == bfd_symbol_reloc_link_order)
844 {
845 if (! elf_reloc_link_order (abfd, info, o, p))
846 goto error_return;
847 }
848 else
849 {
850 if (! _bfd_default_link_order (abfd, info, o, p))
851 goto error_return;
852 }
853 }
854 }
855
856 /* Output any global symbols that got converted to local in a
857 version script or due to symbol visibility. We do this in a
858 separate step since ELF requires all local symbols to appear
859 prior to any global symbols. FIXME: We should only do this if
860 some global symbols were, in fact, converted to become local.
861 FIXME: Will this work correctly with the Irix 5 linker? */
862 eoinfo.failed = FALSE;
863 eoinfo.finfo = &finfo;
864 eoinfo.localsyms = TRUE;
865 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
866 &eoinfo);
867 if (eoinfo.failed)
868 return FALSE;
869
870 /* That wrote out all the local symbols. Finish up the symbol table
871 with the global symbols. Even if we want to strip everything we
872 can, we still need to deal with those global symbols that got
873 converted to local in a version script. */
874
875 /* The sh_info field records the index of the first non local symbol. */
876 symtab_hdr->sh_info = bfd_get_symcount (abfd);
877
878 if (dynamic
879 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
880 {
881 Elf_Internal_Sym sym;
882 bfd_byte *dynsym = finfo.dynsym_sec->contents;
883 long last_local = 0;
884
885 /* Write out the section symbols for the output sections. */
886 if (info->shared)
887 {
888 asection *s;
889
890 sym.st_size = 0;
891 sym.st_name = 0;
892 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
893 sym.st_other = 0;
894
895 for (s = abfd->sections; s != NULL; s = s->next)
896 {
897 int indx;
898 bfd_byte *dest;
899 long dynindx;
900
901 indx = elf_section_data (s)->this_idx;
902 dynindx = elf_section_data (s)->dynindx;
903 BFD_ASSERT (indx > 0);
904 sym.st_shndx = indx;
905 sym.st_value = s->vma;
906 dest = dynsym + dynindx * bed->s->sizeof_sym;
907 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
908 }
909
910 last_local = bfd_count_sections (abfd);
911 }
912
913 /* Write out the local dynsyms. */
914 if (elf_hash_table (info)->dynlocal)
915 {
916 struct elf_link_local_dynamic_entry *e;
917 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
918 {
919 asection *s;
920 bfd_byte *dest;
921
922 sym.st_size = e->isym.st_size;
923 sym.st_other = e->isym.st_other;
924
925 /* Copy the internal symbol as is.
926 Note that we saved a word of storage and overwrote
927 the original st_name with the dynstr_index. */
928 sym = e->isym;
929
930 if (e->isym.st_shndx != SHN_UNDEF
931 && (e->isym.st_shndx < SHN_LORESERVE
932 || e->isym.st_shndx > SHN_HIRESERVE))
933 {
934 s = bfd_section_from_elf_index (e->input_bfd,
935 e->isym.st_shndx);
936
937 sym.st_shndx =
938 elf_section_data (s->output_section)->this_idx;
939 sym.st_value = (s->output_section->vma
940 + s->output_offset
941 + e->isym.st_value);
942 }
943
944 if (last_local < e->dynindx)
945 last_local = e->dynindx;
946
947 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
948 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
949 }
950 }
951
952 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
953 last_local + 1;
954 }
955
956 /* We get the global symbols from the hash table. */
957 eoinfo.failed = FALSE;
958 eoinfo.localsyms = FALSE;
959 eoinfo.finfo = &finfo;
960 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
961 &eoinfo);
962 if (eoinfo.failed)
963 return FALSE;
964
965 /* If backend needs to output some symbols not present in the hash
966 table, do it now. */
967 if (bed->elf_backend_output_arch_syms)
968 {
969 typedef bfd_boolean (*out_sym_func)
970 (void *, const char *, Elf_Internal_Sym *, asection *,
971 struct elf_link_hash_entry *);
972
973 if (! ((*bed->elf_backend_output_arch_syms)
974 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
975 return FALSE;
976 }
977
978 /* Flush all symbols to the file. */
979 if (! elf_link_flush_output_syms (&finfo, bed))
980 return FALSE;
981
982 /* Now we know the size of the symtab section. */
983 off += symtab_hdr->sh_size;
984
985 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
986 if (symtab_shndx_hdr->sh_name != 0)
987 {
988 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
989 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
990 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
991 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
992 symtab_shndx_hdr->sh_size = amt;
993
994 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
995 off, TRUE);
996
997 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
998 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
999 return FALSE;
1000 }
1001
1002
1003 /* Finish up and write out the symbol string table (.strtab)
1004 section. */
1005 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
1006 /* sh_name was set in prep_headers. */
1007 symstrtab_hdr->sh_type = SHT_STRTAB;
1008 symstrtab_hdr->sh_flags = 0;
1009 symstrtab_hdr->sh_addr = 0;
1010 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
1011 symstrtab_hdr->sh_entsize = 0;
1012 symstrtab_hdr->sh_link = 0;
1013 symstrtab_hdr->sh_info = 0;
1014 /* sh_offset is set just below. */
1015 symstrtab_hdr->sh_addralign = 1;
1016
1017 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
1018 elf_tdata (abfd)->next_file_pos = off;
1019
1020 if (bfd_get_symcount (abfd) > 0)
1021 {
1022 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
1023 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
1024 return FALSE;
1025 }
1026
1027 /* Adjust the relocs to have the correct symbol indices. */
1028 for (o = abfd->sections; o != NULL; o = o->next)
1029 {
1030 if ((o->flags & SEC_RELOC) == 0)
1031 continue;
1032
1033 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
1034 elf_section_data (o)->rel_count,
1035 elf_section_data (o)->rel_hashes);
1036 if (elf_section_data (o)->rel_hdr2 != NULL)
1037 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
1038 elf_section_data (o)->rel_count2,
1039 (elf_section_data (o)->rel_hashes
1040 + elf_section_data (o)->rel_count));
1041
1042 /* Set the reloc_count field to 0 to prevent write_relocs from
1043 trying to swap the relocs out itself. */
1044 o->reloc_count = 0;
1045 }
1046
1047 if (dynamic && info->combreloc && dynobj != NULL)
1048 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
1049
1050 /* If we are linking against a dynamic object, or generating a
1051 shared library, finish up the dynamic linking information. */
1052 if (dynamic)
1053 {
1054 bfd_byte *dyncon, *dynconend;
1055
1056 /* Fix up .dynamic entries. */
1057 o = bfd_get_section_by_name (dynobj, ".dynamic");
1058 BFD_ASSERT (o != NULL);
1059
1060 dyncon = o->contents;
1061 dynconend = o->contents + o->_raw_size;
1062 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
1063 {
1064 Elf_Internal_Dyn dyn;
1065 const char *name;
1066 unsigned int type;
1067
1068 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
1069
1070 switch (dyn.d_tag)
1071 {
1072 default:
1073 continue;
1074 case DT_NULL:
1075 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
1076 {
1077 switch (elf_section_data (reldyn)->this_hdr.sh_type)
1078 {
1079 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
1080 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
1081 default: continue;
1082 }
1083 dyn.d_un.d_val = relativecount;
1084 relativecount = 0;
1085 break;
1086 }
1087 continue;
1088
1089 case DT_INIT:
1090 name = info->init_function;
1091 goto get_sym;
1092 case DT_FINI:
1093 name = info->fini_function;
1094 get_sym:
1095 {
1096 struct elf_link_hash_entry *h;
1097
1098 h = elf_link_hash_lookup (elf_hash_table (info), name,
1099 FALSE, FALSE, TRUE);
1100 if (h != NULL
1101 && (h->root.type == bfd_link_hash_defined
1102 || h->root.type == bfd_link_hash_defweak))
1103 {
1104 dyn.d_un.d_val = h->root.u.def.value;
1105 o = h->root.u.def.section;
1106 if (o->output_section != NULL)
1107 dyn.d_un.d_val += (o->output_section->vma
1108 + o->output_offset);
1109 else
1110 {
1111 /* The symbol is imported from another shared
1112 library and does not apply to this one. */
1113 dyn.d_un.d_val = 0;
1114 }
1115 break;
1116 }
1117 }
1118 continue;
1119
1120 case DT_PREINIT_ARRAYSZ:
1121 name = ".preinit_array";
1122 goto get_size;
1123 case DT_INIT_ARRAYSZ:
1124 name = ".init_array";
1125 goto get_size;
1126 case DT_FINI_ARRAYSZ:
1127 name = ".fini_array";
1128 get_size:
1129 o = bfd_get_section_by_name (abfd, name);
1130 if (o == NULL)
1131 {
1132 (*_bfd_error_handler)
1133 (_("%s: could not find output section %s"),
1134 bfd_get_filename (abfd), name);
1135 goto error_return;
1136 }
1137 if (o->_raw_size == 0)
1138 (*_bfd_error_handler)
1139 (_("warning: %s section has zero size"), name);
1140 dyn.d_un.d_val = o->_raw_size;
1141 break;
1142
1143 case DT_PREINIT_ARRAY:
1144 name = ".preinit_array";
1145 goto get_vma;
1146 case DT_INIT_ARRAY:
1147 name = ".init_array";
1148 goto get_vma;
1149 case DT_FINI_ARRAY:
1150 name = ".fini_array";
1151 goto get_vma;
1152
1153 case DT_HASH:
1154 name = ".hash";
1155 goto get_vma;
1156 case DT_STRTAB:
1157 name = ".dynstr";
1158 goto get_vma;
1159 case DT_SYMTAB:
1160 name = ".dynsym";
1161 goto get_vma;
1162 case DT_VERDEF:
1163 name = ".gnu.version_d";
1164 goto get_vma;
1165 case DT_VERNEED:
1166 name = ".gnu.version_r";
1167 goto get_vma;
1168 case DT_VERSYM:
1169 name = ".gnu.version";
1170 get_vma:
1171 o = bfd_get_section_by_name (abfd, name);
1172 if (o == NULL)
1173 {
1174 (*_bfd_error_handler)
1175 (_("%s: could not find output section %s"),
1176 bfd_get_filename (abfd), name);
1177 goto error_return;
1178 }
1179 dyn.d_un.d_ptr = o->vma;
1180 break;
1181
1182 case DT_REL:
1183 case DT_RELA:
1184 case DT_RELSZ:
1185 case DT_RELASZ:
1186 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
1187 type = SHT_REL;
1188 else
1189 type = SHT_RELA;
1190 dyn.d_un.d_val = 0;
1191 for (i = 1; i < elf_numsections (abfd); i++)
1192 {
1193 Elf_Internal_Shdr *hdr;
1194
1195 hdr = elf_elfsections (abfd)[i];
1196 if (hdr->sh_type == type
1197 && (hdr->sh_flags & SHF_ALLOC) != 0)
1198 {
1199 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
1200 dyn.d_un.d_val += hdr->sh_size;
1201 else
1202 {
1203 if (dyn.d_un.d_val == 0
1204 || hdr->sh_addr < dyn.d_un.d_val)
1205 dyn.d_un.d_val = hdr->sh_addr;
1206 }
1207 }
1208 }
1209 break;
1210 }
1211 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
1212 }
1213 }
1214
1215 /* If we have created any dynamic sections, then output them. */
1216 if (dynobj != NULL)
1217 {
1218 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
1219 goto error_return;
1220
1221 for (o = dynobj->sections; o != NULL; o = o->next)
1222 {
1223 if ((o->flags & SEC_HAS_CONTENTS) == 0
1224 || o->_raw_size == 0
1225 || o->output_section == bfd_abs_section_ptr)
1226 continue;
1227 if ((o->flags & SEC_LINKER_CREATED) == 0)
1228 {
1229 /* At this point, we are only interested in sections
1230 created by _bfd_elf_link_create_dynamic_sections. */
1231 continue;
1232 }
1233 if ((elf_section_data (o->output_section)->this_hdr.sh_type
1234 != SHT_STRTAB)
1235 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
1236 {
1237 if (! bfd_set_section_contents (abfd, o->output_section,
1238 o->contents,
1239 (file_ptr) o->output_offset,
1240 o->_raw_size))
1241 goto error_return;
1242 }
1243 else
1244 {
1245 /* The contents of the .dynstr section are actually in a
1246 stringtab. */
1247 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
1248 if (bfd_seek (abfd, off, SEEK_SET) != 0
1249 || ! _bfd_elf_strtab_emit (abfd,
1250 elf_hash_table (info)->dynstr))
1251 goto error_return;
1252 }
1253 }
1254 }
1255
1256 if (info->relocatable)
1257 {
1258 bfd_boolean failed = FALSE;
1259
1260 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
1261 if (failed)
1262 goto error_return;
1263 }
1264
1265 /* If we have optimized stabs strings, output them. */
1266 if (elf_hash_table (info)->stab_info != NULL)
1267 {
1268 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
1269 goto error_return;
1270 }
1271
1272 if (info->eh_frame_hdr)
1273 {
1274 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
1275 goto error_return;
1276 }
1277
1278 if (finfo.symstrtab != NULL)
1279 _bfd_stringtab_free (finfo.symstrtab);
1280 if (finfo.contents != NULL)
1281 free (finfo.contents);
1282 if (finfo.external_relocs != NULL)
1283 free (finfo.external_relocs);
1284 if (finfo.internal_relocs != NULL)
1285 free (finfo.internal_relocs);
1286 if (finfo.external_syms != NULL)
1287 free (finfo.external_syms);
1288 if (finfo.locsym_shndx != NULL)
1289 free (finfo.locsym_shndx);
1290 if (finfo.internal_syms != NULL)
1291 free (finfo.internal_syms);
1292 if (finfo.indices != NULL)
1293 free (finfo.indices);
1294 if (finfo.sections != NULL)
1295 free (finfo.sections);
1296 if (finfo.symbuf != NULL)
1297 free (finfo.symbuf);
1298 if (finfo.symshndxbuf != NULL)
1299 free (finfo.symshndxbuf);
1300 for (o = abfd->sections; o != NULL; o = o->next)
1301 {
1302 if ((o->flags & SEC_RELOC) != 0
1303 && elf_section_data (o)->rel_hashes != NULL)
1304 free (elf_section_data (o)->rel_hashes);
1305 }
1306
1307 elf_tdata (abfd)->linker = TRUE;
1308
1309 return TRUE;
1310
1311 error_return:
1312 if (finfo.symstrtab != NULL)
1313 _bfd_stringtab_free (finfo.symstrtab);
1314 if (finfo.contents != NULL)
1315 free (finfo.contents);
1316 if (finfo.external_relocs != NULL)
1317 free (finfo.external_relocs);
1318 if (finfo.internal_relocs != NULL)
1319 free (finfo.internal_relocs);
1320 if (finfo.external_syms != NULL)
1321 free (finfo.external_syms);
1322 if (finfo.locsym_shndx != NULL)
1323 free (finfo.locsym_shndx);
1324 if (finfo.internal_syms != NULL)
1325 free (finfo.internal_syms);
1326 if (finfo.indices != NULL)
1327 free (finfo.indices);
1328 if (finfo.sections != NULL)
1329 free (finfo.sections);
1330 if (finfo.symbuf != NULL)
1331 free (finfo.symbuf);
1332 if (finfo.symshndxbuf != NULL)
1333 free (finfo.symshndxbuf);
1334 for (o = abfd->sections; o != NULL; o = o->next)
1335 {
1336 if ((o->flags & SEC_RELOC) != 0
1337 && elf_section_data (o)->rel_hashes != NULL)
1338 free (elf_section_data (o)->rel_hashes);
1339 }
1340
1341 return FALSE;
1342 }
1343
1344 /* Add a symbol to the output symbol table. */
1345
1346 static bfd_boolean
1347 elf_link_output_sym (struct elf_final_link_info *finfo,
1348 const char *name,
1349 Elf_Internal_Sym *elfsym,
1350 asection *input_sec,
1351 struct elf_link_hash_entry *h)
1352 {
1353 bfd_byte *dest;
1354 Elf_External_Sym_Shndx *destshndx;
1355 bfd_boolean (*output_symbol_hook)
1356 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
1357 struct elf_link_hash_entry *);
1358 const struct elf_backend_data *bed;
1359
1360 bed = get_elf_backend_data (finfo->output_bfd);
1361 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
1362 if (output_symbol_hook != NULL)
1363 {
1364 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
1365 return FALSE;
1366 }
1367
1368 if (name == NULL || *name == '\0')
1369 elfsym->st_name = 0;
1370 else if (input_sec->flags & SEC_EXCLUDE)
1371 elfsym->st_name = 0;
1372 else
1373 {
1374 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
1375 name, TRUE, FALSE);
1376 if (elfsym->st_name == (unsigned long) -1)
1377 return FALSE;
1378 }
1379
1380 if (finfo->symbuf_count >= finfo->symbuf_size)
1381 {
1382 if (! elf_link_flush_output_syms (finfo, bed))
1383 return FALSE;
1384 }
1385
1386 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
1387 destshndx = finfo->symshndxbuf;
1388 if (destshndx != NULL)
1389 {
1390 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
1391 {
1392 bfd_size_type amt;
1393
1394 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
1395 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
1396 if (destshndx == NULL)
1397 return FALSE;
1398 memset ((char *) destshndx + amt, 0, amt);
1399 finfo->shndxbuf_size *= 2;
1400 }
1401 destshndx += bfd_get_symcount (finfo->output_bfd);
1402 }
1403
1404 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
1405 finfo->symbuf_count += 1;
1406 bfd_get_symcount (finfo->output_bfd) += 1;
1407
1408 return TRUE;
1409 }
1410
1411 /* Flush the output symbols to the file. */
1412
1413 static bfd_boolean
1414 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
1415 const struct elf_backend_data *bed)
1416 {
1417 if (finfo->symbuf_count > 0)
1418 {
1419 Elf_Internal_Shdr *hdr;
1420 file_ptr pos;
1421 bfd_size_type amt;
1422
1423 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
1424 pos = hdr->sh_offset + hdr->sh_size;
1425 amt = finfo->symbuf_count * bed->s->sizeof_sym;
1426 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
1427 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
1428 return FALSE;
1429
1430 hdr->sh_size += amt;
1431 finfo->symbuf_count = 0;
1432 }
1433
1434 return TRUE;
1435 }
1436
1437 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
1438 allowing an unsatisfied unversioned symbol in the DSO to match a
1439 versioned symbol that would normally require an explicit version.
1440 We also handle the case that a DSO references a hidden symbol
1441 which may be satisfied by a versioned symbol in another DSO. */
1442
1443 static bfd_boolean
1444 elf_link_check_versioned_symbol (struct bfd_link_info *info,
1445 const struct elf_backend_data *bed,
1446 struct elf_link_hash_entry *h)
1447 {
1448 bfd *abfd;
1449 struct elf_link_loaded_list *loaded;
1450
1451 if (!is_elf_hash_table (info->hash))
1452 return FALSE;
1453
1454 switch (h->root.type)
1455 {
1456 default:
1457 abfd = NULL;
1458 break;
1459
1460 case bfd_link_hash_undefined:
1461 case bfd_link_hash_undefweak:
1462 abfd = h->root.u.undef.abfd;
1463 if ((abfd->flags & DYNAMIC) == 0
1464 || elf_dyn_lib_class (abfd) != DYN_DT_NEEDED)
1465 return FALSE;
1466 break;
1467
1468 case bfd_link_hash_defined:
1469 case bfd_link_hash_defweak:
1470 abfd = h->root.u.def.section->owner;
1471 break;
1472
1473 case bfd_link_hash_common:
1474 abfd = h->root.u.c.p->section->owner;
1475 break;
1476 }
1477 BFD_ASSERT (abfd != NULL);
1478
1479 for (loaded = elf_hash_table (info)->loaded;
1480 loaded != NULL;
1481 loaded = loaded->next)
1482 {
1483 bfd *input;
1484 Elf_Internal_Shdr *hdr;
1485 bfd_size_type symcount;
1486 bfd_size_type extsymcount;
1487 bfd_size_type extsymoff;
1488 Elf_Internal_Shdr *versymhdr;
1489 Elf_Internal_Sym *isym;
1490 Elf_Internal_Sym *isymend;
1491 Elf_Internal_Sym *isymbuf;
1492 Elf_External_Versym *ever;
1493 Elf_External_Versym *extversym;
1494
1495 input = loaded->abfd;
1496
1497 /* We check each DSO for a possible hidden versioned definition. */
1498 if (input == abfd
1499 || (input->flags & DYNAMIC) == 0
1500 || elf_dynversym (input) == 0)
1501 continue;
1502
1503 hdr = &elf_tdata (input)->dynsymtab_hdr;
1504
1505 symcount = hdr->sh_size / bed->s->sizeof_sym;
1506 if (elf_bad_symtab (input))
1507 {
1508 extsymcount = symcount;
1509 extsymoff = 0;
1510 }
1511 else
1512 {
1513 extsymcount = symcount - hdr->sh_info;
1514 extsymoff = hdr->sh_info;
1515 }
1516
1517 if (extsymcount == 0)
1518 continue;
1519
1520 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
1521 NULL, NULL, NULL);
1522 if (isymbuf == NULL)
1523 return FALSE;
1524
1525 /* Read in any version definitions. */
1526 versymhdr = &elf_tdata (input)->dynversym_hdr;
1527 extversym = bfd_malloc (versymhdr->sh_size);
1528 if (extversym == NULL)
1529 goto error_ret;
1530
1531 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
1532 || (bfd_bread (extversym, versymhdr->sh_size, input)
1533 != versymhdr->sh_size))
1534 {
1535 free (extversym);
1536 error_ret:
1537 free (isymbuf);
1538 return FALSE;
1539 }
1540
1541 ever = extversym + extsymoff;
1542 isymend = isymbuf + extsymcount;
1543 for (isym = isymbuf; isym < isymend; isym++, ever++)
1544 {
1545 const char *name;
1546 Elf_Internal_Versym iver;
1547 unsigned short version_index;
1548
1549 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
1550 || isym->st_shndx == SHN_UNDEF)
1551 continue;
1552
1553 name = bfd_elf_string_from_elf_section (input,
1554 hdr->sh_link,
1555 isym->st_name);
1556 if (strcmp (name, h->root.root.string) != 0)
1557 continue;
1558
1559 _bfd_elf_swap_versym_in (input, ever, &iver);
1560
1561 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1562 {
1563 /* If we have a non-hidden versioned sym, then it should
1564 have provided a definition for the undefined sym. */
1565 abort ();
1566 }
1567
1568 version_index = iver.vs_vers & VERSYM_VERSION;
1569 if (version_index == 1 || version_index == 2)
1570 {
1571 /* This is the base or first version. We can use it. */
1572 free (extversym);
1573 free (isymbuf);
1574 return TRUE;
1575 }
1576 }
1577
1578 free (extversym);
1579 free (isymbuf);
1580 }
1581
1582 return FALSE;
1583 }
1584
1585 /* Add an external symbol to the symbol table. This is called from
1586 the hash table traversal routine. When generating a shared object,
1587 we go through the symbol table twice. The first time we output
1588 anything that might have been forced to local scope in a version
1589 script. The second time we output the symbols that are still
1590 global symbols. */
1591
1592 static bfd_boolean
1593 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
1594 {
1595 struct elf_outext_info *eoinfo = data;
1596 struct elf_final_link_info *finfo = eoinfo->finfo;
1597 bfd_boolean strip;
1598 Elf_Internal_Sym sym;
1599 asection *input_sec;
1600 const struct elf_backend_data *bed;
1601
1602 if (h->root.type == bfd_link_hash_warning)
1603 {
1604 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1605 if (h->root.type == bfd_link_hash_new)
1606 return TRUE;
1607 }
1608
1609 /* Decide whether to output this symbol in this pass. */
1610 if (eoinfo->localsyms)
1611 {
1612 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1613 return TRUE;
1614 }
1615 else
1616 {
1617 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1618 return TRUE;
1619 }
1620
1621 bed = get_elf_backend_data (finfo->output_bfd);
1622
1623 /* If we have an undefined symbol reference here then it must have
1624 come from a shared library that is being linked in. (Undefined
1625 references in regular files have already been handled). If we
1626 are reporting errors for this situation then do so now. */
1627 if (h->root.type == bfd_link_hash_undefined
1628 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
1629 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
1630 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
1631 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1632 {
1633 if (! ((*finfo->info->callbacks->undefined_symbol)
1634 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
1635 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
1636 {
1637 eoinfo->failed = TRUE;
1638 return FALSE;
1639 }
1640 }
1641
1642 /* We should also warn if a forced local symbol is referenced from
1643 shared libraries. */
1644 if (! finfo->info->relocatable
1645 && (! finfo->info->shared)
1646 && (h->elf_link_hash_flags
1647 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
1648 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
1649 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
1650 {
1651 (*_bfd_error_handler)
1652 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
1653 bfd_get_filename (finfo->output_bfd),
1654 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
1655 ? "internal"
1656 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
1657 ? "hidden" : "local",
1658 h->root.root.string,
1659 bfd_archive_filename (h->root.u.def.section->owner));
1660 eoinfo->failed = TRUE;
1661 return FALSE;
1662 }
1663
1664 /* We don't want to output symbols that have never been mentioned by
1665 a regular file, or that we have been told to strip. However, if
1666 h->indx is set to -2, the symbol is used by a reloc and we must
1667 output it. */
1668 if (h->indx == -2)
1669 strip = FALSE;
1670 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1671 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
1672 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1673 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
1674 strip = TRUE;
1675 else if (finfo->info->strip == strip_all)
1676 strip = TRUE;
1677 else if (finfo->info->strip == strip_some
1678 && bfd_hash_lookup (finfo->info->keep_hash,
1679 h->root.root.string, FALSE, FALSE) == NULL)
1680 strip = TRUE;
1681 else if (finfo->info->strip_discarded
1682 && (h->root.type == bfd_link_hash_defined
1683 || h->root.type == bfd_link_hash_defweak)
1684 && elf_discarded_section (h->root.u.def.section))
1685 strip = TRUE;
1686 else
1687 strip = FALSE;
1688
1689 /* If we're stripping it, and it's not a dynamic symbol, there's
1690 nothing else to do unless it is a forced local symbol. */
1691 if (strip
1692 && h->dynindx == -1
1693 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1694 return TRUE;
1695
1696 sym.st_value = 0;
1697 sym.st_size = h->size;
1698 sym.st_other = h->other;
1699 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1700 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
1701 else if (h->root.type == bfd_link_hash_undefweak
1702 || h->root.type == bfd_link_hash_defweak)
1703 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
1704 else
1705 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
1706
1707 switch (h->root.type)
1708 {
1709 default:
1710 case bfd_link_hash_new:
1711 case bfd_link_hash_warning:
1712 abort ();
1713 return FALSE;
1714
1715 case bfd_link_hash_undefined:
1716 case bfd_link_hash_undefweak:
1717 input_sec = bfd_und_section_ptr;
1718 sym.st_shndx = SHN_UNDEF;
1719 break;
1720
1721 case bfd_link_hash_defined:
1722 case bfd_link_hash_defweak:
1723 {
1724 input_sec = h->root.u.def.section;
1725 if (input_sec->output_section != NULL)
1726 {
1727 sym.st_shndx =
1728 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
1729 input_sec->output_section);
1730 if (sym.st_shndx == SHN_BAD)
1731 {
1732 (*_bfd_error_handler)
1733 (_("%s: could not find output section %s for input section %s"),
1734 bfd_get_filename (finfo->output_bfd),
1735 input_sec->output_section->name,
1736 input_sec->name);
1737 eoinfo->failed = TRUE;
1738 return FALSE;
1739 }
1740
1741 /* ELF symbols in relocatable files are section relative,
1742 but in nonrelocatable files they are virtual
1743 addresses. */
1744 sym.st_value = h->root.u.def.value + input_sec->output_offset;
1745 if (! finfo->info->relocatable)
1746 {
1747 sym.st_value += input_sec->output_section->vma;
1748 if (h->type == STT_TLS)
1749 {
1750 /* STT_TLS symbols are relative to PT_TLS segment
1751 base. */
1752 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
1753 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
1754 }
1755 }
1756 }
1757 else
1758 {
1759 BFD_ASSERT (input_sec->owner == NULL
1760 || (input_sec->owner->flags & DYNAMIC) != 0);
1761 sym.st_shndx = SHN_UNDEF;
1762 input_sec = bfd_und_section_ptr;
1763 }
1764 }
1765 break;
1766
1767 case bfd_link_hash_common:
1768 input_sec = h->root.u.c.p->section;
1769 sym.st_shndx = SHN_COMMON;
1770 sym.st_value = 1 << h->root.u.c.p->alignment_power;
1771 break;
1772
1773 case bfd_link_hash_indirect:
1774 /* These symbols are created by symbol versioning. They point
1775 to the decorated version of the name. For example, if the
1776 symbol foo@@GNU_1.2 is the default, which should be used when
1777 foo is used with no version, then we add an indirect symbol
1778 foo which points to foo@@GNU_1.2. We ignore these symbols,
1779 since the indirected symbol is already in the hash table. */
1780 return TRUE;
1781 }
1782
1783 /* Give the processor backend a chance to tweak the symbol value,
1784 and also to finish up anything that needs to be done for this
1785 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
1786 forced local syms when non-shared is due to a historical quirk. */
1787 if ((h->dynindx != -1
1788 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1789 && ((finfo->info->shared
1790 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1791 || h->root.type != bfd_link_hash_undefweak))
1792 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1793 && elf_hash_table (finfo->info)->dynamic_sections_created)
1794 {
1795 if (! ((*bed->elf_backend_finish_dynamic_symbol)
1796 (finfo->output_bfd, finfo->info, h, &sym)))
1797 {
1798 eoinfo->failed = TRUE;
1799 return FALSE;
1800 }
1801 }
1802
1803 /* If we are marking the symbol as undefined, and there are no
1804 non-weak references to this symbol from a regular object, then
1805 mark the symbol as weak undefined; if there are non-weak
1806 references, mark the symbol as strong. We can't do this earlier,
1807 because it might not be marked as undefined until the
1808 finish_dynamic_symbol routine gets through with it. */
1809 if (sym.st_shndx == SHN_UNDEF
1810 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
1811 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
1812 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
1813 {
1814 int bindtype;
1815
1816 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
1817 bindtype = STB_GLOBAL;
1818 else
1819 bindtype = STB_WEAK;
1820 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
1821 }
1822
1823 /* If a non-weak symbol with non-default visibility is not defined
1824 locally, it is a fatal error. */
1825 if (! finfo->info->relocatable
1826 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
1827 && ELF_ST_BIND (sym.st_info) != STB_WEAK
1828 && h->root.type == bfd_link_hash_undefined
1829 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1830 {
1831 (*_bfd_error_handler)
1832 (_("%s: %s symbol `%s' isn't defined"),
1833 bfd_get_filename (finfo->output_bfd),
1834 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
1835 ? "protected"
1836 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
1837 ? "internal" : "hidden",
1838 h->root.root.string);
1839 eoinfo->failed = TRUE;
1840 return FALSE;
1841 }
1842
1843 /* If this symbol should be put in the .dynsym section, then put it
1844 there now. We already know the symbol index. We also fill in
1845 the entry in the .hash section. */
1846 if (h->dynindx != -1
1847 && elf_hash_table (finfo->info)->dynamic_sections_created)
1848 {
1849 size_t bucketcount;
1850 size_t bucket;
1851 size_t hash_entry_size;
1852 bfd_byte *bucketpos;
1853 bfd_vma chain;
1854 bfd_byte *esym;
1855
1856 sym.st_name = h->dynstr_index;
1857 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
1858 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
1859
1860 bucketcount = elf_hash_table (finfo->info)->bucketcount;
1861 bucket = h->elf_hash_value % bucketcount;
1862 hash_entry_size
1863 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
1864 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
1865 + (bucket + 2) * hash_entry_size);
1866 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
1867 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
1868 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
1869 ((bfd_byte *) finfo->hash_sec->contents
1870 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
1871
1872 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
1873 {
1874 Elf_Internal_Versym iversym;
1875 Elf_External_Versym *eversym;
1876
1877 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1878 {
1879 if (h->verinfo.verdef == NULL)
1880 iversym.vs_vers = 0;
1881 else
1882 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
1883 }
1884 else
1885 {
1886 if (h->verinfo.vertree == NULL)
1887 iversym.vs_vers = 1;
1888 else
1889 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
1890 }
1891
1892 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
1893 iversym.vs_vers |= VERSYM_HIDDEN;
1894
1895 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
1896 eversym += h->dynindx;
1897 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
1898 }
1899 }
1900
1901 /* If we're stripping it, then it was just a dynamic symbol, and
1902 there's nothing else to do. */
1903 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
1904 return TRUE;
1905
1906 h->indx = bfd_get_symcount (finfo->output_bfd);
1907
1908 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
1909 {
1910 eoinfo->failed = TRUE;
1911 return FALSE;
1912 }
1913
1914 return TRUE;
1915 }
1916
1917 /* Link an input file into the linker output file. This function
1918 handles all the sections and relocations of the input file at once.
1919 This is so that we only have to read the local symbols once, and
1920 don't have to keep them in memory. */
1921
1922 static bfd_boolean
1923 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
1924 {
1925 bfd_boolean (*relocate_section)
1926 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
1927 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
1928 bfd *output_bfd;
1929 Elf_Internal_Shdr *symtab_hdr;
1930 size_t locsymcount;
1931 size_t extsymoff;
1932 Elf_Internal_Sym *isymbuf;
1933 Elf_Internal_Sym *isym;
1934 Elf_Internal_Sym *isymend;
1935 long *pindex;
1936 asection **ppsection;
1937 asection *o;
1938 const struct elf_backend_data *bed;
1939 bfd_boolean emit_relocs;
1940 struct elf_link_hash_entry **sym_hashes;
1941
1942 output_bfd = finfo->output_bfd;
1943 bed = get_elf_backend_data (output_bfd);
1944 relocate_section = bed->elf_backend_relocate_section;
1945
1946 /* If this is a dynamic object, we don't want to do anything here:
1947 we don't want the local symbols, and we don't want the section
1948 contents. */
1949 if ((input_bfd->flags & DYNAMIC) != 0)
1950 return TRUE;
1951
1952 emit_relocs = (finfo->info->relocatable
1953 || finfo->info->emitrelocations
1954 || bed->elf_backend_emit_relocs);
1955
1956 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1957 if (elf_bad_symtab (input_bfd))
1958 {
1959 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
1960 extsymoff = 0;
1961 }
1962 else
1963 {
1964 locsymcount = symtab_hdr->sh_info;
1965 extsymoff = symtab_hdr->sh_info;
1966 }
1967
1968 /* Read the local symbols. */
1969 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1970 if (isymbuf == NULL && locsymcount != 0)
1971 {
1972 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
1973 finfo->internal_syms,
1974 finfo->external_syms,
1975 finfo->locsym_shndx);
1976 if (isymbuf == NULL)
1977 return FALSE;
1978 }
1979
1980 /* Find local symbol sections and adjust values of symbols in
1981 SEC_MERGE sections. Write out those local symbols we know are
1982 going into the output file. */
1983 isymend = isymbuf + locsymcount;
1984 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
1985 isym < isymend;
1986 isym++, pindex++, ppsection++)
1987 {
1988 asection *isec;
1989 const char *name;
1990 Elf_Internal_Sym osym;
1991
1992 *pindex = -1;
1993
1994 if (elf_bad_symtab (input_bfd))
1995 {
1996 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
1997 {
1998 *ppsection = NULL;
1999 continue;
2000 }
2001 }
2002
2003 if (isym->st_shndx == SHN_UNDEF)
2004 isec = bfd_und_section_ptr;
2005 else if (isym->st_shndx < SHN_LORESERVE
2006 || isym->st_shndx > SHN_HIRESERVE)
2007 {
2008 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2009 if (isec
2010 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
2011 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
2012 isym->st_value =
2013 _bfd_merged_section_offset (output_bfd, &isec,
2014 elf_section_data (isec)->sec_info,
2015 isym->st_value, 0);
2016 }
2017 else if (isym->st_shndx == SHN_ABS)
2018 isec = bfd_abs_section_ptr;
2019 else if (isym->st_shndx == SHN_COMMON)
2020 isec = bfd_com_section_ptr;
2021 else
2022 {
2023 /* Who knows? */
2024 isec = NULL;
2025 }
2026
2027 *ppsection = isec;
2028
2029 /* Don't output the first, undefined, symbol. */
2030 if (ppsection == finfo->sections)
2031 continue;
2032
2033 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
2034 {
2035 /* We never output section symbols. Instead, we use the
2036 section symbol of the corresponding section in the output
2037 file. */
2038 continue;
2039 }
2040
2041 /* If we are stripping all symbols, we don't want to output this
2042 one. */
2043 if (finfo->info->strip == strip_all)
2044 continue;
2045
2046 /* If we are discarding all local symbols, we don't want to
2047 output this one. If we are generating a relocatable output
2048 file, then some of the local symbols may be required by
2049 relocs; we output them below as we discover that they are
2050 needed. */
2051 if (finfo->info->discard == discard_all)
2052 continue;
2053
2054 /* If this symbol is defined in a section which we are
2055 discarding, we don't need to keep it, but note that
2056 linker_mark is only reliable for sections that have contents.
2057 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
2058 as well as linker_mark. */
2059 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
2060 && isec != NULL
2061 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
2062 || (! finfo->info->relocatable
2063 && (isec->flags & SEC_EXCLUDE) != 0)))
2064 continue;
2065
2066 /* Get the name of the symbol. */
2067 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
2068 isym->st_name);
2069 if (name == NULL)
2070 return FALSE;
2071
2072 /* See if we are discarding symbols with this name. */
2073 if ((finfo->info->strip == strip_some
2074 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
2075 == NULL))
2076 || (((finfo->info->discard == discard_sec_merge
2077 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
2078 || finfo->info->discard == discard_l)
2079 && bfd_is_local_label_name (input_bfd, name)))
2080 continue;
2081
2082 /* If we get here, we are going to output this symbol. */
2083
2084 osym = *isym;
2085
2086 /* Adjust the section index for the output file. */
2087 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
2088 isec->output_section);
2089 if (osym.st_shndx == SHN_BAD)
2090 return FALSE;
2091
2092 *pindex = bfd_get_symcount (output_bfd);
2093
2094 /* ELF symbols in relocatable files are section relative, but
2095 in executable files they are virtual addresses. Note that
2096 this code assumes that all ELF sections have an associated
2097 BFD section with a reasonable value for output_offset; below
2098 we assume that they also have a reasonable value for
2099 output_section. Any special sections must be set up to meet
2100 these requirements. */
2101 osym.st_value += isec->output_offset;
2102 if (! finfo->info->relocatable)
2103 {
2104 osym.st_value += isec->output_section->vma;
2105 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
2106 {
2107 /* STT_TLS symbols are relative to PT_TLS segment base. */
2108 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
2109 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
2110 }
2111 }
2112
2113 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
2114 return FALSE;
2115 }
2116
2117 /* Relocate the contents of each section. */
2118 sym_hashes = elf_sym_hashes (input_bfd);
2119 for (o = input_bfd->sections; o != NULL; o = o->next)
2120 {
2121 bfd_byte *contents;
2122
2123 if (! o->linker_mark)
2124 {
2125 /* This section was omitted from the link. */
2126 continue;
2127 }
2128
2129 if ((o->flags & SEC_HAS_CONTENTS) == 0
2130 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
2131 continue;
2132
2133 if ((o->flags & SEC_LINKER_CREATED) != 0)
2134 {
2135 /* Section was created by _bfd_elf_link_create_dynamic_sections
2136 or somesuch. */
2137 continue;
2138 }
2139
2140 /* Get the contents of the section. They have been cached by a
2141 relaxation routine. Note that o is a section in an input
2142 file, so the contents field will not have been set by any of
2143 the routines which work on output files. */
2144 if (elf_section_data (o)->this_hdr.contents != NULL)
2145 contents = elf_section_data (o)->this_hdr.contents;
2146 else
2147 {
2148 contents = finfo->contents;
2149 if (! bfd_get_section_contents (input_bfd, o, contents, 0,
2150 o->_raw_size))
2151 return FALSE;
2152 }
2153
2154 if ((o->flags & SEC_RELOC) != 0)
2155 {
2156 Elf_Internal_Rela *internal_relocs;
2157 bfd_vma r_type_mask;
2158 int r_sym_shift;
2159
2160 /* Get the swapped relocs. */
2161 internal_relocs
2162 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
2163 finfo->internal_relocs, FALSE);
2164 if (internal_relocs == NULL
2165 && o->reloc_count > 0)
2166 return FALSE;
2167
2168 if (bed->s->arch_size == 32)
2169 {
2170 r_type_mask = 0xff;
2171 r_sym_shift = 8;
2172 }
2173 else
2174 {
2175 r_type_mask = 0xffffffff;
2176 r_sym_shift = 32;
2177 }
2178
2179 /* Run through the relocs looking for any against symbols
2180 from discarded sections and section symbols from
2181 removed link-once sections. Complain about relocs
2182 against discarded sections. Zero relocs against removed
2183 link-once sections. Preserve debug information as much
2184 as we can. */
2185 if (!elf_section_ignore_discarded_relocs (o))
2186 {
2187 Elf_Internal_Rela *rel, *relend;
2188
2189 rel = internal_relocs;
2190 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
2191 for ( ; rel < relend; rel++)
2192 {
2193 unsigned long r_symndx = rel->r_info >> r_sym_shift;
2194 asection *sec;
2195
2196 if (r_symndx >= locsymcount
2197 || (elf_bad_symtab (input_bfd)
2198 && finfo->sections[r_symndx] == NULL))
2199 {
2200 struct elf_link_hash_entry *h;
2201
2202 h = sym_hashes[r_symndx - extsymoff];
2203 while (h->root.type == bfd_link_hash_indirect
2204 || h->root.type == bfd_link_hash_warning)
2205 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2206
2207 /* Complain if the definition comes from a
2208 discarded section. */
2209 sec = h->root.u.def.section;
2210 if ((h->root.type == bfd_link_hash_defined
2211 || h->root.type == bfd_link_hash_defweak)
2212 && elf_discarded_section (sec))
2213 {
2214 if ((o->flags & SEC_DEBUGGING) != 0)
2215 {
2216 BFD_ASSERT (r_symndx != 0);
2217 /* Try to preserve debug information. */
2218 if ((o->flags & SEC_DEBUGGING) != 0
2219 && sec->kept_section != NULL
2220 && sec->_raw_size == sec->kept_section->_raw_size)
2221 h->root.u.def.section
2222 = sec->kept_section;
2223 else
2224 memset (rel, 0, sizeof (*rel));
2225 }
2226 else
2227 finfo->info->callbacks->error_handler
2228 (LD_DEFINITION_IN_DISCARDED_SECTION,
2229 _("%T: discarded in section `%s' from %s\n"),
2230 h->root.root.string,
2231 h->root.root.string,
2232 h->root.u.def.section->name,
2233 bfd_archive_filename (h->root.u.def.section->owner));
2234 }
2235 }
2236 else
2237 {
2238 sec = finfo->sections[r_symndx];
2239
2240 if (sec != NULL && elf_discarded_section (sec))
2241 {
2242 if ((o->flags & SEC_DEBUGGING) != 0
2243 || (sec->flags & SEC_LINK_ONCE) != 0)
2244 {
2245 BFD_ASSERT (r_symndx != 0);
2246 /* Try to preserve debug information. */
2247 if ((o->flags & SEC_DEBUGGING) != 0
2248 && sec->kept_section != NULL
2249 && sec->_raw_size == sec->kept_section->_raw_size)
2250 finfo->sections[r_symndx]
2251 = sec->kept_section;
2252 else
2253 {
2254 rel->r_info &= r_type_mask;
2255 rel->r_addend = 0;
2256 }
2257 }
2258 else
2259 {
2260 static int count;
2261 int ok;
2262 char *buf;
2263
2264 ok = asprintf (&buf, "local symbol %d",
2265 count++);
2266 if (ok <= 0)
2267 buf = (char *) "local symbol";
2268 finfo->info->callbacks->error_handler
2269 (LD_DEFINITION_IN_DISCARDED_SECTION,
2270 _("%T: discarded in section `%s' from %s\n"),
2271 buf, buf, sec->name,
2272 bfd_archive_filename (input_bfd));
2273 if (ok != -1)
2274 free (buf);
2275 }
2276 }
2277 }
2278 }
2279 }
2280
2281 /* Relocate the section by invoking a back end routine.
2282
2283 The back end routine is responsible for adjusting the
2284 section contents as necessary, and (if using Rela relocs
2285 and generating a relocatable output file) adjusting the
2286 reloc addend as necessary.
2287
2288 The back end routine does not have to worry about setting
2289 the reloc address or the reloc symbol index.
2290
2291 The back end routine is given a pointer to the swapped in
2292 internal symbols, and can access the hash table entries
2293 for the external symbols via elf_sym_hashes (input_bfd).
2294
2295 When generating relocatable output, the back end routine
2296 must handle STB_LOCAL/STT_SECTION symbols specially. The
2297 output symbol is going to be a section symbol
2298 corresponding to the output section, which will require
2299 the addend to be adjusted. */
2300
2301 if (! (*relocate_section) (output_bfd, finfo->info,
2302 input_bfd, o, contents,
2303 internal_relocs,
2304 isymbuf,
2305 finfo->sections))
2306 return FALSE;
2307
2308 if (emit_relocs)
2309 {
2310 Elf_Internal_Rela *irela;
2311 Elf_Internal_Rela *irelaend;
2312 bfd_vma last_offset;
2313 struct elf_link_hash_entry **rel_hash;
2314 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
2315 unsigned int next_erel;
2316 bfd_boolean (*reloc_emitter)
2317 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
2318 bfd_boolean rela_normal;
2319
2320 input_rel_hdr = &elf_section_data (o)->rel_hdr;
2321 rela_normal = (bed->rela_normal
2322 && (input_rel_hdr->sh_entsize
2323 == bed->s->sizeof_rela));
2324
2325 /* Adjust the reloc addresses and symbol indices. */
2326
2327 irela = internal_relocs;
2328 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
2329 rel_hash = (elf_section_data (o->output_section)->rel_hashes
2330 + elf_section_data (o->output_section)->rel_count
2331 + elf_section_data (o->output_section)->rel_count2);
2332 last_offset = o->output_offset;
2333 if (!finfo->info->relocatable)
2334 last_offset += o->output_section->vma;
2335 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
2336 {
2337 unsigned long r_symndx;
2338 asection *sec;
2339 Elf_Internal_Sym sym;
2340
2341 if (next_erel == bed->s->int_rels_per_ext_rel)
2342 {
2343 rel_hash++;
2344 next_erel = 0;
2345 }
2346
2347 irela->r_offset = _bfd_elf_section_offset (output_bfd,
2348 finfo->info, o,
2349 irela->r_offset);
2350 if (irela->r_offset >= (bfd_vma) -2)
2351 {
2352 /* This is a reloc for a deleted entry or somesuch.
2353 Turn it into an R_*_NONE reloc, at the same
2354 offset as the last reloc. elf_eh_frame.c and
2355 elf_bfd_discard_info rely on reloc offsets
2356 being ordered. */
2357 irela->r_offset = last_offset;
2358 irela->r_info = 0;
2359 irela->r_addend = 0;
2360 continue;
2361 }
2362
2363 irela->r_offset += o->output_offset;
2364
2365 /* Relocs in an executable have to be virtual addresses. */
2366 if (!finfo->info->relocatable)
2367 irela->r_offset += o->output_section->vma;
2368
2369 last_offset = irela->r_offset;
2370
2371 r_symndx = irela->r_info >> r_sym_shift;
2372 if (r_symndx == STN_UNDEF)
2373 continue;
2374
2375 if (r_symndx >= locsymcount
2376 || (elf_bad_symtab (input_bfd)
2377 && finfo->sections[r_symndx] == NULL))
2378 {
2379 struct elf_link_hash_entry *rh;
2380 unsigned long indx;
2381
2382 /* This is a reloc against a global symbol. We
2383 have not yet output all the local symbols, so
2384 we do not know the symbol index of any global
2385 symbol. We set the rel_hash entry for this
2386 reloc to point to the global hash table entry
2387 for this symbol. The symbol index is then
2388 set at the end of elf_bfd_final_link. */
2389 indx = r_symndx - extsymoff;
2390 rh = elf_sym_hashes (input_bfd)[indx];
2391 while (rh->root.type == bfd_link_hash_indirect
2392 || rh->root.type == bfd_link_hash_warning)
2393 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
2394
2395 /* Setting the index to -2 tells
2396 elf_link_output_extsym that this symbol is
2397 used by a reloc. */
2398 BFD_ASSERT (rh->indx < 0);
2399 rh->indx = -2;
2400
2401 *rel_hash = rh;
2402
2403 continue;
2404 }
2405
2406 /* This is a reloc against a local symbol. */
2407
2408 *rel_hash = NULL;
2409 sym = isymbuf[r_symndx];
2410 sec = finfo->sections[r_symndx];
2411 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
2412 {
2413 /* I suppose the backend ought to fill in the
2414 section of any STT_SECTION symbol against a
2415 processor specific section. If we have
2416 discarded a section, the output_section will
2417 be the absolute section. */
2418 if (bfd_is_abs_section (sec)
2419 || (sec != NULL
2420 && bfd_is_abs_section (sec->output_section)))
2421 r_symndx = 0;
2422 else if (sec == NULL || sec->owner == NULL)
2423 {
2424 bfd_set_error (bfd_error_bad_value);
2425 return FALSE;
2426 }
2427 else
2428 {
2429 r_symndx = sec->output_section->target_index;
2430 BFD_ASSERT (r_symndx != 0);
2431 }
2432
2433 /* Adjust the addend according to where the
2434 section winds up in the output section. */
2435 if (rela_normal)
2436 irela->r_addend += sec->output_offset;
2437 }
2438 else
2439 {
2440 if (finfo->indices[r_symndx] == -1)
2441 {
2442 unsigned long shlink;
2443 const char *name;
2444 asection *osec;
2445
2446 if (finfo->info->strip == strip_all)
2447 {
2448 /* You can't do ld -r -s. */
2449 bfd_set_error (bfd_error_invalid_operation);
2450 return FALSE;
2451 }
2452
2453 /* This symbol was skipped earlier, but
2454 since it is needed by a reloc, we
2455 must output it now. */
2456 shlink = symtab_hdr->sh_link;
2457 name = (bfd_elf_string_from_elf_section
2458 (input_bfd, shlink, sym.st_name));
2459 if (name == NULL)
2460 return FALSE;
2461
2462 osec = sec->output_section;
2463 sym.st_shndx =
2464 _bfd_elf_section_from_bfd_section (output_bfd,
2465 osec);
2466 if (sym.st_shndx == SHN_BAD)
2467 return FALSE;
2468
2469 sym.st_value += sec->output_offset;
2470 if (! finfo->info->relocatable)
2471 {
2472 sym.st_value += osec->vma;
2473 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
2474 {
2475 /* STT_TLS symbols are relative to PT_TLS
2476 segment base. */
2477 BFD_ASSERT (elf_hash_table (finfo->info)
2478 ->tls_sec != NULL);
2479 sym.st_value -= (elf_hash_table (finfo->info)
2480 ->tls_sec->vma);
2481 }
2482 }
2483
2484 finfo->indices[r_symndx]
2485 = bfd_get_symcount (output_bfd);
2486
2487 if (! elf_link_output_sym (finfo, name, &sym, sec,
2488 NULL))
2489 return FALSE;
2490 }
2491
2492 r_symndx = finfo->indices[r_symndx];
2493 }
2494
2495 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
2496 | (irela->r_info & r_type_mask));
2497 }
2498
2499 /* Swap out the relocs. */
2500 if (bed->elf_backend_emit_relocs
2501 && !(finfo->info->relocatable
2502 || finfo->info->emitrelocations))
2503 reloc_emitter = bed->elf_backend_emit_relocs;
2504 else
2505 reloc_emitter = _bfd_elf_link_output_relocs;
2506
2507 if (input_rel_hdr->sh_size != 0
2508 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
2509 internal_relocs))
2510 return FALSE;
2511
2512 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
2513 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
2514 {
2515 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
2516 * bed->s->int_rels_per_ext_rel);
2517 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
2518 internal_relocs))
2519 return FALSE;
2520 }
2521 }
2522 }
2523
2524 /* Write out the modified section contents. */
2525 if (bed->elf_backend_write_section
2526 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
2527 {
2528 /* Section written out. */
2529 }
2530 else switch (o->sec_info_type)
2531 {
2532 case ELF_INFO_TYPE_STABS:
2533 if (! (_bfd_write_section_stabs
2534 (output_bfd,
2535 &elf_hash_table (finfo->info)->stab_info,
2536 o, &elf_section_data (o)->sec_info, contents)))
2537 return FALSE;
2538 break;
2539 case ELF_INFO_TYPE_MERGE:
2540 if (! _bfd_write_merged_section (output_bfd, o,
2541 elf_section_data (o)->sec_info))
2542 return FALSE;
2543 break;
2544 case ELF_INFO_TYPE_EH_FRAME:
2545 {
2546 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
2547 o, contents))
2548 return FALSE;
2549 }
2550 break;
2551 default:
2552 {
2553 bfd_size_type sec_size;
2554
2555 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
2556 if (! (o->flags & SEC_EXCLUDE)
2557 && ! bfd_set_section_contents (output_bfd, o->output_section,
2558 contents,
2559 (file_ptr) o->output_offset,
2560 sec_size))
2561 return FALSE;
2562 }
2563 break;
2564 }
2565 }
2566
2567 return TRUE;
2568 }
2569
2570 /* Generate a reloc when linking an ELF file. This is a reloc
2571 requested by the linker, and does come from any input file. This
2572 is used to build constructor and destructor tables when linking
2573 with -Ur. */
2574
2575 static bfd_boolean
2576 elf_reloc_link_order (bfd *output_bfd,
2577 struct bfd_link_info *info,
2578 asection *output_section,
2579 struct bfd_link_order *link_order)
2580 {
2581 reloc_howto_type *howto;
2582 long indx;
2583 bfd_vma offset;
2584 bfd_vma addend;
2585 struct elf_link_hash_entry **rel_hash_ptr;
2586 Elf_Internal_Shdr *rel_hdr;
2587 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2588 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
2589 bfd_byte *erel;
2590 unsigned int i;
2591
2592 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2593 if (howto == NULL)
2594 {
2595 bfd_set_error (bfd_error_bad_value);
2596 return FALSE;
2597 }
2598
2599 addend = link_order->u.reloc.p->addend;
2600
2601 /* Figure out the symbol index. */
2602 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
2603 + elf_section_data (output_section)->rel_count
2604 + elf_section_data (output_section)->rel_count2);
2605 if (link_order->type == bfd_section_reloc_link_order)
2606 {
2607 indx = link_order->u.reloc.p->u.section->target_index;
2608 BFD_ASSERT (indx != 0);
2609 *rel_hash_ptr = NULL;
2610 }
2611 else
2612 {
2613 struct elf_link_hash_entry *h;
2614
2615 /* Treat a reloc against a defined symbol as though it were
2616 actually against the section. */
2617 h = ((struct elf_link_hash_entry *)
2618 bfd_wrapped_link_hash_lookup (output_bfd, info,
2619 link_order->u.reloc.p->u.name,
2620 FALSE, FALSE, TRUE));
2621 if (h != NULL
2622 && (h->root.type == bfd_link_hash_defined
2623 || h->root.type == bfd_link_hash_defweak))
2624 {
2625 asection *section;
2626
2627 section = h->root.u.def.section;
2628 indx = section->output_section->target_index;
2629 *rel_hash_ptr = NULL;
2630 /* It seems that we ought to add the symbol value to the
2631 addend here, but in practice it has already been added
2632 because it was passed to constructor_callback. */
2633 addend += section->output_section->vma + section->output_offset;
2634 }
2635 else if (h != NULL)
2636 {
2637 /* Setting the index to -2 tells elf_link_output_extsym that
2638 this symbol is used by a reloc. */
2639 h->indx = -2;
2640 *rel_hash_ptr = h;
2641 indx = 0;
2642 }
2643 else
2644 {
2645 if (! ((*info->callbacks->unattached_reloc)
2646 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
2647 return FALSE;
2648 indx = 0;
2649 }
2650 }
2651
2652 /* If this is an inplace reloc, we must write the addend into the
2653 object file. */
2654 if (howto->partial_inplace && addend != 0)
2655 {
2656 bfd_size_type size;
2657 bfd_reloc_status_type rstat;
2658 bfd_byte *buf;
2659 bfd_boolean ok;
2660 const char *sym_name;
2661
2662 size = bfd_get_reloc_size (howto);
2663 buf = bfd_zmalloc (size);
2664 if (buf == NULL)
2665 return FALSE;
2666 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
2667 switch (rstat)
2668 {
2669 case bfd_reloc_ok:
2670 break;
2671
2672 default:
2673 case bfd_reloc_outofrange:
2674 abort ();
2675
2676 case bfd_reloc_overflow:
2677 if (link_order->type == bfd_section_reloc_link_order)
2678 sym_name = bfd_section_name (output_bfd,
2679 link_order->u.reloc.p->u.section);
2680 else
2681 sym_name = link_order->u.reloc.p->u.name;
2682 if (! ((*info->callbacks->reloc_overflow)
2683 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
2684 {
2685 free (buf);
2686 return FALSE;
2687 }
2688 break;
2689 }
2690 ok = bfd_set_section_contents (output_bfd, output_section, buf,
2691 link_order->offset, size);
2692 free (buf);
2693 if (! ok)
2694 return FALSE;
2695 }
2696
2697 /* The address of a reloc is relative to the section in a
2698 relocatable file, and is a virtual address in an executable
2699 file. */
2700 offset = link_order->offset;
2701 if (! info->relocatable)
2702 offset += output_section->vma;
2703
2704 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
2705 {
2706 irel[i].r_offset = offset;
2707 irel[i].r_info = 0;
2708 irel[i].r_addend = 0;
2709 }
2710 if (bed->s->arch_size == 32)
2711 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
2712 else
2713 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
2714
2715 rel_hdr = &elf_section_data (output_section)->rel_hdr;
2716 erel = rel_hdr->contents;
2717 if (rel_hdr->sh_type == SHT_REL)
2718 {
2719 erel += (elf_section_data (output_section)->rel_count
2720 * bed->s->sizeof_rel);
2721 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
2722 }
2723 else
2724 {
2725 irel[0].r_addend = addend;
2726 erel += (elf_section_data (output_section)->rel_count
2727 * bed->s->sizeof_rela);
2728 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
2729 }
2730
2731 ++elf_section_data (output_section)->rel_count;
2732
2733 return TRUE;
2734 }
2735 \f
2736 /* Garbage collect unused sections. */
2737
2738 static bfd_boolean elf_gc_sweep_symbol
2739 (struct elf_link_hash_entry *, void *);
2740
2741 static bfd_boolean elf_gc_allocate_got_offsets
2742 (struct elf_link_hash_entry *, void *);
2743
2744 /* The mark phase of garbage collection. For a given section, mark
2745 it and any sections in this section's group, and all the sections
2746 which define symbols to which it refers. */
2747
2748 typedef asection * (*gc_mark_hook_fn)
2749 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
2750 struct elf_link_hash_entry *, Elf_Internal_Sym *);
2751
2752 static bfd_boolean
2753 elf_gc_mark (struct bfd_link_info *info,
2754 asection *sec,
2755 gc_mark_hook_fn gc_mark_hook)
2756 {
2757 bfd_boolean ret;
2758 asection *group_sec;
2759
2760 sec->gc_mark = 1;
2761
2762 /* Mark all the sections in the group. */
2763 group_sec = elf_section_data (sec)->next_in_group;
2764 if (group_sec && !group_sec->gc_mark)
2765 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
2766 return FALSE;
2767
2768 /* Look through the section relocs. */
2769 ret = TRUE;
2770 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
2771 {
2772 Elf_Internal_Rela *relstart, *rel, *relend;
2773 Elf_Internal_Shdr *symtab_hdr;
2774 struct elf_link_hash_entry **sym_hashes;
2775 size_t nlocsyms;
2776 size_t extsymoff;
2777 bfd *input_bfd = sec->owner;
2778 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
2779 Elf_Internal_Sym *isym = NULL;
2780 int r_sym_shift;
2781
2782 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2783 sym_hashes = elf_sym_hashes (input_bfd);
2784
2785 /* Read the local symbols. */
2786 if (elf_bad_symtab (input_bfd))
2787 {
2788 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
2789 extsymoff = 0;
2790 }
2791 else
2792 extsymoff = nlocsyms = symtab_hdr->sh_info;
2793
2794 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2795 if (isym == NULL && nlocsyms != 0)
2796 {
2797 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
2798 NULL, NULL, NULL);
2799 if (isym == NULL)
2800 return FALSE;
2801 }
2802
2803 /* Read the relocations. */
2804 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
2805 info->keep_memory);
2806 if (relstart == NULL)
2807 {
2808 ret = FALSE;
2809 goto out1;
2810 }
2811 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
2812
2813 if (bed->s->arch_size == 32)
2814 r_sym_shift = 8;
2815 else
2816 r_sym_shift = 32;
2817
2818 for (rel = relstart; rel < relend; rel++)
2819 {
2820 unsigned long r_symndx;
2821 asection *rsec;
2822 struct elf_link_hash_entry *h;
2823
2824 r_symndx = rel->r_info >> r_sym_shift;
2825 if (r_symndx == 0)
2826 continue;
2827
2828 if (r_symndx >= nlocsyms
2829 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
2830 {
2831 h = sym_hashes[r_symndx - extsymoff];
2832 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
2833 }
2834 else
2835 {
2836 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
2837 }
2838
2839 if (rsec && !rsec->gc_mark)
2840 {
2841 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
2842 rsec->gc_mark = 1;
2843 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
2844 {
2845 ret = FALSE;
2846 goto out2;
2847 }
2848 }
2849 }
2850
2851 out2:
2852 if (elf_section_data (sec)->relocs != relstart)
2853 free (relstart);
2854 out1:
2855 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
2856 {
2857 if (! info->keep_memory)
2858 free (isym);
2859 else
2860 symtab_hdr->contents = (unsigned char *) isym;
2861 }
2862 }
2863
2864 return ret;
2865 }
2866
2867 /* The sweep phase of garbage collection. Remove all garbage sections. */
2868
2869 typedef bfd_boolean (*gc_sweep_hook_fn)
2870 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2871
2872 static bfd_boolean
2873 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
2874 {
2875 bfd *sub;
2876
2877 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2878 {
2879 asection *o;
2880
2881 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
2882 continue;
2883
2884 for (o = sub->sections; o != NULL; o = o->next)
2885 {
2886 /* Keep special sections. Keep .debug sections. */
2887 if ((o->flags & SEC_LINKER_CREATED)
2888 || (o->flags & SEC_DEBUGGING))
2889 o->gc_mark = 1;
2890
2891 if (o->gc_mark)
2892 continue;
2893
2894 /* Skip sweeping sections already excluded. */
2895 if (o->flags & SEC_EXCLUDE)
2896 continue;
2897
2898 /* Since this is early in the link process, it is simple
2899 to remove a section from the output. */
2900 o->flags |= SEC_EXCLUDE;
2901
2902 /* But we also have to update some of the relocation
2903 info we collected before. */
2904 if (gc_sweep_hook
2905 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
2906 {
2907 Elf_Internal_Rela *internal_relocs;
2908 bfd_boolean r;
2909
2910 internal_relocs
2911 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
2912 info->keep_memory);
2913 if (internal_relocs == NULL)
2914 return FALSE;
2915
2916 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
2917
2918 if (elf_section_data (o)->relocs != internal_relocs)
2919 free (internal_relocs);
2920
2921 if (!r)
2922 return FALSE;
2923 }
2924 }
2925 }
2926
2927 /* Remove the symbols that were in the swept sections from the dynamic
2928 symbol table. GCFIXME: Anyone know how to get them out of the
2929 static symbol table as well? */
2930 {
2931 int i = 0;
2932
2933 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
2934
2935 elf_hash_table (info)->dynsymcount = i;
2936 }
2937
2938 return TRUE;
2939 }
2940
2941 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
2942
2943 static bfd_boolean
2944 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
2945 {
2946 int *idx = idxptr;
2947
2948 if (h->root.type == bfd_link_hash_warning)
2949 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2950
2951 if (h->dynindx != -1
2952 && ((h->root.type != bfd_link_hash_defined
2953 && h->root.type != bfd_link_hash_defweak)
2954 || h->root.u.def.section->gc_mark))
2955 h->dynindx = (*idx)++;
2956
2957 return TRUE;
2958 }
2959
2960 /* Propagate collected vtable information. This is called through
2961 elf_link_hash_traverse. */
2962
2963 static bfd_boolean
2964 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
2965 {
2966 if (h->root.type == bfd_link_hash_warning)
2967 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2968
2969 /* Those that are not vtables. */
2970 if (h->vtable_parent == NULL)
2971 return TRUE;
2972
2973 /* Those vtables that do not have parents, we cannot merge. */
2974 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
2975 return TRUE;
2976
2977 /* If we've already been done, exit. */
2978 if (h->vtable_entries_used && h->vtable_entries_used[-1])
2979 return TRUE;
2980
2981 /* Make sure the parent's table is up to date. */
2982 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
2983
2984 if (h->vtable_entries_used == NULL)
2985 {
2986 /* None of this table's entries were referenced. Re-use the
2987 parent's table. */
2988 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
2989 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
2990 }
2991 else
2992 {
2993 size_t n;
2994 bfd_boolean *cu, *pu;
2995
2996 /* Or the parent's entries into ours. */
2997 cu = h->vtable_entries_used;
2998 cu[-1] = TRUE;
2999 pu = h->vtable_parent->vtable_entries_used;
3000 if (pu != NULL)
3001 {
3002 const struct elf_backend_data *bed;
3003 unsigned int log_file_align;
3004
3005 bed = get_elf_backend_data (h->root.u.def.section->owner);
3006 log_file_align = bed->s->log_file_align;
3007 n = h->vtable_parent->vtable_entries_size >> log_file_align;
3008 while (n--)
3009 {
3010 if (*pu)
3011 *cu = TRUE;
3012 pu++;
3013 cu++;
3014 }
3015 }
3016 }
3017
3018 return TRUE;
3019 }
3020
3021 static bfd_boolean
3022 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
3023 {
3024 asection *sec;
3025 bfd_vma hstart, hend;
3026 Elf_Internal_Rela *relstart, *relend, *rel;
3027 const struct elf_backend_data *bed;
3028 unsigned int log_file_align;
3029
3030 if (h->root.type == bfd_link_hash_warning)
3031 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3032
3033 /* Take care of both those symbols that do not describe vtables as
3034 well as those that are not loaded. */
3035 if (h->vtable_parent == NULL)
3036 return TRUE;
3037
3038 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3039 || h->root.type == bfd_link_hash_defweak);
3040
3041 sec = h->root.u.def.section;
3042 hstart = h->root.u.def.value;
3043 hend = hstart + h->size;
3044
3045 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
3046 if (!relstart)
3047 return *(bfd_boolean *) okp = FALSE;
3048 bed = get_elf_backend_data (sec->owner);
3049 log_file_align = bed->s->log_file_align;
3050
3051 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
3052
3053 for (rel = relstart; rel < relend; ++rel)
3054 if (rel->r_offset >= hstart && rel->r_offset < hend)
3055 {
3056 /* If the entry is in use, do nothing. */
3057 if (h->vtable_entries_used
3058 && (rel->r_offset - hstart) < h->vtable_entries_size)
3059 {
3060 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
3061 if (h->vtable_entries_used[entry])
3062 continue;
3063 }
3064 /* Otherwise, kill it. */
3065 rel->r_offset = rel->r_info = rel->r_addend = 0;
3066 }
3067
3068 return TRUE;
3069 }
3070
3071 /* Do mark and sweep of unused sections. */
3072
3073 bfd_boolean
3074 elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
3075 {
3076 bfd_boolean ok = TRUE;
3077 bfd *sub;
3078 asection * (*gc_mark_hook)
3079 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
3080 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
3081
3082 if (!get_elf_backend_data (abfd)->can_gc_sections
3083 || info->relocatable
3084 || info->emitrelocations
3085 || !is_elf_hash_table (info->hash)
3086 || elf_hash_table (info)->dynamic_sections_created)
3087 {
3088 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
3089 return TRUE;
3090 }
3091
3092 /* Apply transitive closure to the vtable entry usage info. */
3093 elf_link_hash_traverse (elf_hash_table (info),
3094 elf_gc_propagate_vtable_entries_used,
3095 &ok);
3096 if (!ok)
3097 return FALSE;
3098
3099 /* Kill the vtable relocations that were not used. */
3100 elf_link_hash_traverse (elf_hash_table (info),
3101 elf_gc_smash_unused_vtentry_relocs,
3102 &ok);
3103 if (!ok)
3104 return FALSE;
3105
3106 /* Grovel through relocs to find out who stays ... */
3107
3108 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
3109 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3110 {
3111 asection *o;
3112
3113 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
3114 continue;
3115
3116 for (o = sub->sections; o != NULL; o = o->next)
3117 {
3118 if (o->flags & SEC_KEEP)
3119 if (!elf_gc_mark (info, o, gc_mark_hook))
3120 return FALSE;
3121 }
3122 }
3123
3124 /* ... and mark SEC_EXCLUDE for those that go. */
3125 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
3126 return FALSE;
3127
3128 return TRUE;
3129 }
3130 \f
3131 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
3132
3133 bfd_boolean
3134 elf_gc_record_vtinherit (bfd *abfd,
3135 asection *sec,
3136 struct elf_link_hash_entry *h,
3137 bfd_vma offset)
3138 {
3139 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3140 struct elf_link_hash_entry **search, *child;
3141 bfd_size_type extsymcount;
3142 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3143
3144 /* The sh_info field of the symtab header tells us where the
3145 external symbols start. We don't care about the local symbols at
3146 this point. */
3147 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
3148 if (!elf_bad_symtab (abfd))
3149 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
3150
3151 sym_hashes = elf_sym_hashes (abfd);
3152 sym_hashes_end = sym_hashes + extsymcount;
3153
3154 /* Hunt down the child symbol, which is in this section at the same
3155 offset as the relocation. */
3156 for (search = sym_hashes; search != sym_hashes_end; ++search)
3157 {
3158 if ((child = *search) != NULL
3159 && (child->root.type == bfd_link_hash_defined
3160 || child->root.type == bfd_link_hash_defweak)
3161 && child->root.u.def.section == sec
3162 && child->root.u.def.value == offset)
3163 goto win;
3164 }
3165
3166 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
3167 bfd_archive_filename (abfd), sec->name,
3168 (unsigned long) offset);
3169 bfd_set_error (bfd_error_invalid_operation);
3170 return FALSE;
3171
3172 win:
3173 if (!h)
3174 {
3175 /* This *should* only be the absolute section. It could potentially
3176 be that someone has defined a non-global vtable though, which
3177 would be bad. It isn't worth paging in the local symbols to be
3178 sure though; that case should simply be handled by the assembler. */
3179
3180 child->vtable_parent = (struct elf_link_hash_entry *) -1;
3181 }
3182 else
3183 child->vtable_parent = h;
3184
3185 return TRUE;
3186 }
3187
3188 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
3189
3190 bfd_boolean
3191 elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
3192 asection *sec ATTRIBUTE_UNUSED,
3193 struct elf_link_hash_entry *h,
3194 bfd_vma addend)
3195 {
3196 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3197 unsigned int log_file_align = bed->s->log_file_align;
3198
3199 if (addend >= h->vtable_entries_size)
3200 {
3201 size_t size, bytes, file_align;
3202 bfd_boolean *ptr = h->vtable_entries_used;
3203
3204 /* While the symbol is undefined, we have to be prepared to handle
3205 a zero size. */
3206 file_align = 1 << log_file_align;
3207 if (h->root.type == bfd_link_hash_undefined)
3208 size = addend + file_align;
3209 else
3210 {
3211 size = h->size;
3212 if (addend >= size)
3213 {
3214 /* Oops! We've got a reference past the defined end of
3215 the table. This is probably a bug -- shall we warn? */
3216 size = addend + file_align;
3217 }
3218 }
3219 size = (size + file_align - 1) & -file_align;
3220
3221 /* Allocate one extra entry for use as a "done" flag for the
3222 consolidation pass. */
3223 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
3224
3225 if (ptr)
3226 {
3227 ptr = bfd_realloc (ptr - 1, bytes);
3228
3229 if (ptr != NULL)
3230 {
3231 size_t oldbytes;
3232
3233 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
3234 * sizeof (bfd_boolean));
3235 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
3236 }
3237 }
3238 else
3239 ptr = bfd_zmalloc (bytes);
3240
3241 if (ptr == NULL)
3242 return FALSE;
3243
3244 /* And arrange for that done flag to be at index -1. */
3245 h->vtable_entries_used = ptr + 1;
3246 h->vtable_entries_size = size;
3247 }
3248
3249 h->vtable_entries_used[addend >> log_file_align] = TRUE;
3250
3251 return TRUE;
3252 }
3253
3254 struct alloc_got_off_arg {
3255 bfd_vma gotoff;
3256 unsigned int got_elt_size;
3257 };
3258
3259 /* And an accompanying bit to work out final got entry offsets once
3260 we're done. Should be called from final_link. */
3261
3262 bfd_boolean
3263 elf_gc_common_finalize_got_offsets (bfd *abfd,
3264 struct bfd_link_info *info)
3265 {
3266 bfd *i;
3267 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3268 bfd_vma gotoff;
3269 unsigned int got_elt_size = bed->s->arch_size / 8;
3270 struct alloc_got_off_arg gofarg;
3271
3272 if (! is_elf_hash_table (info->hash))
3273 return FALSE;
3274
3275 /* The GOT offset is relative to the .got section, but the GOT header is
3276 put into the .got.plt section, if the backend uses it. */
3277 if (bed->want_got_plt)
3278 gotoff = 0;
3279 else
3280 gotoff = bed->got_header_size;
3281
3282 /* Do the local .got entries first. */
3283 for (i = info->input_bfds; i; i = i->link_next)
3284 {
3285 bfd_signed_vma *local_got;
3286 bfd_size_type j, locsymcount;
3287 Elf_Internal_Shdr *symtab_hdr;
3288
3289 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
3290 continue;
3291
3292 local_got = elf_local_got_refcounts (i);
3293 if (!local_got)
3294 continue;
3295
3296 symtab_hdr = &elf_tdata (i)->symtab_hdr;
3297 if (elf_bad_symtab (i))
3298 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
3299 else
3300 locsymcount = symtab_hdr->sh_info;
3301
3302 for (j = 0; j < locsymcount; ++j)
3303 {
3304 if (local_got[j] > 0)
3305 {
3306 local_got[j] = gotoff;
3307 gotoff += got_elt_size;
3308 }
3309 else
3310 local_got[j] = (bfd_vma) -1;
3311 }
3312 }
3313
3314 /* Then the global .got entries. .plt refcounts are handled by
3315 adjust_dynamic_symbol */
3316 gofarg.gotoff = gotoff;
3317 gofarg.got_elt_size = got_elt_size;
3318 elf_link_hash_traverse (elf_hash_table (info),
3319 elf_gc_allocate_got_offsets,
3320 &gofarg);
3321 return TRUE;
3322 }
3323
3324 /* We need a special top-level link routine to convert got reference counts
3325 to real got offsets. */
3326
3327 static bfd_boolean
3328 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
3329 {
3330 struct alloc_got_off_arg *gofarg = arg;
3331
3332 if (h->root.type == bfd_link_hash_warning)
3333 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3334
3335 if (h->got.refcount > 0)
3336 {
3337 h->got.offset = gofarg->gotoff;
3338 gofarg->gotoff += gofarg->got_elt_size;
3339 }
3340 else
3341 h->got.offset = (bfd_vma) -1;
3342
3343 return TRUE;
3344 }
3345
3346 /* Many folk need no more in the way of final link than this, once
3347 got entry reference counting is enabled. */
3348
3349 bfd_boolean
3350 elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
3351 {
3352 if (!elf_gc_common_finalize_got_offsets (abfd, info))
3353 return FALSE;
3354
3355 /* Invoke the regular ELF backend linker to do all the work. */
3356 return elf_bfd_final_link (abfd, info);
3357 }
3358
3359 bfd_boolean
3360 elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
3361 {
3362 struct elf_reloc_cookie *rcookie = cookie;
3363
3364 if (rcookie->bad_symtab)
3365 rcookie->rel = rcookie->rels;
3366
3367 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
3368 {
3369 unsigned long r_symndx;
3370
3371 if (! rcookie->bad_symtab)
3372 if (rcookie->rel->r_offset > offset)
3373 return FALSE;
3374 if (rcookie->rel->r_offset != offset)
3375 continue;
3376
3377 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
3378 if (r_symndx == SHN_UNDEF)
3379 return TRUE;
3380
3381 if (r_symndx >= rcookie->locsymcount
3382 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
3383 {
3384 struct elf_link_hash_entry *h;
3385
3386 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
3387
3388 while (h->root.type == bfd_link_hash_indirect
3389 || h->root.type == bfd_link_hash_warning)
3390 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3391
3392 if ((h->root.type == bfd_link_hash_defined
3393 || h->root.type == bfd_link_hash_defweak)
3394 && elf_discarded_section (h->root.u.def.section))
3395 return TRUE;
3396 else
3397 return FALSE;
3398 }
3399 else
3400 {
3401 /* It's not a relocation against a global symbol,
3402 but it could be a relocation against a local
3403 symbol for a discarded section. */
3404 asection *isec;
3405 Elf_Internal_Sym *isym;
3406
3407 /* Need to: get the symbol; get the section. */
3408 isym = &rcookie->locsyms[r_symndx];
3409 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3410 {
3411 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
3412 if (isec != NULL && elf_discarded_section (isec))
3413 return TRUE;
3414 }
3415 }
3416 return FALSE;
3417 }
3418 return FALSE;
3419 }
3420
3421 /* Discard unneeded references to discarded sections.
3422 Returns TRUE if any section's size was changed. */
3423 /* This function assumes that the relocations are in sorted order,
3424 which is true for all known assemblers. */
3425
3426 bfd_boolean
3427 elf_bfd_discard_info (bfd *output_bfd, struct bfd_link_info *info)
3428 {
3429 struct elf_reloc_cookie cookie;
3430 asection *stab, *eh;
3431 Elf_Internal_Shdr *symtab_hdr;
3432 const struct elf_backend_data *bed;
3433 bfd *abfd;
3434 unsigned int count;
3435 bfd_boolean ret = FALSE;
3436
3437 if (info->traditional_format
3438 || !is_elf_hash_table (info->hash))
3439 return FALSE;
3440
3441 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
3442 {
3443 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
3444 continue;
3445
3446 bed = get_elf_backend_data (abfd);
3447
3448 if ((abfd->flags & DYNAMIC) != 0)
3449 continue;
3450
3451 eh = bfd_get_section_by_name (abfd, ".eh_frame");
3452 if (info->relocatable
3453 || (eh != NULL
3454 && (eh->_raw_size == 0
3455 || bfd_is_abs_section (eh->output_section))))
3456 eh = NULL;
3457
3458 stab = bfd_get_section_by_name (abfd, ".stab");
3459 if (stab != NULL
3460 && (stab->_raw_size == 0
3461 || bfd_is_abs_section (stab->output_section)
3462 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
3463 stab = NULL;
3464
3465 if (stab == NULL
3466 && eh == NULL
3467 && bed->elf_backend_discard_info == NULL)
3468 continue;
3469
3470 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3471 cookie.abfd = abfd;
3472 cookie.sym_hashes = elf_sym_hashes (abfd);
3473 cookie.bad_symtab = elf_bad_symtab (abfd);
3474 if (cookie.bad_symtab)
3475 {
3476 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
3477 cookie.extsymoff = 0;
3478 }
3479 else
3480 {
3481 cookie.locsymcount = symtab_hdr->sh_info;
3482 cookie.extsymoff = symtab_hdr->sh_info;
3483 }
3484
3485 if (bed->s->arch_size == 32)
3486 cookie.r_sym_shift = 8;
3487 else
3488 cookie.r_sym_shift = 32;
3489
3490 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3491 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
3492 {
3493 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3494 cookie.locsymcount, 0,
3495 NULL, NULL, NULL);
3496 if (cookie.locsyms == NULL)
3497 return FALSE;
3498 }
3499
3500 if (stab != NULL)
3501 {
3502 cookie.rels = NULL;
3503 count = stab->reloc_count;
3504 if (count != 0)
3505 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
3506 info->keep_memory);
3507 if (cookie.rels != NULL)
3508 {
3509 cookie.rel = cookie.rels;
3510 cookie.relend = cookie.rels;
3511 cookie.relend += count * bed->s->int_rels_per_ext_rel;
3512 if (_bfd_discard_section_stabs (abfd, stab,
3513 elf_section_data (stab)->sec_info,
3514 elf_reloc_symbol_deleted_p,
3515 &cookie))
3516 ret = TRUE;
3517 if (elf_section_data (stab)->relocs != cookie.rels)
3518 free (cookie.rels);
3519 }
3520 }
3521
3522 if (eh != NULL)
3523 {
3524 cookie.rels = NULL;
3525 count = eh->reloc_count;
3526 if (count != 0)
3527 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
3528 info->keep_memory);
3529 cookie.rel = cookie.rels;
3530 cookie.relend = cookie.rels;
3531 if (cookie.rels != NULL)
3532 cookie.relend += count * bed->s->int_rels_per_ext_rel;
3533
3534 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
3535 elf_reloc_symbol_deleted_p,
3536 &cookie))
3537 ret = TRUE;
3538
3539 if (cookie.rels != NULL
3540 && elf_section_data (eh)->relocs != cookie.rels)
3541 free (cookie.rels);
3542 }
3543
3544 if (bed->elf_backend_discard_info != NULL
3545 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
3546 ret = TRUE;
3547
3548 if (cookie.locsyms != NULL
3549 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
3550 {
3551 if (! info->keep_memory)
3552 free (cookie.locsyms);
3553 else
3554 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
3555 }
3556 }
3557
3558 if (info->eh_frame_hdr
3559 && !info->relocatable
3560 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
3561 ret = TRUE;
3562
3563 return ret;
3564 }
3565
3566 static bfd_boolean
3567 elf_section_ignore_discarded_relocs (asection *sec)
3568 {
3569 const struct elf_backend_data *bed;
3570
3571 switch (sec->sec_info_type)
3572 {
3573 case ELF_INFO_TYPE_STABS:
3574 case ELF_INFO_TYPE_EH_FRAME:
3575 return TRUE;
3576 default:
3577 break;
3578 }
3579
3580 bed = get_elf_backend_data (sec->owner);
3581 if (bed->elf_backend_ignore_discarded_relocs != NULL
3582 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
3583 return TRUE;
3584
3585 return FALSE;
3586 }
This page took 0.108505 seconds and 4 git commands to generate.