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