2004-05-14 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004 Free Software Foundation, Inc.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 /* SECTION
23
24 ELF backends
25
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
29
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet. */
33
34 /* For sparc64-cross-sparc32. */
35 #define _SYSCALL32
36 #include "bfd.h"
37 #include "sysdep.h"
38 #include "bfdlink.h"
39 #include "libbfd.h"
40 #define ARCH_SIZE 0
41 #include "elf-bfd.h"
42 #include "libiberty.h"
43
44 static int elf_sort_sections (const void *, const void *);
45 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
46 static bfd_boolean prep_headers (bfd *);
47 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
48 static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ;
49
50 /* Swap version information in and out. The version information is
51 currently size independent. If that ever changes, this code will
52 need to move into elfcode.h. */
53
54 /* Swap in a Verdef structure. */
55
56 void
57 _bfd_elf_swap_verdef_in (bfd *abfd,
58 const Elf_External_Verdef *src,
59 Elf_Internal_Verdef *dst)
60 {
61 dst->vd_version = H_GET_16 (abfd, src->vd_version);
62 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
63 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
64 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
65 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
66 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
67 dst->vd_next = H_GET_32 (abfd, src->vd_next);
68 }
69
70 /* Swap out a Verdef structure. */
71
72 void
73 _bfd_elf_swap_verdef_out (bfd *abfd,
74 const Elf_Internal_Verdef *src,
75 Elf_External_Verdef *dst)
76 {
77 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
78 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
79 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
80 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
81 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
82 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
83 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
84 }
85
86 /* Swap in a Verdaux structure. */
87
88 void
89 _bfd_elf_swap_verdaux_in (bfd *abfd,
90 const Elf_External_Verdaux *src,
91 Elf_Internal_Verdaux *dst)
92 {
93 dst->vda_name = H_GET_32 (abfd, src->vda_name);
94 dst->vda_next = H_GET_32 (abfd, src->vda_next);
95 }
96
97 /* Swap out a Verdaux structure. */
98
99 void
100 _bfd_elf_swap_verdaux_out (bfd *abfd,
101 const Elf_Internal_Verdaux *src,
102 Elf_External_Verdaux *dst)
103 {
104 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
105 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
106 }
107
108 /* Swap in a Verneed structure. */
109
110 void
111 _bfd_elf_swap_verneed_in (bfd *abfd,
112 const Elf_External_Verneed *src,
113 Elf_Internal_Verneed *dst)
114 {
115 dst->vn_version = H_GET_16 (abfd, src->vn_version);
116 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
117 dst->vn_file = H_GET_32 (abfd, src->vn_file);
118 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
119 dst->vn_next = H_GET_32 (abfd, src->vn_next);
120 }
121
122 /* Swap out a Verneed structure. */
123
124 void
125 _bfd_elf_swap_verneed_out (bfd *abfd,
126 const Elf_Internal_Verneed *src,
127 Elf_External_Verneed *dst)
128 {
129 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
130 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
131 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
132 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
133 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
134 }
135
136 /* Swap in a Vernaux structure. */
137
138 void
139 _bfd_elf_swap_vernaux_in (bfd *abfd,
140 const Elf_External_Vernaux *src,
141 Elf_Internal_Vernaux *dst)
142 {
143 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
144 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
145 dst->vna_other = H_GET_16 (abfd, src->vna_other);
146 dst->vna_name = H_GET_32 (abfd, src->vna_name);
147 dst->vna_next = H_GET_32 (abfd, src->vna_next);
148 }
149
150 /* Swap out a Vernaux structure. */
151
152 void
153 _bfd_elf_swap_vernaux_out (bfd *abfd,
154 const Elf_Internal_Vernaux *src,
155 Elf_External_Vernaux *dst)
156 {
157 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
158 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
159 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
160 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
161 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
162 }
163
164 /* Swap in a Versym structure. */
165
166 void
167 _bfd_elf_swap_versym_in (bfd *abfd,
168 const Elf_External_Versym *src,
169 Elf_Internal_Versym *dst)
170 {
171 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
172 }
173
174 /* Swap out a Versym structure. */
175
176 void
177 _bfd_elf_swap_versym_out (bfd *abfd,
178 const Elf_Internal_Versym *src,
179 Elf_External_Versym *dst)
180 {
181 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
182 }
183
184 /* Standard ELF hash function. Do not change this function; you will
185 cause invalid hash tables to be generated. */
186
187 unsigned long
188 bfd_elf_hash (const char *namearg)
189 {
190 const unsigned char *name = (const unsigned char *) namearg;
191 unsigned long h = 0;
192 unsigned long g;
193 int ch;
194
195 while ((ch = *name++) != '\0')
196 {
197 h = (h << 4) + ch;
198 if ((g = (h & 0xf0000000)) != 0)
199 {
200 h ^= g >> 24;
201 /* The ELF ABI says `h &= ~g', but this is equivalent in
202 this case and on some machines one insn instead of two. */
203 h ^= g;
204 }
205 }
206 return h & 0xffffffff;
207 }
208
209 /* Read a specified number of bytes at a specified offset in an ELF
210 file, into a newly allocated buffer, and return a pointer to the
211 buffer. */
212
213 static char *
214 elf_read (bfd *abfd, file_ptr offset, bfd_size_type size)
215 {
216 char *buf;
217
218 if ((buf = bfd_alloc (abfd, size)) == NULL)
219 return NULL;
220 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
221 return NULL;
222 if (bfd_bread (buf, size, abfd) != size)
223 {
224 if (bfd_get_error () != bfd_error_system_call)
225 bfd_set_error (bfd_error_file_truncated);
226 return NULL;
227 }
228 return buf;
229 }
230
231 bfd_boolean
232 bfd_elf_mkobject (bfd *abfd)
233 {
234 /* This just does initialization. */
235 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
236 elf_tdata (abfd) = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
237 if (elf_tdata (abfd) == 0)
238 return FALSE;
239 /* Since everything is done at close time, do we need any
240 initialization? */
241
242 return TRUE;
243 }
244
245 bfd_boolean
246 bfd_elf_mkcorefile (bfd *abfd)
247 {
248 /* I think this can be done just like an object file. */
249 return bfd_elf_mkobject (abfd);
250 }
251
252 char *
253 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
254 {
255 Elf_Internal_Shdr **i_shdrp;
256 char *shstrtab = NULL;
257 file_ptr offset;
258 bfd_size_type shstrtabsize;
259
260 i_shdrp = elf_elfsections (abfd);
261 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
262 return 0;
263
264 shstrtab = (char *) i_shdrp[shindex]->contents;
265 if (shstrtab == NULL)
266 {
267 /* No cached one, attempt to read, and cache what we read. */
268 offset = i_shdrp[shindex]->sh_offset;
269 shstrtabsize = i_shdrp[shindex]->sh_size;
270 shstrtab = elf_read (abfd, offset, shstrtabsize);
271 i_shdrp[shindex]->contents = shstrtab;
272 }
273 return shstrtab;
274 }
275
276 char *
277 bfd_elf_string_from_elf_section (bfd *abfd,
278 unsigned int shindex,
279 unsigned int strindex)
280 {
281 Elf_Internal_Shdr *hdr;
282
283 if (strindex == 0)
284 return "";
285
286 hdr = elf_elfsections (abfd)[shindex];
287
288 if (hdr->contents == NULL
289 && bfd_elf_get_str_section (abfd, shindex) == NULL)
290 return NULL;
291
292 if (strindex >= hdr->sh_size)
293 {
294 (*_bfd_error_handler)
295 (_("%s: invalid string offset %u >= %lu for section `%s'"),
296 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
297 ((shindex == elf_elfheader(abfd)->e_shstrndx
298 && strindex == hdr->sh_name)
299 ? ".shstrtab"
300 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
301 return "";
302 }
303
304 return ((char *) hdr->contents) + strindex;
305 }
306
307 /* Read and convert symbols to internal format.
308 SYMCOUNT specifies the number of symbols to read, starting from
309 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
310 are non-NULL, they are used to store the internal symbols, external
311 symbols, and symbol section index extensions, respectively. */
312
313 Elf_Internal_Sym *
314 bfd_elf_get_elf_syms (bfd *ibfd,
315 Elf_Internal_Shdr *symtab_hdr,
316 size_t symcount,
317 size_t symoffset,
318 Elf_Internal_Sym *intsym_buf,
319 void *extsym_buf,
320 Elf_External_Sym_Shndx *extshndx_buf)
321 {
322 Elf_Internal_Shdr *shndx_hdr;
323 void *alloc_ext;
324 const bfd_byte *esym;
325 Elf_External_Sym_Shndx *alloc_extshndx;
326 Elf_External_Sym_Shndx *shndx;
327 Elf_Internal_Sym *isym;
328 Elf_Internal_Sym *isymend;
329 const struct elf_backend_data *bed;
330 size_t extsym_size;
331 bfd_size_type amt;
332 file_ptr pos;
333
334 if (symcount == 0)
335 return intsym_buf;
336
337 /* Normal syms might have section extension entries. */
338 shndx_hdr = NULL;
339 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
340 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
341
342 /* Read the symbols. */
343 alloc_ext = NULL;
344 alloc_extshndx = NULL;
345 bed = get_elf_backend_data (ibfd);
346 extsym_size = bed->s->sizeof_sym;
347 amt = symcount * extsym_size;
348 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
349 if (extsym_buf == NULL)
350 {
351 alloc_ext = bfd_malloc (amt);
352 extsym_buf = alloc_ext;
353 }
354 if (extsym_buf == NULL
355 || bfd_seek (ibfd, pos, SEEK_SET) != 0
356 || bfd_bread (extsym_buf, amt, ibfd) != amt)
357 {
358 intsym_buf = NULL;
359 goto out;
360 }
361
362 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
363 extshndx_buf = NULL;
364 else
365 {
366 amt = symcount * sizeof (Elf_External_Sym_Shndx);
367 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
368 if (extshndx_buf == NULL)
369 {
370 alloc_extshndx = bfd_malloc (amt);
371 extshndx_buf = alloc_extshndx;
372 }
373 if (extshndx_buf == NULL
374 || bfd_seek (ibfd, pos, SEEK_SET) != 0
375 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
376 {
377 intsym_buf = NULL;
378 goto out;
379 }
380 }
381
382 if (intsym_buf == NULL)
383 {
384 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
385 intsym_buf = bfd_malloc (amt);
386 if (intsym_buf == NULL)
387 goto out;
388 }
389
390 /* Convert the symbols to internal form. */
391 isymend = intsym_buf + symcount;
392 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
393 isym < isymend;
394 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
395 (*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym);
396
397 out:
398 if (alloc_ext != NULL)
399 free (alloc_ext);
400 if (alloc_extshndx != NULL)
401 free (alloc_extshndx);
402
403 return intsym_buf;
404 }
405
406 /* Look up a symbol name. */
407 const char *
408 bfd_elf_local_sym_name (bfd *abfd, Elf_Internal_Sym *isym)
409 {
410 unsigned int iname = isym->st_name;
411 unsigned int shindex = elf_tdata (abfd)->symtab_hdr.sh_link;
412 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION)
413 {
414 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
415 shindex = elf_elfheader (abfd)->e_shstrndx;
416 }
417
418 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
419 }
420
421 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
422 sections. The first element is the flags, the rest are section
423 pointers. */
424
425 typedef union elf_internal_group {
426 Elf_Internal_Shdr *shdr;
427 unsigned int flags;
428 } Elf_Internal_Group;
429
430 /* Return the name of the group signature symbol. Why isn't the
431 signature just a string? */
432
433 static const char *
434 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
435 {
436 Elf_Internal_Shdr *hdr;
437 unsigned char esym[sizeof (Elf64_External_Sym)];
438 Elf_External_Sym_Shndx eshndx;
439 Elf_Internal_Sym isym;
440
441 /* First we need to ensure the symbol table is available. */
442 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
443 return NULL;
444
445 /* Go read the symbol. */
446 hdr = &elf_tdata (abfd)->symtab_hdr;
447 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
448 &isym, esym, &eshndx) == NULL)
449 return NULL;
450
451 return bfd_elf_local_sym_name (abfd, &isym);
452 }
453
454 /* Set next_in_group list pointer, and group name for NEWSECT. */
455
456 static bfd_boolean
457 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
458 {
459 unsigned int num_group = elf_tdata (abfd)->num_group;
460
461 /* If num_group is zero, read in all SHT_GROUP sections. The count
462 is set to -1 if there are no SHT_GROUP sections. */
463 if (num_group == 0)
464 {
465 unsigned int i, shnum;
466
467 /* First count the number of groups. If we have a SHT_GROUP
468 section with just a flag word (ie. sh_size is 4), ignore it. */
469 shnum = elf_numsections (abfd);
470 num_group = 0;
471 for (i = 0; i < shnum; i++)
472 {
473 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
474 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
475 num_group += 1;
476 }
477
478 if (num_group == 0)
479 num_group = (unsigned) -1;
480 elf_tdata (abfd)->num_group = num_group;
481
482 if (num_group > 0)
483 {
484 /* We keep a list of elf section headers for group sections,
485 so we can find them quickly. */
486 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
487 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
488 if (elf_tdata (abfd)->group_sect_ptr == NULL)
489 return FALSE;
490
491 num_group = 0;
492 for (i = 0; i < shnum; i++)
493 {
494 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
495 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
496 {
497 unsigned char *src;
498 Elf_Internal_Group *dest;
499
500 /* Add to list of sections. */
501 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
502 num_group += 1;
503
504 /* Read the raw contents. */
505 BFD_ASSERT (sizeof (*dest) >= 4);
506 amt = shdr->sh_size * sizeof (*dest) / 4;
507 shdr->contents = bfd_alloc (abfd, amt);
508 if (shdr->contents == NULL
509 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
510 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
511 != shdr->sh_size))
512 return FALSE;
513
514 /* Translate raw contents, a flag word followed by an
515 array of elf section indices all in target byte order,
516 to the flag word followed by an array of elf section
517 pointers. */
518 src = shdr->contents + shdr->sh_size;
519 dest = (Elf_Internal_Group *) (shdr->contents + amt);
520 while (1)
521 {
522 unsigned int idx;
523
524 src -= 4;
525 --dest;
526 idx = H_GET_32 (abfd, src);
527 if (src == shdr->contents)
528 {
529 dest->flags = idx;
530 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
531 shdr->bfd_section->flags
532 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
533 break;
534 }
535 if (idx >= shnum)
536 {
537 ((*_bfd_error_handler)
538 (_("%s: invalid SHT_GROUP entry"),
539 bfd_archive_filename (abfd)));
540 idx = 0;
541 }
542 dest->shdr = elf_elfsections (abfd)[idx];
543 }
544 }
545 }
546 }
547 }
548
549 if (num_group != (unsigned) -1)
550 {
551 unsigned int i;
552
553 for (i = 0; i < num_group; i++)
554 {
555 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
556 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
557 unsigned int n_elt = shdr->sh_size / 4;
558
559 /* Look through this group's sections to see if current
560 section is a member. */
561 while (--n_elt != 0)
562 if ((++idx)->shdr == hdr)
563 {
564 asection *s = NULL;
565
566 /* We are a member of this group. Go looking through
567 other members to see if any others are linked via
568 next_in_group. */
569 idx = (Elf_Internal_Group *) shdr->contents;
570 n_elt = shdr->sh_size / 4;
571 while (--n_elt != 0)
572 if ((s = (++idx)->shdr->bfd_section) != NULL
573 && elf_next_in_group (s) != NULL)
574 break;
575 if (n_elt != 0)
576 {
577 /* Snarf the group name from other member, and
578 insert current section in circular list. */
579 elf_group_name (newsect) = elf_group_name (s);
580 elf_next_in_group (newsect) = elf_next_in_group (s);
581 elf_next_in_group (s) = newsect;
582 }
583 else
584 {
585 const char *gname;
586
587 gname = group_signature (abfd, shdr);
588 if (gname == NULL)
589 return FALSE;
590 elf_group_name (newsect) = gname;
591
592 /* Start a circular list with one element. */
593 elf_next_in_group (newsect) = newsect;
594 }
595
596 /* If the group section has been created, point to the
597 new member. */
598 if (shdr->bfd_section != NULL)
599 elf_next_in_group (shdr->bfd_section) = newsect;
600
601 i = num_group - 1;
602 break;
603 }
604 }
605 }
606
607 if (elf_group_name (newsect) == NULL)
608 {
609 (*_bfd_error_handler) (_("%s: no group info for section %s"),
610 bfd_archive_filename (abfd), newsect->name);
611 }
612 return TRUE;
613 }
614
615 bfd_boolean
616 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
617 {
618 return elf_next_in_group (sec) != NULL;
619 }
620
621 bfd_boolean
622 bfd_elf_discard_group (bfd *abfd ATTRIBUTE_UNUSED, asection *group)
623 {
624 asection *first = elf_next_in_group (group);
625 asection *s = first;
626
627 while (s != NULL)
628 {
629 s->output_section = bfd_abs_section_ptr;
630 s = elf_next_in_group (s);
631 /* These lists are circular. */
632 if (s == first)
633 break;
634 }
635 return TRUE;
636 }
637
638 /* Make a BFD section from an ELF section. We store a pointer to the
639 BFD section in the bfd_section field of the header. */
640
641 bfd_boolean
642 _bfd_elf_make_section_from_shdr (bfd *abfd,
643 Elf_Internal_Shdr *hdr,
644 const char *name)
645 {
646 asection *newsect;
647 flagword flags;
648 const struct elf_backend_data *bed;
649
650 if (hdr->bfd_section != NULL)
651 {
652 BFD_ASSERT (strcmp (name,
653 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
654 return TRUE;
655 }
656
657 newsect = bfd_make_section_anyway (abfd, name);
658 if (newsect == NULL)
659 return FALSE;
660
661 hdr->bfd_section = newsect;
662 elf_section_data (newsect)->this_hdr = *hdr;
663
664 /* Always use the real type/flags. */
665 elf_section_type (newsect) = hdr->sh_type;
666 elf_section_flags (newsect) = hdr->sh_flags;
667
668 newsect->filepos = hdr->sh_offset;
669
670 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
671 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
672 || ! bfd_set_section_alignment (abfd, newsect,
673 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
674 return FALSE;
675
676 flags = SEC_NO_FLAGS;
677 if (hdr->sh_type != SHT_NOBITS)
678 flags |= SEC_HAS_CONTENTS;
679 if (hdr->sh_type == SHT_GROUP)
680 flags |= SEC_GROUP | SEC_EXCLUDE;
681 if ((hdr->sh_flags & SHF_ALLOC) != 0)
682 {
683 flags |= SEC_ALLOC;
684 if (hdr->sh_type != SHT_NOBITS)
685 flags |= SEC_LOAD;
686 }
687 if ((hdr->sh_flags & SHF_WRITE) == 0)
688 flags |= SEC_READONLY;
689 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
690 flags |= SEC_CODE;
691 else if ((flags & SEC_LOAD) != 0)
692 flags |= SEC_DATA;
693 if ((hdr->sh_flags & SHF_MERGE) != 0)
694 {
695 flags |= SEC_MERGE;
696 newsect->entsize = hdr->sh_entsize;
697 if ((hdr->sh_flags & SHF_STRINGS) != 0)
698 flags |= SEC_STRINGS;
699 }
700 if (hdr->sh_flags & SHF_GROUP)
701 if (!setup_group (abfd, hdr, newsect))
702 return FALSE;
703 if ((hdr->sh_flags & SHF_TLS) != 0)
704 flags |= SEC_THREAD_LOCAL;
705
706 /* The debugging sections appear to be recognized only by name, not
707 any sort of flag. */
708 {
709 static const char *debug_sec_names [] =
710 {
711 ".debug",
712 ".gnu.linkonce.wi.",
713 ".line",
714 ".stab"
715 };
716 int i;
717
718 for (i = ARRAY_SIZE (debug_sec_names); i--;)
719 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
720 break;
721
722 if (i >= 0)
723 flags |= SEC_DEBUGGING;
724 }
725
726 /* As a GNU extension, if the name begins with .gnu.linkonce, we
727 only link a single copy of the section. This is used to support
728 g++. g++ will emit each template expansion in its own section.
729 The symbols will be defined as weak, so that multiple definitions
730 are permitted. The GNU linker extension is to actually discard
731 all but one of the sections. */
732 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
733 && elf_next_in_group (newsect) == NULL)
734 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
735
736 bed = get_elf_backend_data (abfd);
737 if (bed->elf_backend_section_flags)
738 if (! bed->elf_backend_section_flags (&flags, hdr))
739 return FALSE;
740
741 if (! bfd_set_section_flags (abfd, newsect, flags))
742 return FALSE;
743
744 if ((flags & SEC_ALLOC) != 0)
745 {
746 Elf_Internal_Phdr *phdr;
747 unsigned int i;
748
749 /* Look through the phdrs to see if we need to adjust the lma.
750 If all the p_paddr fields are zero, we ignore them, since
751 some ELF linkers produce such output. */
752 phdr = elf_tdata (abfd)->phdr;
753 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
754 {
755 if (phdr->p_paddr != 0)
756 break;
757 }
758 if (i < elf_elfheader (abfd)->e_phnum)
759 {
760 phdr = elf_tdata (abfd)->phdr;
761 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
762 {
763 /* This section is part of this segment if its file
764 offset plus size lies within the segment's memory
765 span and, if the section is loaded, the extent of the
766 loaded data lies within the extent of the segment.
767
768 Note - we used to check the p_paddr field as well, and
769 refuse to set the LMA if it was 0. This is wrong
770 though, as a perfectly valid initialised segment can
771 have a p_paddr of zero. Some architectures, eg ARM,
772 place special significance on the address 0 and
773 executables need to be able to have a segment which
774 covers this address. */
775 if (phdr->p_type == PT_LOAD
776 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
777 && (hdr->sh_offset + hdr->sh_size
778 <= phdr->p_offset + phdr->p_memsz)
779 && ((flags & SEC_LOAD) == 0
780 || (hdr->sh_offset + hdr->sh_size
781 <= phdr->p_offset + phdr->p_filesz)))
782 {
783 if ((flags & SEC_LOAD) == 0)
784 newsect->lma = (phdr->p_paddr
785 + hdr->sh_addr - phdr->p_vaddr);
786 else
787 /* We used to use the same adjustment for SEC_LOAD
788 sections, but that doesn't work if the segment
789 is packed with code from multiple VMAs.
790 Instead we calculate the section LMA based on
791 the segment LMA. It is assumed that the
792 segment will contain sections with contiguous
793 LMAs, even if the VMAs are not. */
794 newsect->lma = (phdr->p_paddr
795 + hdr->sh_offset - phdr->p_offset);
796
797 /* With contiguous segments, we can't tell from file
798 offsets whether a section with zero size should
799 be placed at the end of one segment or the
800 beginning of the next. Decide based on vaddr. */
801 if (hdr->sh_addr >= phdr->p_vaddr
802 && (hdr->sh_addr + hdr->sh_size
803 <= phdr->p_vaddr + phdr->p_memsz))
804 break;
805 }
806 }
807 }
808 }
809
810 return TRUE;
811 }
812
813 /*
814 INTERNAL_FUNCTION
815 bfd_elf_find_section
816
817 SYNOPSIS
818 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
819
820 DESCRIPTION
821 Helper functions for GDB to locate the string tables.
822 Since BFD hides string tables from callers, GDB needs to use an
823 internal hook to find them. Sun's .stabstr, in particular,
824 isn't even pointed to by the .stab section, so ordinary
825 mechanisms wouldn't work to find it, even if we had some.
826 */
827
828 struct elf_internal_shdr *
829 bfd_elf_find_section (bfd *abfd, char *name)
830 {
831 Elf_Internal_Shdr **i_shdrp;
832 char *shstrtab;
833 unsigned int max;
834 unsigned int i;
835
836 i_shdrp = elf_elfsections (abfd);
837 if (i_shdrp != NULL)
838 {
839 shstrtab = bfd_elf_get_str_section (abfd,
840 elf_elfheader (abfd)->e_shstrndx);
841 if (shstrtab != NULL)
842 {
843 max = elf_numsections (abfd);
844 for (i = 1; i < max; i++)
845 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
846 return i_shdrp[i];
847 }
848 }
849 return 0;
850 }
851
852 const char *const bfd_elf_section_type_names[] = {
853 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
854 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
855 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
856 };
857
858 /* ELF relocs are against symbols. If we are producing relocatable
859 output, and the reloc is against an external symbol, and nothing
860 has given us any additional addend, the resulting reloc will also
861 be against the same symbol. In such a case, we don't want to
862 change anything about the way the reloc is handled, since it will
863 all be done at final link time. Rather than put special case code
864 into bfd_perform_relocation, all the reloc types use this howto
865 function. It just short circuits the reloc if producing
866 relocatable output against an external symbol. */
867
868 bfd_reloc_status_type
869 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
870 arelent *reloc_entry,
871 asymbol *symbol,
872 void *data ATTRIBUTE_UNUSED,
873 asection *input_section,
874 bfd *output_bfd,
875 char **error_message ATTRIBUTE_UNUSED)
876 {
877 if (output_bfd != NULL
878 && (symbol->flags & BSF_SECTION_SYM) == 0
879 && (! reloc_entry->howto->partial_inplace
880 || reloc_entry->addend == 0))
881 {
882 reloc_entry->address += input_section->output_offset;
883 return bfd_reloc_ok;
884 }
885
886 return bfd_reloc_continue;
887 }
888 \f
889 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
890
891 static void
892 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
893 asection *sec)
894 {
895 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
896 sec->sec_info_type = ELF_INFO_TYPE_NONE;
897 }
898
899 /* Finish SHF_MERGE section merging. */
900
901 bfd_boolean
902 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
903 {
904 if (!is_elf_hash_table (info->hash))
905 return FALSE;
906 if (elf_hash_table (info)->merge_info)
907 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
908 merge_sections_remove_hook);
909 return TRUE;
910 }
911
912 void
913 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
914 {
915 sec->output_section = bfd_abs_section_ptr;
916 sec->output_offset = sec->vma;
917 if (!is_elf_hash_table (info->hash))
918 return;
919
920 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
921 }
922 \f
923 /* Copy the program header and other data from one object module to
924 another. */
925
926 bfd_boolean
927 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
928 {
929 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
930 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
931 return TRUE;
932
933 BFD_ASSERT (!elf_flags_init (obfd)
934 || (elf_elfheader (obfd)->e_flags
935 == elf_elfheader (ibfd)->e_flags));
936
937 elf_gp (obfd) = elf_gp (ibfd);
938 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
939 elf_flags_init (obfd) = TRUE;
940 return TRUE;
941 }
942
943 /* Print out the program headers. */
944
945 bfd_boolean
946 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
947 {
948 FILE *f = farg;
949 Elf_Internal_Phdr *p;
950 asection *s;
951 bfd_byte *dynbuf = NULL;
952
953 p = elf_tdata (abfd)->phdr;
954 if (p != NULL)
955 {
956 unsigned int i, c;
957
958 fprintf (f, _("\nProgram Header:\n"));
959 c = elf_elfheader (abfd)->e_phnum;
960 for (i = 0; i < c; i++, p++)
961 {
962 const char *pt;
963 char buf[20];
964
965 switch (p->p_type)
966 {
967 case PT_NULL: pt = "NULL"; break;
968 case PT_LOAD: pt = "LOAD"; break;
969 case PT_DYNAMIC: pt = "DYNAMIC"; break;
970 case PT_INTERP: pt = "INTERP"; break;
971 case PT_NOTE: pt = "NOTE"; break;
972 case PT_SHLIB: pt = "SHLIB"; break;
973 case PT_PHDR: pt = "PHDR"; break;
974 case PT_TLS: pt = "TLS"; break;
975 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
976 case PT_GNU_STACK: pt = "STACK"; break;
977 case PT_GNU_RELRO: pt = "RELRO"; break;
978 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
979 }
980 fprintf (f, "%8s off 0x", pt);
981 bfd_fprintf_vma (abfd, f, p->p_offset);
982 fprintf (f, " vaddr 0x");
983 bfd_fprintf_vma (abfd, f, p->p_vaddr);
984 fprintf (f, " paddr 0x");
985 bfd_fprintf_vma (abfd, f, p->p_paddr);
986 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
987 fprintf (f, " filesz 0x");
988 bfd_fprintf_vma (abfd, f, p->p_filesz);
989 fprintf (f, " memsz 0x");
990 bfd_fprintf_vma (abfd, f, p->p_memsz);
991 fprintf (f, " flags %c%c%c",
992 (p->p_flags & PF_R) != 0 ? 'r' : '-',
993 (p->p_flags & PF_W) != 0 ? 'w' : '-',
994 (p->p_flags & PF_X) != 0 ? 'x' : '-');
995 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
996 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
997 fprintf (f, "\n");
998 }
999 }
1000
1001 s = bfd_get_section_by_name (abfd, ".dynamic");
1002 if (s != NULL)
1003 {
1004 int elfsec;
1005 unsigned long shlink;
1006 bfd_byte *extdyn, *extdynend;
1007 size_t extdynsize;
1008 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1009
1010 fprintf (f, _("\nDynamic Section:\n"));
1011
1012 dynbuf = bfd_malloc (s->_raw_size);
1013 if (dynbuf == NULL)
1014 goto error_return;
1015 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
1016 goto error_return;
1017
1018 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1019 if (elfsec == -1)
1020 goto error_return;
1021 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1022
1023 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1024 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1025
1026 extdyn = dynbuf;
1027 extdynend = extdyn + s->_raw_size;
1028 for (; extdyn < extdynend; extdyn += extdynsize)
1029 {
1030 Elf_Internal_Dyn dyn;
1031 const char *name;
1032 char ab[20];
1033 bfd_boolean stringp;
1034
1035 (*swap_dyn_in) (abfd, extdyn, &dyn);
1036
1037 if (dyn.d_tag == DT_NULL)
1038 break;
1039
1040 stringp = FALSE;
1041 switch (dyn.d_tag)
1042 {
1043 default:
1044 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1045 name = ab;
1046 break;
1047
1048 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1049 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1050 case DT_PLTGOT: name = "PLTGOT"; break;
1051 case DT_HASH: name = "HASH"; break;
1052 case DT_STRTAB: name = "STRTAB"; break;
1053 case DT_SYMTAB: name = "SYMTAB"; break;
1054 case DT_RELA: name = "RELA"; break;
1055 case DT_RELASZ: name = "RELASZ"; break;
1056 case DT_RELAENT: name = "RELAENT"; break;
1057 case DT_STRSZ: name = "STRSZ"; break;
1058 case DT_SYMENT: name = "SYMENT"; break;
1059 case DT_INIT: name = "INIT"; break;
1060 case DT_FINI: name = "FINI"; break;
1061 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1062 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1063 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1064 case DT_REL: name = "REL"; break;
1065 case DT_RELSZ: name = "RELSZ"; break;
1066 case DT_RELENT: name = "RELENT"; break;
1067 case DT_PLTREL: name = "PLTREL"; break;
1068 case DT_DEBUG: name = "DEBUG"; break;
1069 case DT_TEXTREL: name = "TEXTREL"; break;
1070 case DT_JMPREL: name = "JMPREL"; break;
1071 case DT_BIND_NOW: name = "BIND_NOW"; break;
1072 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1073 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1074 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1075 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1076 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1077 case DT_FLAGS: name = "FLAGS"; break;
1078 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1079 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1080 case DT_CHECKSUM: name = "CHECKSUM"; break;
1081 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1082 case DT_MOVEENT: name = "MOVEENT"; break;
1083 case DT_MOVESZ: name = "MOVESZ"; break;
1084 case DT_FEATURE: name = "FEATURE"; break;
1085 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1086 case DT_SYMINSZ: name = "SYMINSZ"; break;
1087 case DT_SYMINENT: name = "SYMINENT"; break;
1088 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1089 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1090 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1091 case DT_PLTPAD: name = "PLTPAD"; break;
1092 case DT_MOVETAB: name = "MOVETAB"; break;
1093 case DT_SYMINFO: name = "SYMINFO"; break;
1094 case DT_RELACOUNT: name = "RELACOUNT"; break;
1095 case DT_RELCOUNT: name = "RELCOUNT"; break;
1096 case DT_FLAGS_1: name = "FLAGS_1"; break;
1097 case DT_VERSYM: name = "VERSYM"; break;
1098 case DT_VERDEF: name = "VERDEF"; break;
1099 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1100 case DT_VERNEED: name = "VERNEED"; break;
1101 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1102 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1103 case DT_USED: name = "USED"; break;
1104 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1105 }
1106
1107 fprintf (f, " %-11s ", name);
1108 if (! stringp)
1109 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1110 else
1111 {
1112 const char *string;
1113 unsigned int tagv = dyn.d_un.d_val;
1114
1115 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1116 if (string == NULL)
1117 goto error_return;
1118 fprintf (f, "%s", string);
1119 }
1120 fprintf (f, "\n");
1121 }
1122
1123 free (dynbuf);
1124 dynbuf = NULL;
1125 }
1126
1127 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1128 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1129 {
1130 if (! _bfd_elf_slurp_version_tables (abfd))
1131 return FALSE;
1132 }
1133
1134 if (elf_dynverdef (abfd) != 0)
1135 {
1136 Elf_Internal_Verdef *t;
1137
1138 fprintf (f, _("\nVersion definitions:\n"));
1139 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1140 {
1141 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1142 t->vd_flags, t->vd_hash, t->vd_nodename);
1143 if (t->vd_auxptr->vda_nextptr != NULL)
1144 {
1145 Elf_Internal_Verdaux *a;
1146
1147 fprintf (f, "\t");
1148 for (a = t->vd_auxptr->vda_nextptr;
1149 a != NULL;
1150 a = a->vda_nextptr)
1151 fprintf (f, "%s ", a->vda_nodename);
1152 fprintf (f, "\n");
1153 }
1154 }
1155 }
1156
1157 if (elf_dynverref (abfd) != 0)
1158 {
1159 Elf_Internal_Verneed *t;
1160
1161 fprintf (f, _("\nVersion References:\n"));
1162 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1163 {
1164 Elf_Internal_Vernaux *a;
1165
1166 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1167 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1168 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1169 a->vna_flags, a->vna_other, a->vna_nodename);
1170 }
1171 }
1172
1173 return TRUE;
1174
1175 error_return:
1176 if (dynbuf != NULL)
1177 free (dynbuf);
1178 return FALSE;
1179 }
1180
1181 /* Display ELF-specific fields of a symbol. */
1182
1183 void
1184 bfd_elf_print_symbol (bfd *abfd,
1185 void *filep,
1186 asymbol *symbol,
1187 bfd_print_symbol_type how)
1188 {
1189 FILE *file = filep;
1190 switch (how)
1191 {
1192 case bfd_print_symbol_name:
1193 fprintf (file, "%s", symbol->name);
1194 break;
1195 case bfd_print_symbol_more:
1196 fprintf (file, "elf ");
1197 bfd_fprintf_vma (abfd, file, symbol->value);
1198 fprintf (file, " %lx", (long) symbol->flags);
1199 break;
1200 case bfd_print_symbol_all:
1201 {
1202 const char *section_name;
1203 const char *name = NULL;
1204 const struct elf_backend_data *bed;
1205 unsigned char st_other;
1206 bfd_vma val;
1207
1208 section_name = symbol->section ? symbol->section->name : "(*none*)";
1209
1210 bed = get_elf_backend_data (abfd);
1211 if (bed->elf_backend_print_symbol_all)
1212 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1213
1214 if (name == NULL)
1215 {
1216 name = symbol->name;
1217 bfd_print_symbol_vandf (abfd, file, symbol);
1218 }
1219
1220 fprintf (file, " %s\t", section_name);
1221 /* Print the "other" value for a symbol. For common symbols,
1222 we've already printed the size; now print the alignment.
1223 For other symbols, we have no specified alignment, and
1224 we've printed the address; now print the size. */
1225 if (bfd_is_com_section (symbol->section))
1226 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1227 else
1228 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1229 bfd_fprintf_vma (abfd, file, val);
1230
1231 /* If we have version information, print it. */
1232 if (elf_tdata (abfd)->dynversym_section != 0
1233 && (elf_tdata (abfd)->dynverdef_section != 0
1234 || elf_tdata (abfd)->dynverref_section != 0))
1235 {
1236 unsigned int vernum;
1237 const char *version_string;
1238
1239 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1240
1241 if (vernum == 0)
1242 version_string = "";
1243 else if (vernum == 1)
1244 version_string = "Base";
1245 else if (vernum <= elf_tdata (abfd)->cverdefs)
1246 version_string =
1247 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1248 else
1249 {
1250 Elf_Internal_Verneed *t;
1251
1252 version_string = "";
1253 for (t = elf_tdata (abfd)->verref;
1254 t != NULL;
1255 t = t->vn_nextref)
1256 {
1257 Elf_Internal_Vernaux *a;
1258
1259 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1260 {
1261 if (a->vna_other == vernum)
1262 {
1263 version_string = a->vna_nodename;
1264 break;
1265 }
1266 }
1267 }
1268 }
1269
1270 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1271 fprintf (file, " %-11s", version_string);
1272 else
1273 {
1274 int i;
1275
1276 fprintf (file, " (%s)", version_string);
1277 for (i = 10 - strlen (version_string); i > 0; --i)
1278 putc (' ', file);
1279 }
1280 }
1281
1282 /* If the st_other field is not zero, print it. */
1283 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1284
1285 switch (st_other)
1286 {
1287 case 0: break;
1288 case STV_INTERNAL: fprintf (file, " .internal"); break;
1289 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1290 case STV_PROTECTED: fprintf (file, " .protected"); break;
1291 default:
1292 /* Some other non-defined flags are also present, so print
1293 everything hex. */
1294 fprintf (file, " 0x%02x", (unsigned int) st_other);
1295 }
1296
1297 fprintf (file, " %s", name);
1298 }
1299 break;
1300 }
1301 }
1302 \f
1303 /* Create an entry in an ELF linker hash table. */
1304
1305 struct bfd_hash_entry *
1306 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1307 struct bfd_hash_table *table,
1308 const char *string)
1309 {
1310 /* Allocate the structure if it has not already been allocated by a
1311 subclass. */
1312 if (entry == NULL)
1313 {
1314 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1315 if (entry == NULL)
1316 return entry;
1317 }
1318
1319 /* Call the allocation method of the superclass. */
1320 entry = _bfd_link_hash_newfunc (entry, table, string);
1321 if (entry != NULL)
1322 {
1323 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1324 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1325
1326 /* Set local fields. */
1327 ret->indx = -1;
1328 ret->dynindx = -1;
1329 ret->dynstr_index = 0;
1330 ret->elf_hash_value = 0;
1331 ret->weakdef = NULL;
1332 ret->verinfo.verdef = NULL;
1333 ret->vtable_entries_size = 0;
1334 ret->vtable_entries_used = NULL;
1335 ret->vtable_parent = NULL;
1336 ret->got = htab->init_refcount;
1337 ret->plt = htab->init_refcount;
1338 ret->size = 0;
1339 ret->type = STT_NOTYPE;
1340 ret->other = 0;
1341 /* Assume that we have been called by a non-ELF symbol reader.
1342 This flag is then reset by the code which reads an ELF input
1343 file. This ensures that a symbol created by a non-ELF symbol
1344 reader will have the flag set correctly. */
1345 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1346 }
1347
1348 return entry;
1349 }
1350
1351 /* Copy data from an indirect symbol to its direct symbol, hiding the
1352 old indirect symbol. Also used for copying flags to a weakdef. */
1353
1354 void
1355 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data *bed,
1356 struct elf_link_hash_entry *dir,
1357 struct elf_link_hash_entry *ind)
1358 {
1359 bfd_signed_vma tmp;
1360 bfd_signed_vma lowest_valid = bed->can_refcount;
1361
1362 /* Copy down any references that we may have already seen to the
1363 symbol which just became indirect. */
1364
1365 dir->elf_link_hash_flags
1366 |= ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
1367 | ELF_LINK_HASH_REF_REGULAR
1368 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1369 | ELF_LINK_NON_GOT_REF
1370 | ELF_LINK_HASH_NEEDS_PLT
1371 | ELF_LINK_POINTER_EQUALITY_NEEDED);
1372
1373 if (ind->root.type != bfd_link_hash_indirect)
1374 return;
1375
1376 /* Copy over the global and procedure linkage table refcount entries.
1377 These may have been already set up by a check_relocs routine. */
1378 tmp = dir->got.refcount;
1379 if (tmp < lowest_valid)
1380 {
1381 dir->got.refcount = ind->got.refcount;
1382 ind->got.refcount = tmp;
1383 }
1384 else
1385 BFD_ASSERT (ind->got.refcount < lowest_valid);
1386
1387 tmp = dir->plt.refcount;
1388 if (tmp < lowest_valid)
1389 {
1390 dir->plt.refcount = ind->plt.refcount;
1391 ind->plt.refcount = tmp;
1392 }
1393 else
1394 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1395
1396 if (dir->dynindx == -1)
1397 {
1398 dir->dynindx = ind->dynindx;
1399 dir->dynstr_index = ind->dynstr_index;
1400 ind->dynindx = -1;
1401 ind->dynstr_index = 0;
1402 }
1403 else
1404 BFD_ASSERT (ind->dynindx == -1);
1405 }
1406
1407 void
1408 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
1409 struct elf_link_hash_entry *h,
1410 bfd_boolean force_local)
1411 {
1412 h->plt = elf_hash_table (info)->init_offset;
1413 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1414 if (force_local)
1415 {
1416 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1417 if (h->dynindx != -1)
1418 {
1419 h->dynindx = -1;
1420 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1421 h->dynstr_index);
1422 }
1423 }
1424 }
1425
1426 /* Initialize an ELF linker hash table. */
1427
1428 bfd_boolean
1429 _bfd_elf_link_hash_table_init
1430 (struct elf_link_hash_table *table,
1431 bfd *abfd,
1432 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
1433 struct bfd_hash_table *,
1434 const char *))
1435 {
1436 bfd_boolean ret;
1437
1438 table->dynamic_sections_created = FALSE;
1439 table->dynobj = NULL;
1440 /* Make sure can_refcount is extended to the width and signedness of
1441 init_refcount before we subtract one from it. */
1442 table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1443 table->init_refcount.refcount -= 1;
1444 table->init_offset.offset = -(bfd_vma) 1;
1445 /* The first dynamic symbol is a dummy. */
1446 table->dynsymcount = 1;
1447 table->dynstr = NULL;
1448 table->bucketcount = 0;
1449 table->needed = NULL;
1450 table->hgot = NULL;
1451 table->stab_info = NULL;
1452 table->merge_info = NULL;
1453 memset (&table->eh_info, 0, sizeof (table->eh_info));
1454 table->dynlocal = NULL;
1455 table->runpath = NULL;
1456 table->tls_sec = NULL;
1457 table->tls_size = 0;
1458 table->loaded = NULL;
1459
1460 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1461 table->root.type = bfd_link_elf_hash_table;
1462
1463 return ret;
1464 }
1465
1466 /* Create an ELF linker hash table. */
1467
1468 struct bfd_link_hash_table *
1469 _bfd_elf_link_hash_table_create (bfd *abfd)
1470 {
1471 struct elf_link_hash_table *ret;
1472 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1473
1474 ret = bfd_malloc (amt);
1475 if (ret == NULL)
1476 return NULL;
1477
1478 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1479 {
1480 free (ret);
1481 return NULL;
1482 }
1483
1484 return &ret->root;
1485 }
1486
1487 /* This is a hook for the ELF emulation code in the generic linker to
1488 tell the backend linker what file name to use for the DT_NEEDED
1489 entry for a dynamic object. */
1490
1491 void
1492 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
1493 {
1494 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1495 && bfd_get_format (abfd) == bfd_object)
1496 elf_dt_name (abfd) = name;
1497 }
1498
1499 void
1500 bfd_elf_set_dyn_lib_class (bfd *abfd, int lib_class)
1501 {
1502 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1503 && bfd_get_format (abfd) == bfd_object)
1504 elf_dyn_lib_class (abfd) = lib_class;
1505 }
1506
1507 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1508 the linker ELF emulation code. */
1509
1510 struct bfd_link_needed_list *
1511 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1512 struct bfd_link_info *info)
1513 {
1514 if (! is_elf_hash_table (info->hash))
1515 return NULL;
1516 return elf_hash_table (info)->needed;
1517 }
1518
1519 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1520 hook for the linker ELF emulation code. */
1521
1522 struct bfd_link_needed_list *
1523 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
1524 struct bfd_link_info *info)
1525 {
1526 if (! is_elf_hash_table (info->hash))
1527 return NULL;
1528 return elf_hash_table (info)->runpath;
1529 }
1530
1531 /* Get the name actually used for a dynamic object for a link. This
1532 is the SONAME entry if there is one. Otherwise, it is the string
1533 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1534
1535 const char *
1536 bfd_elf_get_dt_soname (bfd *abfd)
1537 {
1538 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1539 && bfd_get_format (abfd) == bfd_object)
1540 return elf_dt_name (abfd);
1541 return NULL;
1542 }
1543
1544 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1545 the ELF linker emulation code. */
1546
1547 bfd_boolean
1548 bfd_elf_get_bfd_needed_list (bfd *abfd,
1549 struct bfd_link_needed_list **pneeded)
1550 {
1551 asection *s;
1552 bfd_byte *dynbuf = NULL;
1553 int elfsec;
1554 unsigned long shlink;
1555 bfd_byte *extdyn, *extdynend;
1556 size_t extdynsize;
1557 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1558
1559 *pneeded = NULL;
1560
1561 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1562 || bfd_get_format (abfd) != bfd_object)
1563 return TRUE;
1564
1565 s = bfd_get_section_by_name (abfd, ".dynamic");
1566 if (s == NULL || s->_raw_size == 0)
1567 return TRUE;
1568
1569 dynbuf = bfd_malloc (s->_raw_size);
1570 if (dynbuf == NULL)
1571 goto error_return;
1572
1573 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
1574 goto error_return;
1575
1576 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1577 if (elfsec == -1)
1578 goto error_return;
1579
1580 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1581
1582 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1583 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1584
1585 extdyn = dynbuf;
1586 extdynend = extdyn + s->_raw_size;
1587 for (; extdyn < extdynend; extdyn += extdynsize)
1588 {
1589 Elf_Internal_Dyn dyn;
1590
1591 (*swap_dyn_in) (abfd, extdyn, &dyn);
1592
1593 if (dyn.d_tag == DT_NULL)
1594 break;
1595
1596 if (dyn.d_tag == DT_NEEDED)
1597 {
1598 const char *string;
1599 struct bfd_link_needed_list *l;
1600 unsigned int tagv = dyn.d_un.d_val;
1601 bfd_size_type amt;
1602
1603 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1604 if (string == NULL)
1605 goto error_return;
1606
1607 amt = sizeof *l;
1608 l = bfd_alloc (abfd, amt);
1609 if (l == NULL)
1610 goto error_return;
1611
1612 l->by = abfd;
1613 l->name = string;
1614 l->next = *pneeded;
1615 *pneeded = l;
1616 }
1617 }
1618
1619 free (dynbuf);
1620
1621 return TRUE;
1622
1623 error_return:
1624 if (dynbuf != NULL)
1625 free (dynbuf);
1626 return FALSE;
1627 }
1628 \f
1629 /* Allocate an ELF string table--force the first byte to be zero. */
1630
1631 struct bfd_strtab_hash *
1632 _bfd_elf_stringtab_init (void)
1633 {
1634 struct bfd_strtab_hash *ret;
1635
1636 ret = _bfd_stringtab_init ();
1637 if (ret != NULL)
1638 {
1639 bfd_size_type loc;
1640
1641 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1642 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1643 if (loc == (bfd_size_type) -1)
1644 {
1645 _bfd_stringtab_free (ret);
1646 ret = NULL;
1647 }
1648 }
1649 return ret;
1650 }
1651 \f
1652 /* ELF .o/exec file reading */
1653
1654 /* Create a new bfd section from an ELF section header. */
1655
1656 bfd_boolean
1657 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1658 {
1659 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1660 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1661 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1662 const char *name;
1663
1664 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1665
1666 switch (hdr->sh_type)
1667 {
1668 case SHT_NULL:
1669 /* Inactive section. Throw it away. */
1670 return TRUE;
1671
1672 case SHT_PROGBITS: /* Normal section with contents. */
1673 case SHT_NOBITS: /* .bss section. */
1674 case SHT_HASH: /* .hash section. */
1675 case SHT_NOTE: /* .note section. */
1676 case SHT_INIT_ARRAY: /* .init_array section. */
1677 case SHT_FINI_ARRAY: /* .fini_array section. */
1678 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1679 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1680
1681 case SHT_DYNAMIC: /* Dynamic linking information. */
1682 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1683 return FALSE;
1684 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1685 {
1686 Elf_Internal_Shdr *dynsymhdr;
1687
1688 /* The shared libraries distributed with hpux11 have a bogus
1689 sh_link field for the ".dynamic" section. Find the
1690 string table for the ".dynsym" section instead. */
1691 if (elf_dynsymtab (abfd) != 0)
1692 {
1693 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1694 hdr->sh_link = dynsymhdr->sh_link;
1695 }
1696 else
1697 {
1698 unsigned int i, num_sec;
1699
1700 num_sec = elf_numsections (abfd);
1701 for (i = 1; i < num_sec; i++)
1702 {
1703 dynsymhdr = elf_elfsections (abfd)[i];
1704 if (dynsymhdr->sh_type == SHT_DYNSYM)
1705 {
1706 hdr->sh_link = dynsymhdr->sh_link;
1707 break;
1708 }
1709 }
1710 }
1711 }
1712 break;
1713
1714 case SHT_SYMTAB: /* A symbol table */
1715 if (elf_onesymtab (abfd) == shindex)
1716 return TRUE;
1717
1718 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1719 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1720 elf_onesymtab (abfd) = shindex;
1721 elf_tdata (abfd)->symtab_hdr = *hdr;
1722 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1723 abfd->flags |= HAS_SYMS;
1724
1725 /* Sometimes a shared object will map in the symbol table. If
1726 SHF_ALLOC is set, and this is a shared object, then we also
1727 treat this section as a BFD section. We can not base the
1728 decision purely on SHF_ALLOC, because that flag is sometimes
1729 set in a relocatable object file, which would confuse the
1730 linker. */
1731 if ((hdr->sh_flags & SHF_ALLOC) != 0
1732 && (abfd->flags & DYNAMIC) != 0
1733 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1734 return FALSE;
1735
1736 return TRUE;
1737
1738 case SHT_DYNSYM: /* A dynamic symbol table */
1739 if (elf_dynsymtab (abfd) == shindex)
1740 return TRUE;
1741
1742 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1743 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1744 elf_dynsymtab (abfd) = shindex;
1745 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1746 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1747 abfd->flags |= HAS_SYMS;
1748
1749 /* Besides being a symbol table, we also treat this as a regular
1750 section, so that objcopy can handle it. */
1751 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1752
1753 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1754 if (elf_symtab_shndx (abfd) == shindex)
1755 return TRUE;
1756
1757 /* Get the associated symbol table. */
1758 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1759 || hdr->sh_link != elf_onesymtab (abfd))
1760 return FALSE;
1761
1762 elf_symtab_shndx (abfd) = shindex;
1763 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1764 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1765 return TRUE;
1766
1767 case SHT_STRTAB: /* A string table */
1768 if (hdr->bfd_section != NULL)
1769 return TRUE;
1770 if (ehdr->e_shstrndx == shindex)
1771 {
1772 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1773 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1774 return TRUE;
1775 }
1776 {
1777 unsigned int i, num_sec;
1778
1779 num_sec = elf_numsections (abfd);
1780 for (i = 1; i < num_sec; i++)
1781 {
1782 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1783 if (hdr2->sh_link == shindex)
1784 {
1785 if (! bfd_section_from_shdr (abfd, i))
1786 return FALSE;
1787 if (elf_onesymtab (abfd) == i)
1788 {
1789 elf_tdata (abfd)->strtab_hdr = *hdr;
1790 elf_elfsections (abfd)[shindex] =
1791 &elf_tdata (abfd)->strtab_hdr;
1792 return TRUE;
1793 }
1794 if (elf_dynsymtab (abfd) == i)
1795 {
1796 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1797 elf_elfsections (abfd)[shindex] = hdr =
1798 &elf_tdata (abfd)->dynstrtab_hdr;
1799 /* We also treat this as a regular section, so
1800 that objcopy can handle it. */
1801 break;
1802 }
1803 #if 0 /* Not handling other string tables specially right now. */
1804 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1805 /* We have a strtab for some random other section. */
1806 newsect = (asection *) hdr2->bfd_section;
1807 if (!newsect)
1808 break;
1809 hdr->bfd_section = newsect;
1810 hdr2 = &elf_section_data (newsect)->str_hdr;
1811 *hdr2 = *hdr;
1812 elf_elfsections (abfd)[shindex] = hdr2;
1813 #endif
1814 }
1815 }
1816 }
1817
1818 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1819
1820 case SHT_REL:
1821 case SHT_RELA:
1822 /* *These* do a lot of work -- but build no sections! */
1823 {
1824 asection *target_sect;
1825 Elf_Internal_Shdr *hdr2;
1826 unsigned int num_sec = elf_numsections (abfd);
1827
1828 /* Check for a bogus link to avoid crashing. */
1829 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1830 || hdr->sh_link >= num_sec)
1831 {
1832 ((*_bfd_error_handler)
1833 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1834 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1835 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1836 }
1837
1838 /* For some incomprehensible reason Oracle distributes
1839 libraries for Solaris in which some of the objects have
1840 bogus sh_link fields. It would be nice if we could just
1841 reject them, but, unfortunately, some people need to use
1842 them. We scan through the section headers; if we find only
1843 one suitable symbol table, we clobber the sh_link to point
1844 to it. I hope this doesn't break anything. */
1845 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1846 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1847 {
1848 unsigned int scan;
1849 int found;
1850
1851 found = 0;
1852 for (scan = 1; scan < num_sec; scan++)
1853 {
1854 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1855 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1856 {
1857 if (found != 0)
1858 {
1859 found = 0;
1860 break;
1861 }
1862 found = scan;
1863 }
1864 }
1865 if (found != 0)
1866 hdr->sh_link = found;
1867 }
1868
1869 /* Get the symbol table. */
1870 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1871 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1872 return FALSE;
1873
1874 /* If this reloc section does not use the main symbol table we
1875 don't treat it as a reloc section. BFD can't adequately
1876 represent such a section, so at least for now, we don't
1877 try. We just present it as a normal section. We also
1878 can't use it as a reloc section if it points to the null
1879 section. */
1880 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1881 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1882
1883 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1884 return FALSE;
1885 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1886 if (target_sect == NULL)
1887 return FALSE;
1888
1889 if ((target_sect->flags & SEC_RELOC) == 0
1890 || target_sect->reloc_count == 0)
1891 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1892 else
1893 {
1894 bfd_size_type amt;
1895 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1896 amt = sizeof (*hdr2);
1897 hdr2 = bfd_alloc (abfd, amt);
1898 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1899 }
1900 *hdr2 = *hdr;
1901 elf_elfsections (abfd)[shindex] = hdr2;
1902 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1903 target_sect->flags |= SEC_RELOC;
1904 target_sect->relocation = NULL;
1905 target_sect->rel_filepos = hdr->sh_offset;
1906 /* In the section to which the relocations apply, mark whether
1907 its relocations are of the REL or RELA variety. */
1908 if (hdr->sh_size != 0)
1909 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1910 abfd->flags |= HAS_RELOC;
1911 return TRUE;
1912 }
1913 break;
1914
1915 case SHT_GNU_verdef:
1916 elf_dynverdef (abfd) = shindex;
1917 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1918 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1919 break;
1920
1921 case SHT_GNU_versym:
1922 elf_dynversym (abfd) = shindex;
1923 elf_tdata (abfd)->dynversym_hdr = *hdr;
1924 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1925 break;
1926
1927 case SHT_GNU_verneed:
1928 elf_dynverref (abfd) = shindex;
1929 elf_tdata (abfd)->dynverref_hdr = *hdr;
1930 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1931 break;
1932
1933 case SHT_SHLIB:
1934 return TRUE;
1935
1936 case SHT_GROUP:
1937 /* We need a BFD section for objcopy and relocatable linking,
1938 and it's handy to have the signature available as the section
1939 name. */
1940 name = group_signature (abfd, hdr);
1941 if (name == NULL)
1942 return FALSE;
1943 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1944 return FALSE;
1945 if (hdr->contents != NULL)
1946 {
1947 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1948 unsigned int n_elt = hdr->sh_size / 4;
1949 asection *s;
1950
1951 if (idx->flags & GRP_COMDAT)
1952 hdr->bfd_section->flags
1953 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1954
1955 /* We try to keep the same section order as it comes in. */
1956 idx += n_elt;
1957 while (--n_elt != 0)
1958 if ((s = (--idx)->shdr->bfd_section) != NULL
1959 && elf_next_in_group (s) != NULL)
1960 {
1961 elf_next_in_group (hdr->bfd_section) = s;
1962 break;
1963 }
1964 }
1965 break;
1966
1967 default:
1968 /* Check for any processor-specific section types. */
1969 {
1970 if (bed->elf_backend_section_from_shdr)
1971 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1972 }
1973 break;
1974 }
1975
1976 return TRUE;
1977 }
1978
1979 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1980 Return SEC for sections that have no elf section, and NULL on error. */
1981
1982 asection *
1983 bfd_section_from_r_symndx (bfd *abfd,
1984 struct sym_sec_cache *cache,
1985 asection *sec,
1986 unsigned long r_symndx)
1987 {
1988 Elf_Internal_Shdr *symtab_hdr;
1989 unsigned char esym[sizeof (Elf64_External_Sym)];
1990 Elf_External_Sym_Shndx eshndx;
1991 Elf_Internal_Sym isym;
1992 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1993
1994 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
1995 return cache->sec[ent];
1996
1997 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1998 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1999 &isym, esym, &eshndx) == NULL)
2000 return NULL;
2001
2002 if (cache->abfd != abfd)
2003 {
2004 memset (cache->indx, -1, sizeof (cache->indx));
2005 cache->abfd = abfd;
2006 }
2007 cache->indx[ent] = r_symndx;
2008 cache->sec[ent] = sec;
2009 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
2010 || isym.st_shndx > SHN_HIRESERVE)
2011 {
2012 asection *s;
2013 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2014 if (s != NULL)
2015 cache->sec[ent] = s;
2016 }
2017 return cache->sec[ent];
2018 }
2019
2020 /* Given an ELF section number, retrieve the corresponding BFD
2021 section. */
2022
2023 asection *
2024 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2025 {
2026 if (index >= elf_numsections (abfd))
2027 return NULL;
2028 return elf_elfsections (abfd)[index]->bfd_section;
2029 }
2030
2031 static struct bfd_elf_special_section const special_sections[] =
2032 {
2033 { ".bss", 4, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2034 { ".comment", 8, 0, SHT_PROGBITS, 0 },
2035 { ".data", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2036 { ".data1", 6, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2037 { ".debug", 6, 0, SHT_PROGBITS, 0 },
2038 { ".fini", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2039 { ".init", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2040 { ".line", 5, 0, SHT_PROGBITS, 0 },
2041 { ".rodata", 7, -2, SHT_PROGBITS, SHF_ALLOC },
2042 { ".rodata1", 8, 0, SHT_PROGBITS, SHF_ALLOC },
2043 { ".tbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2044 { ".tdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2045 { ".text", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2046 { ".init_array", 11, 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2047 { ".fini_array", 11, 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2048 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2049 { ".debug_line", 11, 0, SHT_PROGBITS, 0 },
2050 { ".debug_info", 11, 0, SHT_PROGBITS, 0 },
2051 { ".debug_abbrev", 13, 0, SHT_PROGBITS, 0 },
2052 { ".debug_aranges", 14, 0, SHT_PROGBITS, 0 },
2053 { ".dynamic", 8, 0, SHT_DYNAMIC, SHF_ALLOC },
2054 { ".dynstr", 7, 0, SHT_STRTAB, SHF_ALLOC },
2055 { ".dynsym", 7, 0, SHT_DYNSYM, SHF_ALLOC },
2056 { ".got", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2057 { ".hash", 5, 0, SHT_HASH, SHF_ALLOC },
2058 { ".interp", 7, 0, SHT_PROGBITS, 0 },
2059 { ".plt", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2060 { ".shstrtab", 9, 0, SHT_STRTAB, 0 },
2061 { ".strtab", 7, 0, SHT_STRTAB, 0 },
2062 { ".symtab", 7, 0, SHT_SYMTAB, 0 },
2063 { ".gnu.version", 12, 0, SHT_GNU_versym, 0 },
2064 { ".gnu.version_d", 14, 0, SHT_GNU_verdef, 0 },
2065 { ".gnu.version_r", 14, 0, SHT_GNU_verneed, 0 },
2066 { ".note.GNU-stack",15, 0, SHT_PROGBITS, 0 },
2067 { ".note", 5, -1, SHT_NOTE, 0 },
2068 { ".rela", 5, -1, SHT_RELA, 0 },
2069 { ".rel", 4, -1, SHT_REL, 0 },
2070 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2071 { NULL, 0, 0, 0, 0 }
2072 };
2073
2074 static const struct bfd_elf_special_section *
2075 get_special_section (const char *name,
2076 const struct bfd_elf_special_section *special_sections,
2077 unsigned int rela)
2078 {
2079 int i;
2080 int len = strlen (name);
2081
2082 for (i = 0; special_sections[i].prefix != NULL; i++)
2083 {
2084 int suffix_len;
2085 int prefix_len = special_sections[i].prefix_length;
2086
2087 if (len < prefix_len)
2088 continue;
2089 if (memcmp (name, special_sections[i].prefix, prefix_len) != 0)
2090 continue;
2091
2092 suffix_len = special_sections[i].suffix_length;
2093 if (suffix_len <= 0)
2094 {
2095 if (name[prefix_len] != 0)
2096 {
2097 if (suffix_len == 0)
2098 continue;
2099 if (name[prefix_len] != '.'
2100 && (suffix_len == -2
2101 || (rela && special_sections[i].type == SHT_REL)))
2102 continue;
2103 }
2104 }
2105 else
2106 {
2107 if (len < prefix_len + suffix_len)
2108 continue;
2109 if (memcmp (name + len - suffix_len,
2110 special_sections[i].prefix + prefix_len,
2111 suffix_len) != 0)
2112 continue;
2113 }
2114 return &special_sections[i];
2115 }
2116
2117 return NULL;
2118 }
2119
2120 const struct bfd_elf_special_section *
2121 _bfd_elf_get_sec_type_attr (bfd *abfd, const char *name)
2122 {
2123 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2124 const struct bfd_elf_special_section *ssect = NULL;
2125
2126 /* See if this is one of the special sections. */
2127 if (name)
2128 {
2129 unsigned int rela = bed->default_use_rela_p;
2130
2131 if (bed->special_sections)
2132 ssect = get_special_section (name, bed->special_sections, rela);
2133
2134 if (! ssect)
2135 ssect = get_special_section (name, special_sections, rela);
2136 }
2137
2138 return ssect;
2139 }
2140
2141 bfd_boolean
2142 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2143 {
2144 struct bfd_elf_section_data *sdata;
2145 const struct bfd_elf_special_section *ssect;
2146
2147 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2148 if (sdata == NULL)
2149 {
2150 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2151 if (sdata == NULL)
2152 return FALSE;
2153 sec->used_by_bfd = sdata;
2154 }
2155
2156 elf_section_type (sec) = SHT_NULL;
2157 ssect = _bfd_elf_get_sec_type_attr (abfd, sec->name);
2158 if (ssect != NULL)
2159 {
2160 elf_section_type (sec) = ssect->type;
2161 elf_section_flags (sec) = ssect->attr;
2162 }
2163
2164 /* Indicate whether or not this section should use RELA relocations. */
2165 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2166
2167 return TRUE;
2168 }
2169
2170 /* Create a new bfd section from an ELF program header.
2171
2172 Since program segments have no names, we generate a synthetic name
2173 of the form segment<NUM>, where NUM is generally the index in the
2174 program header table. For segments that are split (see below) we
2175 generate the names segment<NUM>a and segment<NUM>b.
2176
2177 Note that some program segments may have a file size that is different than
2178 (less than) the memory size. All this means is that at execution the
2179 system must allocate the amount of memory specified by the memory size,
2180 but only initialize it with the first "file size" bytes read from the
2181 file. This would occur for example, with program segments consisting
2182 of combined data+bss.
2183
2184 To handle the above situation, this routine generates TWO bfd sections
2185 for the single program segment. The first has the length specified by
2186 the file size of the segment, and the second has the length specified
2187 by the difference between the two sizes. In effect, the segment is split
2188 into it's initialized and uninitialized parts.
2189
2190 */
2191
2192 bfd_boolean
2193 _bfd_elf_make_section_from_phdr (bfd *abfd,
2194 Elf_Internal_Phdr *hdr,
2195 int index,
2196 const char *typename)
2197 {
2198 asection *newsect;
2199 char *name;
2200 char namebuf[64];
2201 size_t len;
2202 int split;
2203
2204 split = ((hdr->p_memsz > 0)
2205 && (hdr->p_filesz > 0)
2206 && (hdr->p_memsz > hdr->p_filesz));
2207 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2208 len = strlen (namebuf) + 1;
2209 name = bfd_alloc (abfd, len);
2210 if (!name)
2211 return FALSE;
2212 memcpy (name, namebuf, len);
2213 newsect = bfd_make_section (abfd, name);
2214 if (newsect == NULL)
2215 return FALSE;
2216 newsect->vma = hdr->p_vaddr;
2217 newsect->lma = hdr->p_paddr;
2218 newsect->_raw_size = hdr->p_filesz;
2219 newsect->filepos = hdr->p_offset;
2220 newsect->flags |= SEC_HAS_CONTENTS;
2221 newsect->alignment_power = bfd_log2 (hdr->p_align);
2222 if (hdr->p_type == PT_LOAD)
2223 {
2224 newsect->flags |= SEC_ALLOC;
2225 newsect->flags |= SEC_LOAD;
2226 if (hdr->p_flags & PF_X)
2227 {
2228 /* FIXME: all we known is that it has execute PERMISSION,
2229 may be data. */
2230 newsect->flags |= SEC_CODE;
2231 }
2232 }
2233 if (!(hdr->p_flags & PF_W))
2234 {
2235 newsect->flags |= SEC_READONLY;
2236 }
2237
2238 if (split)
2239 {
2240 sprintf (namebuf, "%s%db", typename, index);
2241 len = strlen (namebuf) + 1;
2242 name = bfd_alloc (abfd, len);
2243 if (!name)
2244 return FALSE;
2245 memcpy (name, namebuf, len);
2246 newsect = bfd_make_section (abfd, name);
2247 if (newsect == NULL)
2248 return FALSE;
2249 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2250 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2251 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2252 if (hdr->p_type == PT_LOAD)
2253 {
2254 newsect->flags |= SEC_ALLOC;
2255 if (hdr->p_flags & PF_X)
2256 newsect->flags |= SEC_CODE;
2257 }
2258 if (!(hdr->p_flags & PF_W))
2259 newsect->flags |= SEC_READONLY;
2260 }
2261
2262 return TRUE;
2263 }
2264
2265 bfd_boolean
2266 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2267 {
2268 const struct elf_backend_data *bed;
2269
2270 switch (hdr->p_type)
2271 {
2272 case PT_NULL:
2273 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2274
2275 case PT_LOAD:
2276 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2277
2278 case PT_DYNAMIC:
2279 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2280
2281 case PT_INTERP:
2282 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2283
2284 case PT_NOTE:
2285 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2286 return FALSE;
2287 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2288 return FALSE;
2289 return TRUE;
2290
2291 case PT_SHLIB:
2292 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2293
2294 case PT_PHDR:
2295 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2296
2297 case PT_GNU_EH_FRAME:
2298 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2299 "eh_frame_hdr");
2300
2301 case PT_GNU_STACK:
2302 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2303
2304 case PT_GNU_RELRO:
2305 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2306
2307 default:
2308 /* Check for any processor-specific program segment types.
2309 If no handler for them, default to making "segment" sections. */
2310 bed = get_elf_backend_data (abfd);
2311 if (bed->elf_backend_section_from_phdr)
2312 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2313 else
2314 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2315 }
2316 }
2317
2318 /* Initialize REL_HDR, the section-header for new section, containing
2319 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2320 relocations; otherwise, we use REL relocations. */
2321
2322 bfd_boolean
2323 _bfd_elf_init_reloc_shdr (bfd *abfd,
2324 Elf_Internal_Shdr *rel_hdr,
2325 asection *asect,
2326 bfd_boolean use_rela_p)
2327 {
2328 char *name;
2329 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2330 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2331
2332 name = bfd_alloc (abfd, amt);
2333 if (name == NULL)
2334 return FALSE;
2335 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2336 rel_hdr->sh_name =
2337 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2338 FALSE);
2339 if (rel_hdr->sh_name == (unsigned int) -1)
2340 return FALSE;
2341 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2342 rel_hdr->sh_entsize = (use_rela_p
2343 ? bed->s->sizeof_rela
2344 : bed->s->sizeof_rel);
2345 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2346 rel_hdr->sh_flags = 0;
2347 rel_hdr->sh_addr = 0;
2348 rel_hdr->sh_size = 0;
2349 rel_hdr->sh_offset = 0;
2350
2351 return TRUE;
2352 }
2353
2354 /* Set up an ELF internal section header for a section. */
2355
2356 static void
2357 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2358 {
2359 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2360 bfd_boolean *failedptr = failedptrarg;
2361 Elf_Internal_Shdr *this_hdr;
2362
2363 if (*failedptr)
2364 {
2365 /* We already failed; just get out of the bfd_map_over_sections
2366 loop. */
2367 return;
2368 }
2369
2370 this_hdr = &elf_section_data (asect)->this_hdr;
2371
2372 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2373 asect->name, FALSE);
2374 if (this_hdr->sh_name == (unsigned int) -1)
2375 {
2376 *failedptr = TRUE;
2377 return;
2378 }
2379
2380 this_hdr->sh_flags = 0;
2381
2382 if ((asect->flags & SEC_ALLOC) != 0
2383 || asect->user_set_vma)
2384 this_hdr->sh_addr = asect->vma;
2385 else
2386 this_hdr->sh_addr = 0;
2387
2388 this_hdr->sh_offset = 0;
2389 this_hdr->sh_size = asect->_raw_size;
2390 this_hdr->sh_link = 0;
2391 this_hdr->sh_addralign = 1 << asect->alignment_power;
2392 /* The sh_entsize and sh_info fields may have been set already by
2393 copy_private_section_data. */
2394
2395 this_hdr->bfd_section = asect;
2396 this_hdr->contents = NULL;
2397
2398 /* If the section type is unspecified, we set it based on
2399 asect->flags. */
2400 if (this_hdr->sh_type == SHT_NULL)
2401 {
2402 if ((asect->flags & SEC_GROUP) != 0)
2403 {
2404 /* We also need to mark SHF_GROUP here for relocatable
2405 link. */
2406 struct bfd_link_order *l;
2407 asection *elt;
2408
2409 for (l = asect->link_order_head; l != NULL; l = l->next)
2410 if (l->type == bfd_indirect_link_order
2411 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2412 do
2413 {
2414 /* The name is not important. Anything will do. */
2415 elf_group_name (elt->output_section) = "G";
2416 elf_section_flags (elt->output_section) |= SHF_GROUP;
2417
2418 elt = elf_next_in_group (elt);
2419 /* During a relocatable link, the lists are
2420 circular. */
2421 }
2422 while (elt != elf_next_in_group (l->u.indirect.section));
2423
2424 this_hdr->sh_type = SHT_GROUP;
2425 }
2426 else if ((asect->flags & SEC_ALLOC) != 0
2427 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2428 || (asect->flags & SEC_NEVER_LOAD) != 0))
2429 this_hdr->sh_type = SHT_NOBITS;
2430 else
2431 this_hdr->sh_type = SHT_PROGBITS;
2432 }
2433
2434 switch (this_hdr->sh_type)
2435 {
2436 default:
2437 break;
2438
2439 case SHT_STRTAB:
2440 case SHT_INIT_ARRAY:
2441 case SHT_FINI_ARRAY:
2442 case SHT_PREINIT_ARRAY:
2443 case SHT_NOTE:
2444 case SHT_NOBITS:
2445 case SHT_PROGBITS:
2446 break;
2447
2448 case SHT_HASH:
2449 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2450 break;
2451
2452 case SHT_DYNSYM:
2453 this_hdr->sh_entsize = bed->s->sizeof_sym;
2454 break;
2455
2456 case SHT_DYNAMIC:
2457 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2458 break;
2459
2460 case SHT_RELA:
2461 if (get_elf_backend_data (abfd)->may_use_rela_p)
2462 this_hdr->sh_entsize = bed->s->sizeof_rela;
2463 break;
2464
2465 case SHT_REL:
2466 if (get_elf_backend_data (abfd)->may_use_rel_p)
2467 this_hdr->sh_entsize = bed->s->sizeof_rel;
2468 break;
2469
2470 case SHT_GNU_versym:
2471 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2472 break;
2473
2474 case SHT_GNU_verdef:
2475 this_hdr->sh_entsize = 0;
2476 /* objcopy or strip will copy over sh_info, but may not set
2477 cverdefs. The linker will set cverdefs, but sh_info will be
2478 zero. */
2479 if (this_hdr->sh_info == 0)
2480 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2481 else
2482 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2483 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2484 break;
2485
2486 case SHT_GNU_verneed:
2487 this_hdr->sh_entsize = 0;
2488 /* objcopy or strip will copy over sh_info, but may not set
2489 cverrefs. The linker will set cverrefs, but sh_info will be
2490 zero. */
2491 if (this_hdr->sh_info == 0)
2492 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2493 else
2494 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2495 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2496 break;
2497
2498 case SHT_GROUP:
2499 this_hdr->sh_entsize = 4;
2500 break;
2501 }
2502
2503 if ((asect->flags & SEC_ALLOC) != 0)
2504 this_hdr->sh_flags |= SHF_ALLOC;
2505 if ((asect->flags & SEC_READONLY) == 0)
2506 this_hdr->sh_flags |= SHF_WRITE;
2507 if ((asect->flags & SEC_CODE) != 0)
2508 this_hdr->sh_flags |= SHF_EXECINSTR;
2509 if ((asect->flags & SEC_MERGE) != 0)
2510 {
2511 this_hdr->sh_flags |= SHF_MERGE;
2512 this_hdr->sh_entsize = asect->entsize;
2513 if ((asect->flags & SEC_STRINGS) != 0)
2514 this_hdr->sh_flags |= SHF_STRINGS;
2515 }
2516 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2517 this_hdr->sh_flags |= SHF_GROUP;
2518 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2519 {
2520 this_hdr->sh_flags |= SHF_TLS;
2521 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2522 {
2523 struct bfd_link_order *o;
2524
2525 this_hdr->sh_size = 0;
2526 for (o = asect->link_order_head; o != NULL; o = o->next)
2527 if (this_hdr->sh_size < o->offset + o->size)
2528 this_hdr->sh_size = o->offset + o->size;
2529 if (this_hdr->sh_size)
2530 this_hdr->sh_type = SHT_NOBITS;
2531 }
2532 }
2533
2534 /* Check for processor-specific section types. */
2535 if (bed->elf_backend_fake_sections
2536 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2537 *failedptr = TRUE;
2538
2539 /* If the section has relocs, set up a section header for the
2540 SHT_REL[A] section. If two relocation sections are required for
2541 this section, it is up to the processor-specific back-end to
2542 create the other. */
2543 if ((asect->flags & SEC_RELOC) != 0
2544 && !_bfd_elf_init_reloc_shdr (abfd,
2545 &elf_section_data (asect)->rel_hdr,
2546 asect,
2547 asect->use_rela_p))
2548 *failedptr = TRUE;
2549 }
2550
2551 /* Fill in the contents of a SHT_GROUP section. */
2552
2553 void
2554 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2555 {
2556 bfd_boolean *failedptr = failedptrarg;
2557 unsigned long symindx;
2558 asection *elt, *first;
2559 unsigned char *loc;
2560 struct bfd_link_order *l;
2561 bfd_boolean gas;
2562
2563 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2564 || *failedptr)
2565 return;
2566
2567 symindx = 0;
2568 if (elf_group_id (sec) != NULL)
2569 symindx = elf_group_id (sec)->udata.i;
2570
2571 if (symindx == 0)
2572 {
2573 /* If called from the assembler, swap_out_syms will have set up
2574 elf_section_syms; If called for "ld -r", use target_index. */
2575 if (elf_section_syms (abfd) != NULL)
2576 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2577 else
2578 symindx = sec->target_index;
2579 }
2580 elf_section_data (sec)->this_hdr.sh_info = symindx;
2581
2582 /* The contents won't be allocated for "ld -r" or objcopy. */
2583 gas = TRUE;
2584 if (sec->contents == NULL)
2585 {
2586 gas = FALSE;
2587 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2588
2589 /* Arrange for the section to be written out. */
2590 elf_section_data (sec)->this_hdr.contents = sec->contents;
2591 if (sec->contents == NULL)
2592 {
2593 *failedptr = TRUE;
2594 return;
2595 }
2596 }
2597
2598 loc = sec->contents + sec->_raw_size;
2599
2600 /* Get the pointer to the first section in the group that gas
2601 squirreled away here. objcopy arranges for this to be set to the
2602 start of the input section group. */
2603 first = elt = elf_next_in_group (sec);
2604
2605 /* First element is a flag word. Rest of section is elf section
2606 indices for all the sections of the group. Write them backwards
2607 just to keep the group in the same order as given in .section
2608 directives, not that it matters. */
2609 while (elt != NULL)
2610 {
2611 asection *s;
2612 unsigned int idx;
2613
2614 loc -= 4;
2615 s = elt;
2616 if (!gas)
2617 s = s->output_section;
2618 idx = 0;
2619 if (s != NULL)
2620 idx = elf_section_data (s)->this_idx;
2621 H_PUT_32 (abfd, idx, loc);
2622 elt = elf_next_in_group (elt);
2623 if (elt == first)
2624 break;
2625 }
2626
2627 /* If this is a relocatable link, then the above did nothing because
2628 SEC is the output section. Look through the input sections
2629 instead. */
2630 for (l = sec->link_order_head; l != NULL; l = l->next)
2631 if (l->type == bfd_indirect_link_order
2632 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2633 do
2634 {
2635 loc -= 4;
2636 H_PUT_32 (abfd,
2637 elf_section_data (elt->output_section)->this_idx, loc);
2638 elt = elf_next_in_group (elt);
2639 /* During a relocatable link, the lists are circular. */
2640 }
2641 while (elt != elf_next_in_group (l->u.indirect.section));
2642
2643 /* With ld -r, merging SHT_GROUP sections results in wasted space
2644 due to allowing for the flag word on each input. We may well
2645 duplicate entries too. */
2646 while ((loc -= 4) > sec->contents)
2647 H_PUT_32 (abfd, 0, loc);
2648
2649 if (loc != sec->contents)
2650 abort ();
2651
2652 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2653 }
2654
2655 /* Assign all ELF section numbers. The dummy first section is handled here
2656 too. The link/info pointers for the standard section types are filled
2657 in here too, while we're at it. */
2658
2659 static bfd_boolean
2660 assign_section_numbers (bfd *abfd)
2661 {
2662 struct elf_obj_tdata *t = elf_tdata (abfd);
2663 asection *sec;
2664 unsigned int section_number, secn;
2665 Elf_Internal_Shdr **i_shdrp;
2666 bfd_size_type amt;
2667
2668 section_number = 1;
2669
2670 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2671
2672 for (sec = abfd->sections; sec; sec = sec->next)
2673 {
2674 struct bfd_elf_section_data *d = elf_section_data (sec);
2675
2676 if (section_number == SHN_LORESERVE)
2677 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2678 d->this_idx = section_number++;
2679 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2680 if ((sec->flags & SEC_RELOC) == 0)
2681 d->rel_idx = 0;
2682 else
2683 {
2684 if (section_number == SHN_LORESERVE)
2685 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2686 d->rel_idx = section_number++;
2687 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2688 }
2689
2690 if (d->rel_hdr2)
2691 {
2692 if (section_number == SHN_LORESERVE)
2693 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2694 d->rel_idx2 = section_number++;
2695 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2696 }
2697 else
2698 d->rel_idx2 = 0;
2699 }
2700
2701 if (section_number == SHN_LORESERVE)
2702 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2703 t->shstrtab_section = section_number++;
2704 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2705 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2706
2707 if (bfd_get_symcount (abfd) > 0)
2708 {
2709 if (section_number == SHN_LORESERVE)
2710 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2711 t->symtab_section = section_number++;
2712 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2713 if (section_number > SHN_LORESERVE - 2)
2714 {
2715 if (section_number == SHN_LORESERVE)
2716 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2717 t->symtab_shndx_section = section_number++;
2718 t->symtab_shndx_hdr.sh_name
2719 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2720 ".symtab_shndx", FALSE);
2721 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2722 return FALSE;
2723 }
2724 if (section_number == SHN_LORESERVE)
2725 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2726 t->strtab_section = section_number++;
2727 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2728 }
2729
2730 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2731 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2732
2733 elf_numsections (abfd) = section_number;
2734 elf_elfheader (abfd)->e_shnum = section_number;
2735 if (section_number > SHN_LORESERVE)
2736 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2737
2738 /* Set up the list of section header pointers, in agreement with the
2739 indices. */
2740 amt = section_number * sizeof (Elf_Internal_Shdr *);
2741 i_shdrp = bfd_zalloc (abfd, amt);
2742 if (i_shdrp == NULL)
2743 return FALSE;
2744
2745 amt = sizeof (Elf_Internal_Shdr);
2746 i_shdrp[0] = bfd_zalloc (abfd, amt);
2747 if (i_shdrp[0] == NULL)
2748 {
2749 bfd_release (abfd, i_shdrp);
2750 return FALSE;
2751 }
2752
2753 elf_elfsections (abfd) = i_shdrp;
2754
2755 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2756 if (bfd_get_symcount (abfd) > 0)
2757 {
2758 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2759 if (elf_numsections (abfd) > SHN_LORESERVE)
2760 {
2761 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2762 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2763 }
2764 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2765 t->symtab_hdr.sh_link = t->strtab_section;
2766 }
2767 for (sec = abfd->sections; sec; sec = sec->next)
2768 {
2769 struct bfd_elf_section_data *d = elf_section_data (sec);
2770 asection *s;
2771 const char *name;
2772
2773 i_shdrp[d->this_idx] = &d->this_hdr;
2774 if (d->rel_idx != 0)
2775 i_shdrp[d->rel_idx] = &d->rel_hdr;
2776 if (d->rel_idx2 != 0)
2777 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2778
2779 /* Fill in the sh_link and sh_info fields while we're at it. */
2780
2781 /* sh_link of a reloc section is the section index of the symbol
2782 table. sh_info is the section index of the section to which
2783 the relocation entries apply. */
2784 if (d->rel_idx != 0)
2785 {
2786 d->rel_hdr.sh_link = t->symtab_section;
2787 d->rel_hdr.sh_info = d->this_idx;
2788 }
2789 if (d->rel_idx2 != 0)
2790 {
2791 d->rel_hdr2->sh_link = t->symtab_section;
2792 d->rel_hdr2->sh_info = d->this_idx;
2793 }
2794
2795 switch (d->this_hdr.sh_type)
2796 {
2797 case SHT_REL:
2798 case SHT_RELA:
2799 /* A reloc section which we are treating as a normal BFD
2800 section. sh_link is the section index of the symbol
2801 table. sh_info is the section index of the section to
2802 which the relocation entries apply. We assume that an
2803 allocated reloc section uses the dynamic symbol table.
2804 FIXME: How can we be sure? */
2805 s = bfd_get_section_by_name (abfd, ".dynsym");
2806 if (s != NULL)
2807 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2808
2809 /* We look up the section the relocs apply to by name. */
2810 name = sec->name;
2811 if (d->this_hdr.sh_type == SHT_REL)
2812 name += 4;
2813 else
2814 name += 5;
2815 s = bfd_get_section_by_name (abfd, name);
2816 if (s != NULL)
2817 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2818 break;
2819
2820 case SHT_STRTAB:
2821 /* We assume that a section named .stab*str is a stabs
2822 string section. We look for a section with the same name
2823 but without the trailing ``str'', and set its sh_link
2824 field to point to this section. */
2825 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2826 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2827 {
2828 size_t len;
2829 char *alc;
2830
2831 len = strlen (sec->name);
2832 alc = bfd_malloc (len - 2);
2833 if (alc == NULL)
2834 return FALSE;
2835 memcpy (alc, sec->name, len - 3);
2836 alc[len - 3] = '\0';
2837 s = bfd_get_section_by_name (abfd, alc);
2838 free (alc);
2839 if (s != NULL)
2840 {
2841 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2842
2843 /* This is a .stab section. */
2844 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2845 elf_section_data (s)->this_hdr.sh_entsize
2846 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2847 }
2848 }
2849 break;
2850
2851 case SHT_DYNAMIC:
2852 case SHT_DYNSYM:
2853 case SHT_GNU_verneed:
2854 case SHT_GNU_verdef:
2855 /* sh_link is the section header index of the string table
2856 used for the dynamic entries, or the symbol table, or the
2857 version strings. */
2858 s = bfd_get_section_by_name (abfd, ".dynstr");
2859 if (s != NULL)
2860 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2861 break;
2862
2863 case SHT_HASH:
2864 case SHT_GNU_versym:
2865 /* sh_link is the section header index of the symbol table
2866 this hash table or version table is for. */
2867 s = bfd_get_section_by_name (abfd, ".dynsym");
2868 if (s != NULL)
2869 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2870 break;
2871
2872 case SHT_GROUP:
2873 d->this_hdr.sh_link = t->symtab_section;
2874 }
2875 }
2876
2877 for (secn = 1; secn < section_number; ++secn)
2878 if (i_shdrp[secn] == NULL)
2879 i_shdrp[secn] = i_shdrp[0];
2880 else
2881 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2882 i_shdrp[secn]->sh_name);
2883 return TRUE;
2884 }
2885
2886 /* Map symbol from it's internal number to the external number, moving
2887 all local symbols to be at the head of the list. */
2888
2889 static int
2890 sym_is_global (bfd *abfd, asymbol *sym)
2891 {
2892 /* If the backend has a special mapping, use it. */
2893 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2894 if (bed->elf_backend_sym_is_global)
2895 return (*bed->elf_backend_sym_is_global) (abfd, sym);
2896
2897 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2898 || bfd_is_und_section (bfd_get_section (sym))
2899 || bfd_is_com_section (bfd_get_section (sym)));
2900 }
2901
2902 static bfd_boolean
2903 elf_map_symbols (bfd *abfd)
2904 {
2905 unsigned int symcount = bfd_get_symcount (abfd);
2906 asymbol **syms = bfd_get_outsymbols (abfd);
2907 asymbol **sect_syms;
2908 unsigned int num_locals = 0;
2909 unsigned int num_globals = 0;
2910 unsigned int num_locals2 = 0;
2911 unsigned int num_globals2 = 0;
2912 int max_index = 0;
2913 unsigned int idx;
2914 asection *asect;
2915 asymbol **new_syms;
2916 bfd_size_type amt;
2917
2918 #ifdef DEBUG
2919 fprintf (stderr, "elf_map_symbols\n");
2920 fflush (stderr);
2921 #endif
2922
2923 for (asect = abfd->sections; asect; asect = asect->next)
2924 {
2925 if (max_index < asect->index)
2926 max_index = asect->index;
2927 }
2928
2929 max_index++;
2930 amt = max_index * sizeof (asymbol *);
2931 sect_syms = bfd_zalloc (abfd, amt);
2932 if (sect_syms == NULL)
2933 return FALSE;
2934 elf_section_syms (abfd) = sect_syms;
2935 elf_num_section_syms (abfd) = max_index;
2936
2937 /* Init sect_syms entries for any section symbols we have already
2938 decided to output. */
2939 for (idx = 0; idx < symcount; idx++)
2940 {
2941 asymbol *sym = syms[idx];
2942
2943 if ((sym->flags & BSF_SECTION_SYM) != 0
2944 && sym->value == 0)
2945 {
2946 asection *sec;
2947
2948 sec = sym->section;
2949
2950 if (sec->owner != NULL)
2951 {
2952 if (sec->owner != abfd)
2953 {
2954 if (sec->output_offset != 0)
2955 continue;
2956
2957 sec = sec->output_section;
2958
2959 /* Empty sections in the input files may have had a
2960 section symbol created for them. (See the comment
2961 near the end of _bfd_generic_link_output_symbols in
2962 linker.c). If the linker script discards such
2963 sections then we will reach this point. Since we know
2964 that we cannot avoid this case, we detect it and skip
2965 the abort and the assignment to the sect_syms array.
2966 To reproduce this particular case try running the
2967 linker testsuite test ld-scripts/weak.exp for an ELF
2968 port that uses the generic linker. */
2969 if (sec->owner == NULL)
2970 continue;
2971
2972 BFD_ASSERT (sec->owner == abfd);
2973 }
2974 sect_syms[sec->index] = syms[idx];
2975 }
2976 }
2977 }
2978
2979 /* Classify all of the symbols. */
2980 for (idx = 0; idx < symcount; idx++)
2981 {
2982 if (!sym_is_global (abfd, syms[idx]))
2983 num_locals++;
2984 else
2985 num_globals++;
2986 }
2987
2988 /* We will be adding a section symbol for each BFD section. Most normal
2989 sections will already have a section symbol in outsymbols, but
2990 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2991 at least in that case. */
2992 for (asect = abfd->sections; asect; asect = asect->next)
2993 {
2994 if (sect_syms[asect->index] == NULL)
2995 {
2996 if (!sym_is_global (abfd, asect->symbol))
2997 num_locals++;
2998 else
2999 num_globals++;
3000 }
3001 }
3002
3003 /* Now sort the symbols so the local symbols are first. */
3004 amt = (num_locals + num_globals) * sizeof (asymbol *);
3005 new_syms = bfd_alloc (abfd, amt);
3006
3007 if (new_syms == NULL)
3008 return FALSE;
3009
3010 for (idx = 0; idx < symcount; idx++)
3011 {
3012 asymbol *sym = syms[idx];
3013 unsigned int i;
3014
3015 if (!sym_is_global (abfd, sym))
3016 i = num_locals2++;
3017 else
3018 i = num_locals + num_globals2++;
3019 new_syms[i] = sym;
3020 sym->udata.i = i + 1;
3021 }
3022 for (asect = abfd->sections; asect; asect = asect->next)
3023 {
3024 if (sect_syms[asect->index] == NULL)
3025 {
3026 asymbol *sym = asect->symbol;
3027 unsigned int i;
3028
3029 sect_syms[asect->index] = sym;
3030 if (!sym_is_global (abfd, sym))
3031 i = num_locals2++;
3032 else
3033 i = num_locals + num_globals2++;
3034 new_syms[i] = sym;
3035 sym->udata.i = i + 1;
3036 }
3037 }
3038
3039 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3040
3041 elf_num_locals (abfd) = num_locals;
3042 elf_num_globals (abfd) = num_globals;
3043 return TRUE;
3044 }
3045
3046 /* Align to the maximum file alignment that could be required for any
3047 ELF data structure. */
3048
3049 static inline file_ptr
3050 align_file_position (file_ptr off, int align)
3051 {
3052 return (off + align - 1) & ~(align - 1);
3053 }
3054
3055 /* Assign a file position to a section, optionally aligning to the
3056 required section alignment. */
3057
3058 file_ptr
3059 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3060 file_ptr offset,
3061 bfd_boolean align)
3062 {
3063 if (align)
3064 {
3065 unsigned int al;
3066
3067 al = i_shdrp->sh_addralign;
3068 if (al > 1)
3069 offset = BFD_ALIGN (offset, al);
3070 }
3071 i_shdrp->sh_offset = offset;
3072 if (i_shdrp->bfd_section != NULL)
3073 i_shdrp->bfd_section->filepos = offset;
3074 if (i_shdrp->sh_type != SHT_NOBITS)
3075 offset += i_shdrp->sh_size;
3076 return offset;
3077 }
3078
3079 /* Compute the file positions we are going to put the sections at, and
3080 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3081 is not NULL, this is being called by the ELF backend linker. */
3082
3083 bfd_boolean
3084 _bfd_elf_compute_section_file_positions (bfd *abfd,
3085 struct bfd_link_info *link_info)
3086 {
3087 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3088 bfd_boolean failed;
3089 struct bfd_strtab_hash *strtab;
3090 Elf_Internal_Shdr *shstrtab_hdr;
3091
3092 if (abfd->output_has_begun)
3093 return TRUE;
3094
3095 /* Do any elf backend specific processing first. */
3096 if (bed->elf_backend_begin_write_processing)
3097 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3098
3099 if (! prep_headers (abfd))
3100 return FALSE;
3101
3102 /* Post process the headers if necessary. */
3103 if (bed->elf_backend_post_process_headers)
3104 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3105
3106 failed = FALSE;
3107 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3108 if (failed)
3109 return FALSE;
3110
3111 if (!assign_section_numbers (abfd))
3112 return FALSE;
3113
3114 /* The backend linker builds symbol table information itself. */
3115 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3116 {
3117 /* Non-zero if doing a relocatable link. */
3118 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3119
3120 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3121 return FALSE;
3122 }
3123
3124 if (link_info == NULL)
3125 {
3126 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3127 if (failed)
3128 return FALSE;
3129 }
3130
3131 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3132 /* sh_name was set in prep_headers. */
3133 shstrtab_hdr->sh_type = SHT_STRTAB;
3134 shstrtab_hdr->sh_flags = 0;
3135 shstrtab_hdr->sh_addr = 0;
3136 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3137 shstrtab_hdr->sh_entsize = 0;
3138 shstrtab_hdr->sh_link = 0;
3139 shstrtab_hdr->sh_info = 0;
3140 /* sh_offset is set in assign_file_positions_except_relocs. */
3141 shstrtab_hdr->sh_addralign = 1;
3142
3143 if (!assign_file_positions_except_relocs (abfd, link_info))
3144 return FALSE;
3145
3146 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3147 {
3148 file_ptr off;
3149 Elf_Internal_Shdr *hdr;
3150
3151 off = elf_tdata (abfd)->next_file_pos;
3152
3153 hdr = &elf_tdata (abfd)->symtab_hdr;
3154 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3155
3156 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3157 if (hdr->sh_size != 0)
3158 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3159
3160 hdr = &elf_tdata (abfd)->strtab_hdr;
3161 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3162
3163 elf_tdata (abfd)->next_file_pos = off;
3164
3165 /* Now that we know where the .strtab section goes, write it
3166 out. */
3167 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3168 || ! _bfd_stringtab_emit (abfd, strtab))
3169 return FALSE;
3170 _bfd_stringtab_free (strtab);
3171 }
3172
3173 abfd->output_has_begun = TRUE;
3174
3175 return TRUE;
3176 }
3177
3178 /* Create a mapping from a set of sections to a program segment. */
3179
3180 static struct elf_segment_map *
3181 make_mapping (bfd *abfd,
3182 asection **sections,
3183 unsigned int from,
3184 unsigned int to,
3185 bfd_boolean phdr)
3186 {
3187 struct elf_segment_map *m;
3188 unsigned int i;
3189 asection **hdrpp;
3190 bfd_size_type amt;
3191
3192 amt = sizeof (struct elf_segment_map);
3193 amt += (to - from - 1) * sizeof (asection *);
3194 m = bfd_zalloc (abfd, amt);
3195 if (m == NULL)
3196 return NULL;
3197 m->next = NULL;
3198 m->p_type = PT_LOAD;
3199 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3200 m->sections[i - from] = *hdrpp;
3201 m->count = to - from;
3202
3203 if (from == 0 && phdr)
3204 {
3205 /* Include the headers in the first PT_LOAD segment. */
3206 m->includes_filehdr = 1;
3207 m->includes_phdrs = 1;
3208 }
3209
3210 return m;
3211 }
3212
3213 /* Set up a mapping from BFD sections to program segments. */
3214
3215 static bfd_boolean
3216 map_sections_to_segments (bfd *abfd)
3217 {
3218 asection **sections = NULL;
3219 asection *s;
3220 unsigned int i;
3221 unsigned int count;
3222 struct elf_segment_map *mfirst;
3223 struct elf_segment_map **pm;
3224 struct elf_segment_map *m;
3225 asection *last_hdr;
3226 bfd_vma last_size;
3227 unsigned int phdr_index;
3228 bfd_vma maxpagesize;
3229 asection **hdrpp;
3230 bfd_boolean phdr_in_segment = TRUE;
3231 bfd_boolean writable;
3232 int tls_count = 0;
3233 asection *first_tls = NULL;
3234 asection *dynsec, *eh_frame_hdr;
3235 bfd_size_type amt;
3236
3237 if (elf_tdata (abfd)->segment_map != NULL)
3238 return TRUE;
3239
3240 if (bfd_count_sections (abfd) == 0)
3241 return TRUE;
3242
3243 /* Select the allocated sections, and sort them. */
3244
3245 amt = bfd_count_sections (abfd) * sizeof (asection *);
3246 sections = bfd_malloc (amt);
3247 if (sections == NULL)
3248 goto error_return;
3249
3250 i = 0;
3251 for (s = abfd->sections; s != NULL; s = s->next)
3252 {
3253 if ((s->flags & SEC_ALLOC) != 0)
3254 {
3255 sections[i] = s;
3256 ++i;
3257 }
3258 }
3259 BFD_ASSERT (i <= bfd_count_sections (abfd));
3260 count = i;
3261
3262 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3263
3264 /* Build the mapping. */
3265
3266 mfirst = NULL;
3267 pm = &mfirst;
3268
3269 /* If we have a .interp section, then create a PT_PHDR segment for
3270 the program headers and a PT_INTERP segment for the .interp
3271 section. */
3272 s = bfd_get_section_by_name (abfd, ".interp");
3273 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3274 {
3275 amt = sizeof (struct elf_segment_map);
3276 m = bfd_zalloc (abfd, amt);
3277 if (m == NULL)
3278 goto error_return;
3279 m->next = NULL;
3280 m->p_type = PT_PHDR;
3281 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3282 m->p_flags = PF_R | PF_X;
3283 m->p_flags_valid = 1;
3284 m->includes_phdrs = 1;
3285
3286 *pm = m;
3287 pm = &m->next;
3288
3289 amt = sizeof (struct elf_segment_map);
3290 m = bfd_zalloc (abfd, amt);
3291 if (m == NULL)
3292 goto error_return;
3293 m->next = NULL;
3294 m->p_type = PT_INTERP;
3295 m->count = 1;
3296 m->sections[0] = s;
3297
3298 *pm = m;
3299 pm = &m->next;
3300 }
3301
3302 /* Look through the sections. We put sections in the same program
3303 segment when the start of the second section can be placed within
3304 a few bytes of the end of the first section. */
3305 last_hdr = NULL;
3306 last_size = 0;
3307 phdr_index = 0;
3308 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3309 writable = FALSE;
3310 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3311 if (dynsec != NULL
3312 && (dynsec->flags & SEC_LOAD) == 0)
3313 dynsec = NULL;
3314
3315 /* Deal with -Ttext or something similar such that the first section
3316 is not adjacent to the program headers. This is an
3317 approximation, since at this point we don't know exactly how many
3318 program headers we will need. */
3319 if (count > 0)
3320 {
3321 bfd_size_type phdr_size;
3322
3323 phdr_size = elf_tdata (abfd)->program_header_size;
3324 if (phdr_size == 0)
3325 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3326 if ((abfd->flags & D_PAGED) == 0
3327 || sections[0]->lma < phdr_size
3328 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3329 phdr_in_segment = FALSE;
3330 }
3331
3332 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3333 {
3334 asection *hdr;
3335 bfd_boolean new_segment;
3336
3337 hdr = *hdrpp;
3338
3339 /* See if this section and the last one will fit in the same
3340 segment. */
3341
3342 if (last_hdr == NULL)
3343 {
3344 /* If we don't have a segment yet, then we don't need a new
3345 one (we build the last one after this loop). */
3346 new_segment = FALSE;
3347 }
3348 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3349 {
3350 /* If this section has a different relation between the
3351 virtual address and the load address, then we need a new
3352 segment. */
3353 new_segment = TRUE;
3354 }
3355 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3356 < BFD_ALIGN (hdr->lma, maxpagesize))
3357 {
3358 /* If putting this section in this segment would force us to
3359 skip a page in the segment, then we need a new segment. */
3360 new_segment = TRUE;
3361 }
3362 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3363 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3364 {
3365 /* We don't want to put a loadable section after a
3366 nonloadable section in the same segment.
3367 Consider .tbss sections as loadable for this purpose. */
3368 new_segment = TRUE;
3369 }
3370 else if ((abfd->flags & D_PAGED) == 0)
3371 {
3372 /* If the file is not demand paged, which means that we
3373 don't require the sections to be correctly aligned in the
3374 file, then there is no other reason for a new segment. */
3375 new_segment = FALSE;
3376 }
3377 else if (! writable
3378 && (hdr->flags & SEC_READONLY) == 0
3379 && (((last_hdr->lma + last_size - 1)
3380 & ~(maxpagesize - 1))
3381 != (hdr->lma & ~(maxpagesize - 1))))
3382 {
3383 /* We don't want to put a writable section in a read only
3384 segment, unless they are on the same page in memory
3385 anyhow. We already know that the last section does not
3386 bring us past the current section on the page, so the
3387 only case in which the new section is not on the same
3388 page as the previous section is when the previous section
3389 ends precisely on a page boundary. */
3390 new_segment = TRUE;
3391 }
3392 else
3393 {
3394 /* Otherwise, we can use the same segment. */
3395 new_segment = FALSE;
3396 }
3397
3398 if (! new_segment)
3399 {
3400 if ((hdr->flags & SEC_READONLY) == 0)
3401 writable = TRUE;
3402 last_hdr = hdr;
3403 /* .tbss sections effectively have zero size. */
3404 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3405 last_size = hdr->_raw_size;
3406 else
3407 last_size = 0;
3408 continue;
3409 }
3410
3411 /* We need a new program segment. We must create a new program
3412 header holding all the sections from phdr_index until hdr. */
3413
3414 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3415 if (m == NULL)
3416 goto error_return;
3417
3418 *pm = m;
3419 pm = &m->next;
3420
3421 if ((hdr->flags & SEC_READONLY) == 0)
3422 writable = TRUE;
3423 else
3424 writable = FALSE;
3425
3426 last_hdr = hdr;
3427 /* .tbss sections effectively have zero size. */
3428 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3429 last_size = hdr->_raw_size;
3430 else
3431 last_size = 0;
3432 phdr_index = i;
3433 phdr_in_segment = FALSE;
3434 }
3435
3436 /* Create a final PT_LOAD program segment. */
3437 if (last_hdr != NULL)
3438 {
3439 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3440 if (m == NULL)
3441 goto error_return;
3442
3443 *pm = m;
3444 pm = &m->next;
3445 }
3446
3447 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3448 if (dynsec != NULL)
3449 {
3450 amt = sizeof (struct elf_segment_map);
3451 m = bfd_zalloc (abfd, amt);
3452 if (m == NULL)
3453 goto error_return;
3454 m->next = NULL;
3455 m->p_type = PT_DYNAMIC;
3456 m->count = 1;
3457 m->sections[0] = dynsec;
3458
3459 *pm = m;
3460 pm = &m->next;
3461 }
3462
3463 /* For each loadable .note section, add a PT_NOTE segment. We don't
3464 use bfd_get_section_by_name, because if we link together
3465 nonloadable .note sections and loadable .note sections, we will
3466 generate two .note sections in the output file. FIXME: Using
3467 names for section types is bogus anyhow. */
3468 for (s = abfd->sections; s != NULL; s = s->next)
3469 {
3470 if ((s->flags & SEC_LOAD) != 0
3471 && strncmp (s->name, ".note", 5) == 0)
3472 {
3473 amt = sizeof (struct elf_segment_map);
3474 m = bfd_zalloc (abfd, amt);
3475 if (m == NULL)
3476 goto error_return;
3477 m->next = NULL;
3478 m->p_type = PT_NOTE;
3479 m->count = 1;
3480 m->sections[0] = s;
3481
3482 *pm = m;
3483 pm = &m->next;
3484 }
3485 if (s->flags & SEC_THREAD_LOCAL)
3486 {
3487 if (! tls_count)
3488 first_tls = s;
3489 tls_count++;
3490 }
3491 }
3492
3493 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3494 if (tls_count > 0)
3495 {
3496 int i;
3497
3498 amt = sizeof (struct elf_segment_map);
3499 amt += (tls_count - 1) * sizeof (asection *);
3500 m = bfd_zalloc (abfd, amt);
3501 if (m == NULL)
3502 goto error_return;
3503 m->next = NULL;
3504 m->p_type = PT_TLS;
3505 m->count = tls_count;
3506 /* Mandated PF_R. */
3507 m->p_flags = PF_R;
3508 m->p_flags_valid = 1;
3509 for (i = 0; i < tls_count; ++i)
3510 {
3511 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3512 m->sections[i] = first_tls;
3513 first_tls = first_tls->next;
3514 }
3515
3516 *pm = m;
3517 pm = &m->next;
3518 }
3519
3520 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3521 segment. */
3522 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3523 if (eh_frame_hdr != NULL
3524 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3525 {
3526 amt = sizeof (struct elf_segment_map);
3527 m = bfd_zalloc (abfd, amt);
3528 if (m == NULL)
3529 goto error_return;
3530 m->next = NULL;
3531 m->p_type = PT_GNU_EH_FRAME;
3532 m->count = 1;
3533 m->sections[0] = eh_frame_hdr->output_section;
3534
3535 *pm = m;
3536 pm = &m->next;
3537 }
3538
3539 if (elf_tdata (abfd)->stack_flags)
3540 {
3541 amt = sizeof (struct elf_segment_map);
3542 m = bfd_zalloc (abfd, amt);
3543 if (m == NULL)
3544 goto error_return;
3545 m->next = NULL;
3546 m->p_type = PT_GNU_STACK;
3547 m->p_flags = elf_tdata (abfd)->stack_flags;
3548 m->p_flags_valid = 1;
3549
3550 *pm = m;
3551 pm = &m->next;
3552 }
3553
3554 if (elf_tdata (abfd)->relro)
3555 {
3556 amt = sizeof (struct elf_segment_map);
3557 m = bfd_zalloc (abfd, amt);
3558 if (m == NULL)
3559 goto error_return;
3560 m->next = NULL;
3561 m->p_type = PT_GNU_RELRO;
3562 m->p_flags = PF_R;
3563 m->p_flags_valid = 1;
3564
3565 *pm = m;
3566 pm = &m->next;
3567 }
3568
3569 free (sections);
3570 sections = NULL;
3571
3572 elf_tdata (abfd)->segment_map = mfirst;
3573 return TRUE;
3574
3575 error_return:
3576 if (sections != NULL)
3577 free (sections);
3578 return FALSE;
3579 }
3580
3581 /* Sort sections by address. */
3582
3583 static int
3584 elf_sort_sections (const void *arg1, const void *arg2)
3585 {
3586 const asection *sec1 = *(const asection **) arg1;
3587 const asection *sec2 = *(const asection **) arg2;
3588 bfd_size_type size1, size2;
3589
3590 /* Sort by LMA first, since this is the address used to
3591 place the section into a segment. */
3592 if (sec1->lma < sec2->lma)
3593 return -1;
3594 else if (sec1->lma > sec2->lma)
3595 return 1;
3596
3597 /* Then sort by VMA. Normally the LMA and the VMA will be
3598 the same, and this will do nothing. */
3599 if (sec1->vma < sec2->vma)
3600 return -1;
3601 else if (sec1->vma > sec2->vma)
3602 return 1;
3603
3604 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3605
3606 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3607
3608 if (TOEND (sec1))
3609 {
3610 if (TOEND (sec2))
3611 {
3612 /* If the indicies are the same, do not return 0
3613 here, but continue to try the next comparison. */
3614 if (sec1->target_index - sec2->target_index != 0)
3615 return sec1->target_index - sec2->target_index;
3616 }
3617 else
3618 return 1;
3619 }
3620 else if (TOEND (sec2))
3621 return -1;
3622
3623 #undef TOEND
3624
3625 /* Sort by size, to put zero sized sections
3626 before others at the same address. */
3627
3628 size1 = (sec1->flags & SEC_LOAD) ? sec1->_raw_size : 0;
3629 size2 = (sec2->flags & SEC_LOAD) ? sec2->_raw_size : 0;
3630
3631 if (size1 < size2)
3632 return -1;
3633 if (size1 > size2)
3634 return 1;
3635
3636 return sec1->target_index - sec2->target_index;
3637 }
3638
3639 /* Ian Lance Taylor writes:
3640
3641 We shouldn't be using % with a negative signed number. That's just
3642 not good. We have to make sure either that the number is not
3643 negative, or that the number has an unsigned type. When the types
3644 are all the same size they wind up as unsigned. When file_ptr is a
3645 larger signed type, the arithmetic winds up as signed long long,
3646 which is wrong.
3647
3648 What we're trying to say here is something like ``increase OFF by
3649 the least amount that will cause it to be equal to the VMA modulo
3650 the page size.'' */
3651 /* In other words, something like:
3652
3653 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3654 off_offset = off % bed->maxpagesize;
3655 if (vma_offset < off_offset)
3656 adjustment = vma_offset + bed->maxpagesize - off_offset;
3657 else
3658 adjustment = vma_offset - off_offset;
3659
3660 which can can be collapsed into the expression below. */
3661
3662 static file_ptr
3663 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
3664 {
3665 return ((vma - off) % maxpagesize);
3666 }
3667
3668 /* Assign file positions to the sections based on the mapping from
3669 sections to segments. This function also sets up some fields in
3670 the file header, and writes out the program headers. */
3671
3672 static bfd_boolean
3673 assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
3674 {
3675 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3676 unsigned int count;
3677 struct elf_segment_map *m;
3678 unsigned int alloc;
3679 Elf_Internal_Phdr *phdrs;
3680 file_ptr off, voff;
3681 bfd_vma filehdr_vaddr, filehdr_paddr;
3682 bfd_vma phdrs_vaddr, phdrs_paddr;
3683 Elf_Internal_Phdr *p;
3684 bfd_size_type amt;
3685
3686 if (elf_tdata (abfd)->segment_map == NULL)
3687 {
3688 if (! map_sections_to_segments (abfd))
3689 return FALSE;
3690 }
3691 else
3692 {
3693 /* The placement algorithm assumes that non allocated sections are
3694 not in PT_LOAD segments. We ensure this here by removing such
3695 sections from the segment map. */
3696 for (m = elf_tdata (abfd)->segment_map;
3697 m != NULL;
3698 m = m->next)
3699 {
3700 unsigned int new_count;
3701 unsigned int i;
3702
3703 if (m->p_type != PT_LOAD)
3704 continue;
3705
3706 new_count = 0;
3707 for (i = 0; i < m->count; i ++)
3708 {
3709 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3710 {
3711 if (i != new_count)
3712 m->sections[new_count] = m->sections[i];
3713
3714 new_count ++;
3715 }
3716 }
3717
3718 if (new_count != m->count)
3719 m->count = new_count;
3720 }
3721 }
3722
3723 if (bed->elf_backend_modify_segment_map)
3724 {
3725 if (! (*bed->elf_backend_modify_segment_map) (abfd, link_info))
3726 return FALSE;
3727 }
3728
3729 count = 0;
3730 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3731 ++count;
3732
3733 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3734 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3735 elf_elfheader (abfd)->e_phnum = count;
3736
3737 if (count == 0)
3738 return TRUE;
3739
3740 /* If we already counted the number of program segments, make sure
3741 that we allocated enough space. This happens when SIZEOF_HEADERS
3742 is used in a linker script. */
3743 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3744 if (alloc != 0 && count > alloc)
3745 {
3746 ((*_bfd_error_handler)
3747 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3748 bfd_get_filename (abfd), alloc, count));
3749 bfd_set_error (bfd_error_bad_value);
3750 return FALSE;
3751 }
3752
3753 if (alloc == 0)
3754 alloc = count;
3755
3756 amt = alloc * sizeof (Elf_Internal_Phdr);
3757 phdrs = bfd_alloc (abfd, amt);
3758 if (phdrs == NULL)
3759 return FALSE;
3760
3761 off = bed->s->sizeof_ehdr;
3762 off += alloc * bed->s->sizeof_phdr;
3763
3764 filehdr_vaddr = 0;
3765 filehdr_paddr = 0;
3766 phdrs_vaddr = 0;
3767 phdrs_paddr = 0;
3768
3769 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3770 m != NULL;
3771 m = m->next, p++)
3772 {
3773 unsigned int i;
3774 asection **secpp;
3775
3776 /* If elf_segment_map is not from map_sections_to_segments, the
3777 sections may not be correctly ordered. NOTE: sorting should
3778 not be done to the PT_NOTE section of a corefile, which may
3779 contain several pseudo-sections artificially created by bfd.
3780 Sorting these pseudo-sections breaks things badly. */
3781 if (m->count > 1
3782 && !(elf_elfheader (abfd)->e_type == ET_CORE
3783 && m->p_type == PT_NOTE))
3784 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3785 elf_sort_sections);
3786
3787 p->p_type = m->p_type;
3788 p->p_flags = m->p_flags;
3789
3790 if (p->p_type == PT_LOAD
3791 && m->count > 0
3792 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3793 {
3794 if ((abfd->flags & D_PAGED) != 0)
3795 off += vma_page_aligned_bias (m->sections[0]->vma, off,
3796 bed->maxpagesize);
3797 else
3798 {
3799 bfd_size_type align;
3800
3801 align = 0;
3802 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3803 {
3804 bfd_size_type secalign;
3805
3806 secalign = bfd_get_section_alignment (abfd, *secpp);
3807 if (secalign > align)
3808 align = secalign;
3809 }
3810
3811 off += vma_page_aligned_bias (m->sections[0]->vma, off,
3812 1 << align);
3813 }
3814 }
3815
3816 if (m->count == 0)
3817 p->p_vaddr = 0;
3818 else
3819 p->p_vaddr = m->sections[0]->vma;
3820
3821 if (m->p_paddr_valid)
3822 p->p_paddr = m->p_paddr;
3823 else if (m->count == 0)
3824 p->p_paddr = 0;
3825 else
3826 p->p_paddr = m->sections[0]->lma;
3827
3828 if (p->p_type == PT_LOAD
3829 && (abfd->flags & D_PAGED) != 0)
3830 p->p_align = bed->maxpagesize;
3831 else if (m->count == 0)
3832 p->p_align = 1 << bed->s->log_file_align;
3833 else
3834 p->p_align = 0;
3835
3836 p->p_offset = 0;
3837 p->p_filesz = 0;
3838 p->p_memsz = 0;
3839
3840 if (m->includes_filehdr)
3841 {
3842 if (! m->p_flags_valid)
3843 p->p_flags |= PF_R;
3844 p->p_offset = 0;
3845 p->p_filesz = bed->s->sizeof_ehdr;
3846 p->p_memsz = bed->s->sizeof_ehdr;
3847 if (m->count > 0)
3848 {
3849 BFD_ASSERT (p->p_type == PT_LOAD);
3850
3851 if (p->p_vaddr < (bfd_vma) off)
3852 {
3853 (*_bfd_error_handler)
3854 (_("%s: Not enough room for program headers, try linking with -N"),
3855 bfd_get_filename (abfd));
3856 bfd_set_error (bfd_error_bad_value);
3857 return FALSE;
3858 }
3859
3860 p->p_vaddr -= off;
3861 if (! m->p_paddr_valid)
3862 p->p_paddr -= off;
3863 }
3864 if (p->p_type == PT_LOAD)
3865 {
3866 filehdr_vaddr = p->p_vaddr;
3867 filehdr_paddr = p->p_paddr;
3868 }
3869 }
3870
3871 if (m->includes_phdrs)
3872 {
3873 if (! m->p_flags_valid)
3874 p->p_flags |= PF_R;
3875
3876 if (m->includes_filehdr)
3877 {
3878 if (p->p_type == PT_LOAD)
3879 {
3880 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3881 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3882 }
3883 }
3884 else
3885 {
3886 p->p_offset = bed->s->sizeof_ehdr;
3887
3888 if (m->count > 0)
3889 {
3890 BFD_ASSERT (p->p_type == PT_LOAD);
3891 p->p_vaddr -= off - p->p_offset;
3892 if (! m->p_paddr_valid)
3893 p->p_paddr -= off - p->p_offset;
3894 }
3895
3896 if (p->p_type == PT_LOAD)
3897 {
3898 phdrs_vaddr = p->p_vaddr;
3899 phdrs_paddr = p->p_paddr;
3900 }
3901 else
3902 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3903 }
3904
3905 p->p_filesz += alloc * bed->s->sizeof_phdr;
3906 p->p_memsz += alloc * bed->s->sizeof_phdr;
3907 }
3908
3909 if (p->p_type == PT_LOAD
3910 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3911 {
3912 if (! m->includes_filehdr && ! m->includes_phdrs)
3913 p->p_offset = off;
3914 else
3915 {
3916 file_ptr adjust;
3917
3918 adjust = off - (p->p_offset + p->p_filesz);
3919 p->p_filesz += adjust;
3920 p->p_memsz += adjust;
3921 }
3922 }
3923
3924 voff = off;
3925
3926 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3927 {
3928 asection *sec;
3929 flagword flags;
3930 bfd_size_type align;
3931
3932 sec = *secpp;
3933 flags = sec->flags;
3934 align = 1 << bfd_get_section_alignment (abfd, sec);
3935
3936 /* The section may have artificial alignment forced by a
3937 link script. Notice this case by the gap between the
3938 cumulative phdr lma and the section's lma. */
3939 if (p->p_paddr + p->p_memsz < sec->lma)
3940 {
3941 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3942
3943 p->p_memsz += adjust;
3944 if (p->p_type == PT_LOAD
3945 || (p->p_type == PT_NOTE
3946 && bfd_get_format (abfd) == bfd_core))
3947 {
3948 off += adjust;
3949 voff += adjust;
3950 }
3951 if ((flags & SEC_LOAD) != 0
3952 || (flags & SEC_THREAD_LOCAL) != 0)
3953 p->p_filesz += adjust;
3954 }
3955
3956 if (p->p_type == PT_LOAD)
3957 {
3958 bfd_signed_vma adjust;
3959
3960 if ((flags & SEC_LOAD) != 0)
3961 {
3962 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3963 if (adjust < 0)
3964 adjust = 0;
3965 }
3966 else if ((flags & SEC_ALLOC) != 0)
3967 {
3968 /* The section VMA must equal the file position
3969 modulo the page size. FIXME: I'm not sure if
3970 this adjustment is really necessary. We used to
3971 not have the SEC_LOAD case just above, and then
3972 this was necessary, but now I'm not sure. */
3973 if ((abfd->flags & D_PAGED) != 0)
3974 adjust = vma_page_aligned_bias (sec->vma, voff,
3975 bed->maxpagesize);
3976 else
3977 adjust = vma_page_aligned_bias (sec->vma, voff,
3978 align);
3979 }
3980 else
3981 adjust = 0;
3982
3983 if (adjust != 0)
3984 {
3985 if (i == 0)
3986 {
3987 (* _bfd_error_handler) (_("\
3988 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3989 bfd_section_name (abfd, sec),
3990 sec->lma,
3991 p->p_paddr);
3992 return FALSE;
3993 }
3994 p->p_memsz += adjust;
3995 off += adjust;
3996 voff += adjust;
3997 if ((flags & SEC_LOAD) != 0)
3998 p->p_filesz += adjust;
3999 }
4000
4001 sec->filepos = off;
4002
4003 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
4004 used in a linker script we may have a section with
4005 SEC_LOAD clear but which is supposed to have
4006 contents. */
4007 if ((flags & SEC_LOAD) != 0
4008 || (flags & SEC_HAS_CONTENTS) != 0)
4009 off += sec->_raw_size;
4010
4011 if ((flags & SEC_ALLOC) != 0
4012 && ((flags & SEC_LOAD) != 0
4013 || (flags & SEC_THREAD_LOCAL) == 0))
4014 voff += sec->_raw_size;
4015 }
4016
4017 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4018 {
4019 /* The actual "note" segment has i == 0.
4020 This is the one that actually contains everything. */
4021 if (i == 0)
4022 {
4023 sec->filepos = off;
4024 p->p_filesz = sec->_raw_size;
4025 off += sec->_raw_size;
4026 voff = off;
4027 }
4028 else
4029 {
4030 /* Fake sections -- don't need to be written. */
4031 sec->filepos = 0;
4032 sec->_raw_size = 0;
4033 flags = sec->flags = 0;
4034 }
4035 p->p_memsz = 0;
4036 p->p_align = 1;
4037 }
4038 else
4039 {
4040 if ((sec->flags & SEC_LOAD) != 0
4041 || (sec->flags & SEC_THREAD_LOCAL) == 0
4042 || p->p_type == PT_TLS)
4043 p->p_memsz += sec->_raw_size;
4044
4045 if ((flags & SEC_LOAD) != 0)
4046 p->p_filesz += sec->_raw_size;
4047
4048 if (p->p_type == PT_TLS
4049 && sec->_raw_size == 0
4050 && (sec->flags & SEC_HAS_CONTENTS) == 0)
4051 {
4052 struct bfd_link_order *o;
4053 bfd_vma tbss_size = 0;
4054
4055 for (o = sec->link_order_head; o != NULL; o = o->next)
4056 if (tbss_size < o->offset + o->size)
4057 tbss_size = o->offset + o->size;
4058
4059 p->p_memsz += tbss_size;
4060 }
4061
4062 if (align > p->p_align
4063 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
4064 p->p_align = align;
4065 }
4066
4067 if (! m->p_flags_valid)
4068 {
4069 p->p_flags |= PF_R;
4070 if ((flags & SEC_CODE) != 0)
4071 p->p_flags |= PF_X;
4072 if ((flags & SEC_READONLY) == 0)
4073 p->p_flags |= PF_W;
4074 }
4075 }
4076 }
4077
4078 /* Now that we have set the section file positions, we can set up
4079 the file positions for the non PT_LOAD segments. */
4080 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4081 m != NULL;
4082 m = m->next, p++)
4083 {
4084 if (p->p_type != PT_LOAD && m->count > 0)
4085 {
4086 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
4087 p->p_offset = m->sections[0]->filepos;
4088 }
4089 if (m->count == 0)
4090 {
4091 if (m->includes_filehdr)
4092 {
4093 p->p_vaddr = filehdr_vaddr;
4094 if (! m->p_paddr_valid)
4095 p->p_paddr = filehdr_paddr;
4096 }
4097 else if (m->includes_phdrs)
4098 {
4099 p->p_vaddr = phdrs_vaddr;
4100 if (! m->p_paddr_valid)
4101 p->p_paddr = phdrs_paddr;
4102 }
4103 else if (p->p_type == PT_GNU_RELRO)
4104 {
4105 Elf_Internal_Phdr *lp;
4106
4107 for (lp = phdrs; lp < phdrs + count; ++lp)
4108 {
4109 if (lp->p_type == PT_LOAD
4110 && lp->p_vaddr <= link_info->relro_end
4111 && lp->p_vaddr >= link_info->relro_start
4112 && lp->p_vaddr + lp->p_filesz
4113 >= link_info->relro_end)
4114 break;
4115 }
4116
4117 if (lp < phdrs + count
4118 && link_info->relro_end > lp->p_vaddr)
4119 {
4120 p->p_vaddr = lp->p_vaddr;
4121 p->p_paddr = lp->p_paddr;
4122 p->p_offset = lp->p_offset;
4123 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4124 p->p_memsz = p->p_filesz;
4125 p->p_align = 1;
4126 p->p_flags = (lp->p_flags & ~PF_W);
4127 }
4128 else
4129 {
4130 memset (p, 0, sizeof *p);
4131 p->p_type = PT_NULL;
4132 }
4133 }
4134 }
4135 }
4136
4137 /* Clear out any program headers we allocated but did not use. */
4138 for (; count < alloc; count++, p++)
4139 {
4140 memset (p, 0, sizeof *p);
4141 p->p_type = PT_NULL;
4142 }
4143
4144 elf_tdata (abfd)->phdr = phdrs;
4145
4146 elf_tdata (abfd)->next_file_pos = off;
4147
4148 /* Write out the program headers. */
4149 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4150 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4151 return FALSE;
4152
4153 return TRUE;
4154 }
4155
4156 /* Get the size of the program header.
4157
4158 If this is called by the linker before any of the section VMA's are set, it
4159 can't calculate the correct value for a strange memory layout. This only
4160 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4161 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4162 data segment (exclusive of .interp and .dynamic).
4163
4164 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4165 will be two segments. */
4166
4167 static bfd_size_type
4168 get_program_header_size (bfd *abfd)
4169 {
4170 size_t segs;
4171 asection *s;
4172 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4173
4174 /* We can't return a different result each time we're called. */
4175 if (elf_tdata (abfd)->program_header_size != 0)
4176 return elf_tdata (abfd)->program_header_size;
4177
4178 if (elf_tdata (abfd)->segment_map != NULL)
4179 {
4180 struct elf_segment_map *m;
4181
4182 segs = 0;
4183 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4184 ++segs;
4185 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4186 return elf_tdata (abfd)->program_header_size;
4187 }
4188
4189 /* Assume we will need exactly two PT_LOAD segments: one for text
4190 and one for data. */
4191 segs = 2;
4192
4193 s = bfd_get_section_by_name (abfd, ".interp");
4194 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4195 {
4196 /* If we have a loadable interpreter section, we need a
4197 PT_INTERP segment. In this case, assume we also need a
4198 PT_PHDR segment, although that may not be true for all
4199 targets. */
4200 segs += 2;
4201 }
4202
4203 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4204 {
4205 /* We need a PT_DYNAMIC segment. */
4206 ++segs;
4207 }
4208
4209 if (elf_tdata (abfd)->eh_frame_hdr)
4210 {
4211 /* We need a PT_GNU_EH_FRAME segment. */
4212 ++segs;
4213 }
4214
4215 if (elf_tdata (abfd)->stack_flags)
4216 {
4217 /* We need a PT_GNU_STACK segment. */
4218 ++segs;
4219 }
4220
4221 if (elf_tdata (abfd)->relro)
4222 {
4223 /* We need a PT_GNU_RELRO segment. */
4224 ++segs;
4225 }
4226
4227 for (s = abfd->sections; s != NULL; s = s->next)
4228 {
4229 if ((s->flags & SEC_LOAD) != 0
4230 && strncmp (s->name, ".note", 5) == 0)
4231 {
4232 /* We need a PT_NOTE segment. */
4233 ++segs;
4234 }
4235 }
4236
4237 for (s = abfd->sections; s != NULL; s = s->next)
4238 {
4239 if (s->flags & SEC_THREAD_LOCAL)
4240 {
4241 /* We need a PT_TLS segment. */
4242 ++segs;
4243 break;
4244 }
4245 }
4246
4247 /* Let the backend count up any program headers it might need. */
4248 if (bed->elf_backend_additional_program_headers)
4249 {
4250 int a;
4251
4252 a = (*bed->elf_backend_additional_program_headers) (abfd);
4253 if (a == -1)
4254 abort ();
4255 segs += a;
4256 }
4257
4258 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4259 return elf_tdata (abfd)->program_header_size;
4260 }
4261
4262 /* Work out the file positions of all the sections. This is called by
4263 _bfd_elf_compute_section_file_positions. All the section sizes and
4264 VMAs must be known before this is called.
4265
4266 We do not consider reloc sections at this point, unless they form
4267 part of the loadable image. Reloc sections are assigned file
4268 positions in assign_file_positions_for_relocs, which is called by
4269 write_object_contents and final_link.
4270
4271 We also don't set the positions of the .symtab and .strtab here. */
4272
4273 static bfd_boolean
4274 assign_file_positions_except_relocs (bfd *abfd,
4275 struct bfd_link_info *link_info)
4276 {
4277 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4278 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4279 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4280 unsigned int num_sec = elf_numsections (abfd);
4281 file_ptr off;
4282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4283
4284 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4285 && bfd_get_format (abfd) != bfd_core)
4286 {
4287 Elf_Internal_Shdr **hdrpp;
4288 unsigned int i;
4289
4290 /* Start after the ELF header. */
4291 off = i_ehdrp->e_ehsize;
4292
4293 /* We are not creating an executable, which means that we are
4294 not creating a program header, and that the actual order of
4295 the sections in the file is unimportant. */
4296 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4297 {
4298 Elf_Internal_Shdr *hdr;
4299
4300 hdr = *hdrpp;
4301 if (hdr->sh_type == SHT_REL
4302 || hdr->sh_type == SHT_RELA
4303 || i == tdata->symtab_section
4304 || i == tdata->symtab_shndx_section
4305 || i == tdata->strtab_section)
4306 {
4307 hdr->sh_offset = -1;
4308 }
4309 else
4310 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4311
4312 if (i == SHN_LORESERVE - 1)
4313 {
4314 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4315 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4316 }
4317 }
4318 }
4319 else
4320 {
4321 unsigned int i;
4322 Elf_Internal_Shdr **hdrpp;
4323
4324 /* Assign file positions for the loaded sections based on the
4325 assignment of sections to segments. */
4326 if (! assign_file_positions_for_segments (abfd, link_info))
4327 return FALSE;
4328
4329 /* Assign file positions for the other sections. */
4330
4331 off = elf_tdata (abfd)->next_file_pos;
4332 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4333 {
4334 Elf_Internal_Shdr *hdr;
4335
4336 hdr = *hdrpp;
4337 if (hdr->bfd_section != NULL
4338 && hdr->bfd_section->filepos != 0)
4339 hdr->sh_offset = hdr->bfd_section->filepos;
4340 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4341 {
4342 ((*_bfd_error_handler)
4343 (_("%s: warning: allocated section `%s' not in segment"),
4344 bfd_get_filename (abfd),
4345 (hdr->bfd_section == NULL
4346 ? "*unknown*"
4347 : hdr->bfd_section->name)));
4348 if ((abfd->flags & D_PAGED) != 0)
4349 off += vma_page_aligned_bias (hdr->sh_addr, off,
4350 bed->maxpagesize);
4351 else
4352 off += vma_page_aligned_bias (hdr->sh_addr, off,
4353 hdr->sh_addralign);
4354 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4355 FALSE);
4356 }
4357 else if (hdr->sh_type == SHT_REL
4358 || hdr->sh_type == SHT_RELA
4359 || hdr == i_shdrpp[tdata->symtab_section]
4360 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4361 || hdr == i_shdrpp[tdata->strtab_section])
4362 hdr->sh_offset = -1;
4363 else
4364 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4365
4366 if (i == SHN_LORESERVE - 1)
4367 {
4368 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4369 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4370 }
4371 }
4372 }
4373
4374 /* Place the section headers. */
4375 off = align_file_position (off, 1 << bed->s->log_file_align);
4376 i_ehdrp->e_shoff = off;
4377 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4378
4379 elf_tdata (abfd)->next_file_pos = off;
4380
4381 return TRUE;
4382 }
4383
4384 static bfd_boolean
4385 prep_headers (bfd *abfd)
4386 {
4387 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4388 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4389 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4390 struct elf_strtab_hash *shstrtab;
4391 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4392
4393 i_ehdrp = elf_elfheader (abfd);
4394 i_shdrp = elf_elfsections (abfd);
4395
4396 shstrtab = _bfd_elf_strtab_init ();
4397 if (shstrtab == NULL)
4398 return FALSE;
4399
4400 elf_shstrtab (abfd) = shstrtab;
4401
4402 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4403 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4404 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4405 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4406
4407 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4408 i_ehdrp->e_ident[EI_DATA] =
4409 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4410 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4411
4412 if ((abfd->flags & DYNAMIC) != 0)
4413 i_ehdrp->e_type = ET_DYN;
4414 else if ((abfd->flags & EXEC_P) != 0)
4415 i_ehdrp->e_type = ET_EXEC;
4416 else if (bfd_get_format (abfd) == bfd_core)
4417 i_ehdrp->e_type = ET_CORE;
4418 else
4419 i_ehdrp->e_type = ET_REL;
4420
4421 switch (bfd_get_arch (abfd))
4422 {
4423 case bfd_arch_unknown:
4424 i_ehdrp->e_machine = EM_NONE;
4425 break;
4426
4427 /* There used to be a long list of cases here, each one setting
4428 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4429 in the corresponding bfd definition. To avoid duplication,
4430 the switch was removed. Machines that need special handling
4431 can generally do it in elf_backend_final_write_processing(),
4432 unless they need the information earlier than the final write.
4433 Such need can generally be supplied by replacing the tests for
4434 e_machine with the conditions used to determine it. */
4435 default:
4436 i_ehdrp->e_machine = bed->elf_machine_code;
4437 }
4438
4439 i_ehdrp->e_version = bed->s->ev_current;
4440 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4441
4442 /* No program header, for now. */
4443 i_ehdrp->e_phoff = 0;
4444 i_ehdrp->e_phentsize = 0;
4445 i_ehdrp->e_phnum = 0;
4446
4447 /* Each bfd section is section header entry. */
4448 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4449 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4450
4451 /* If we're building an executable, we'll need a program header table. */
4452 if (abfd->flags & EXEC_P)
4453 {
4454 /* It all happens later. */
4455 #if 0
4456 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4457
4458 /* elf_build_phdrs() returns a (NULL-terminated) array of
4459 Elf_Internal_Phdrs. */
4460 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4461 i_ehdrp->e_phoff = outbase;
4462 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4463 #endif
4464 }
4465 else
4466 {
4467 i_ehdrp->e_phentsize = 0;
4468 i_phdrp = 0;
4469 i_ehdrp->e_phoff = 0;
4470 }
4471
4472 elf_tdata (abfd)->symtab_hdr.sh_name =
4473 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4474 elf_tdata (abfd)->strtab_hdr.sh_name =
4475 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4476 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4477 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4478 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4479 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4480 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4481 return FALSE;
4482
4483 return TRUE;
4484 }
4485
4486 /* Assign file positions for all the reloc sections which are not part
4487 of the loadable file image. */
4488
4489 void
4490 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4491 {
4492 file_ptr off;
4493 unsigned int i, num_sec;
4494 Elf_Internal_Shdr **shdrpp;
4495
4496 off = elf_tdata (abfd)->next_file_pos;
4497
4498 num_sec = elf_numsections (abfd);
4499 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4500 {
4501 Elf_Internal_Shdr *shdrp;
4502
4503 shdrp = *shdrpp;
4504 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4505 && shdrp->sh_offset == -1)
4506 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4507 }
4508
4509 elf_tdata (abfd)->next_file_pos = off;
4510 }
4511
4512 bfd_boolean
4513 _bfd_elf_write_object_contents (bfd *abfd)
4514 {
4515 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4516 Elf_Internal_Ehdr *i_ehdrp;
4517 Elf_Internal_Shdr **i_shdrp;
4518 bfd_boolean failed;
4519 unsigned int count, num_sec;
4520
4521 if (! abfd->output_has_begun
4522 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4523 return FALSE;
4524
4525 i_shdrp = elf_elfsections (abfd);
4526 i_ehdrp = elf_elfheader (abfd);
4527
4528 failed = FALSE;
4529 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4530 if (failed)
4531 return FALSE;
4532
4533 _bfd_elf_assign_file_positions_for_relocs (abfd);
4534
4535 /* After writing the headers, we need to write the sections too... */
4536 num_sec = elf_numsections (abfd);
4537 for (count = 1; count < num_sec; count++)
4538 {
4539 if (bed->elf_backend_section_processing)
4540 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4541 if (i_shdrp[count]->contents)
4542 {
4543 bfd_size_type amt = i_shdrp[count]->sh_size;
4544
4545 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4546 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4547 return FALSE;
4548 }
4549 if (count == SHN_LORESERVE - 1)
4550 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4551 }
4552
4553 /* Write out the section header names. */
4554 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4555 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4556 return FALSE;
4557
4558 if (bed->elf_backend_final_write_processing)
4559 (*bed->elf_backend_final_write_processing) (abfd,
4560 elf_tdata (abfd)->linker);
4561
4562 return bed->s->write_shdrs_and_ehdr (abfd);
4563 }
4564
4565 bfd_boolean
4566 _bfd_elf_write_corefile_contents (bfd *abfd)
4567 {
4568 /* Hopefully this can be done just like an object file. */
4569 return _bfd_elf_write_object_contents (abfd);
4570 }
4571
4572 /* Given a section, search the header to find them. */
4573
4574 int
4575 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4576 {
4577 const struct elf_backend_data *bed;
4578 int index;
4579
4580 if (elf_section_data (asect) != NULL
4581 && elf_section_data (asect)->this_idx != 0)
4582 return elf_section_data (asect)->this_idx;
4583
4584 if (bfd_is_abs_section (asect))
4585 index = SHN_ABS;
4586 else if (bfd_is_com_section (asect))
4587 index = SHN_COMMON;
4588 else if (bfd_is_und_section (asect))
4589 index = SHN_UNDEF;
4590 else
4591 {
4592 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4593 int maxindex = elf_numsections (abfd);
4594
4595 for (index = 1; index < maxindex; index++)
4596 {
4597 Elf_Internal_Shdr *hdr = i_shdrp[index];
4598
4599 if (hdr != NULL && hdr->bfd_section == asect)
4600 return index;
4601 }
4602 index = -1;
4603 }
4604
4605 bed = get_elf_backend_data (abfd);
4606 if (bed->elf_backend_section_from_bfd_section)
4607 {
4608 int retval = index;
4609
4610 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4611 return retval;
4612 }
4613
4614 if (index == -1)
4615 bfd_set_error (bfd_error_nonrepresentable_section);
4616
4617 return index;
4618 }
4619
4620 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4621 on error. */
4622
4623 int
4624 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4625 {
4626 asymbol *asym_ptr = *asym_ptr_ptr;
4627 int idx;
4628 flagword flags = asym_ptr->flags;
4629
4630 /* When gas creates relocations against local labels, it creates its
4631 own symbol for the section, but does put the symbol into the
4632 symbol chain, so udata is 0. When the linker is generating
4633 relocatable output, this section symbol may be for one of the
4634 input sections rather than the output section. */
4635 if (asym_ptr->udata.i == 0
4636 && (flags & BSF_SECTION_SYM)
4637 && asym_ptr->section)
4638 {
4639 int indx;
4640
4641 if (asym_ptr->section->output_section != NULL)
4642 indx = asym_ptr->section->output_section->index;
4643 else
4644 indx = asym_ptr->section->index;
4645 if (indx < elf_num_section_syms (abfd)
4646 && elf_section_syms (abfd)[indx] != NULL)
4647 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4648 }
4649
4650 idx = asym_ptr->udata.i;
4651
4652 if (idx == 0)
4653 {
4654 /* This case can occur when using --strip-symbol on a symbol
4655 which is used in a relocation entry. */
4656 (*_bfd_error_handler)
4657 (_("%s: symbol `%s' required but not present"),
4658 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4659 bfd_set_error (bfd_error_no_symbols);
4660 return -1;
4661 }
4662
4663 #if DEBUG & 4
4664 {
4665 fprintf (stderr,
4666 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4667 (long) asym_ptr, asym_ptr->name, idx, flags,
4668 elf_symbol_flags (flags));
4669 fflush (stderr);
4670 }
4671 #endif
4672
4673 return idx;
4674 }
4675
4676 /* Copy private BFD data. This copies any program header information. */
4677
4678 static bfd_boolean
4679 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4680 {
4681 Elf_Internal_Ehdr *iehdr;
4682 struct elf_segment_map *map;
4683 struct elf_segment_map *map_first;
4684 struct elf_segment_map **pointer_to_map;
4685 Elf_Internal_Phdr *segment;
4686 asection *section;
4687 unsigned int i;
4688 unsigned int num_segments;
4689 bfd_boolean phdr_included = FALSE;
4690 bfd_vma maxpagesize;
4691 struct elf_segment_map *phdr_adjust_seg = NULL;
4692 unsigned int phdr_adjust_num = 0;
4693 const struct elf_backend_data *bed;
4694
4695 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4696 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4697 return TRUE;
4698
4699 if (elf_tdata (ibfd)->phdr == NULL)
4700 return TRUE;
4701
4702 bed = get_elf_backend_data (ibfd);
4703 iehdr = elf_elfheader (ibfd);
4704
4705 map_first = NULL;
4706 pointer_to_map = &map_first;
4707
4708 num_segments = elf_elfheader (ibfd)->e_phnum;
4709 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4710
4711 /* Returns the end address of the segment + 1. */
4712 #define SEGMENT_END(segment, start) \
4713 (start + (segment->p_memsz > segment->p_filesz \
4714 ? segment->p_memsz : segment->p_filesz))
4715
4716 #define SECTION_SIZE(section, segment) \
4717 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4718 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4719 ? section->_raw_size : 0)
4720
4721 /* Returns TRUE if the given section is contained within
4722 the given segment. VMA addresses are compared. */
4723 #define IS_CONTAINED_BY_VMA(section, segment) \
4724 (section->vma >= segment->p_vaddr \
4725 && (section->vma + SECTION_SIZE (section, segment) \
4726 <= (SEGMENT_END (segment, segment->p_vaddr))))
4727
4728 /* Returns TRUE if the given section is contained within
4729 the given segment. LMA addresses are compared. */
4730 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4731 (section->lma >= base \
4732 && (section->lma + SECTION_SIZE (section, segment) \
4733 <= SEGMENT_END (segment, base)))
4734
4735 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4736 #define IS_COREFILE_NOTE(p, s) \
4737 (p->p_type == PT_NOTE \
4738 && bfd_get_format (ibfd) == bfd_core \
4739 && s->vma == 0 && s->lma == 0 \
4740 && (bfd_vma) s->filepos >= p->p_offset \
4741 && ((bfd_vma) s->filepos + s->_raw_size \
4742 <= p->p_offset + p->p_filesz))
4743
4744 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4745 linker, which generates a PT_INTERP section with p_vaddr and
4746 p_memsz set to 0. */
4747 #define IS_SOLARIS_PT_INTERP(p, s) \
4748 (p->p_vaddr == 0 \
4749 && p->p_paddr == 0 \
4750 && p->p_memsz == 0 \
4751 && p->p_filesz > 0 \
4752 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4753 && s->_raw_size > 0 \
4754 && (bfd_vma) s->filepos >= p->p_offset \
4755 && ((bfd_vma) s->filepos + s->_raw_size \
4756 <= p->p_offset + p->p_filesz))
4757
4758 /* Decide if the given section should be included in the given segment.
4759 A section will be included if:
4760 1. It is within the address space of the segment -- we use the LMA
4761 if that is set for the segment and the VMA otherwise,
4762 2. It is an allocated segment,
4763 3. There is an output section associated with it,
4764 4. The section has not already been allocated to a previous segment.
4765 5. PT_GNU_STACK segments do not include any sections.
4766 6. PT_TLS segment includes only SHF_TLS sections.
4767 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4768 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4769 ((((segment->p_paddr \
4770 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4771 : IS_CONTAINED_BY_VMA (section, segment)) \
4772 && (section->flags & SEC_ALLOC) != 0) \
4773 || IS_COREFILE_NOTE (segment, section)) \
4774 && section->output_section != NULL \
4775 && segment->p_type != PT_GNU_STACK \
4776 && (segment->p_type != PT_TLS \
4777 || (section->flags & SEC_THREAD_LOCAL)) \
4778 && (segment->p_type == PT_LOAD \
4779 || segment->p_type == PT_TLS \
4780 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4781 && ! section->segment_mark)
4782
4783 /* Returns TRUE iff seg1 starts after the end of seg2. */
4784 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4785 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4786
4787 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4788 their VMA address ranges and their LMA address ranges overlap.
4789 It is possible to have overlapping VMA ranges without overlapping LMA
4790 ranges. RedBoot images for example can have both .data and .bss mapped
4791 to the same VMA range, but with the .data section mapped to a different
4792 LMA. */
4793 #define SEGMENT_OVERLAPS(seg1, seg2) \
4794 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4795 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4796 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4797 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4798
4799 /* Initialise the segment mark field. */
4800 for (section = ibfd->sections; section != NULL; section = section->next)
4801 section->segment_mark = FALSE;
4802
4803 /* Scan through the segments specified in the program header
4804 of the input BFD. For this first scan we look for overlaps
4805 in the loadable segments. These can be created by weird
4806 parameters to objcopy. Also, fix some solaris weirdness. */
4807 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4808 i < num_segments;
4809 i++, segment++)
4810 {
4811 unsigned int j;
4812 Elf_Internal_Phdr *segment2;
4813
4814 if (segment->p_type == PT_INTERP)
4815 for (section = ibfd->sections; section; section = section->next)
4816 if (IS_SOLARIS_PT_INTERP (segment, section))
4817 {
4818 /* Mininal change so that the normal section to segment
4819 assignment code will work. */
4820 segment->p_vaddr = section->vma;
4821 break;
4822 }
4823
4824 if (segment->p_type != PT_LOAD)
4825 continue;
4826
4827 /* Determine if this segment overlaps any previous segments. */
4828 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4829 {
4830 bfd_signed_vma extra_length;
4831
4832 if (segment2->p_type != PT_LOAD
4833 || ! SEGMENT_OVERLAPS (segment, segment2))
4834 continue;
4835
4836 /* Merge the two segments together. */
4837 if (segment2->p_vaddr < segment->p_vaddr)
4838 {
4839 /* Extend SEGMENT2 to include SEGMENT and then delete
4840 SEGMENT. */
4841 extra_length =
4842 SEGMENT_END (segment, segment->p_vaddr)
4843 - SEGMENT_END (segment2, segment2->p_vaddr);
4844
4845 if (extra_length > 0)
4846 {
4847 segment2->p_memsz += extra_length;
4848 segment2->p_filesz += extra_length;
4849 }
4850
4851 segment->p_type = PT_NULL;
4852
4853 /* Since we have deleted P we must restart the outer loop. */
4854 i = 0;
4855 segment = elf_tdata (ibfd)->phdr;
4856 break;
4857 }
4858 else
4859 {
4860 /* Extend SEGMENT to include SEGMENT2 and then delete
4861 SEGMENT2. */
4862 extra_length =
4863 SEGMENT_END (segment2, segment2->p_vaddr)
4864 - SEGMENT_END (segment, segment->p_vaddr);
4865
4866 if (extra_length > 0)
4867 {
4868 segment->p_memsz += extra_length;
4869 segment->p_filesz += extra_length;
4870 }
4871
4872 segment2->p_type = PT_NULL;
4873 }
4874 }
4875 }
4876
4877 /* The second scan attempts to assign sections to segments. */
4878 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4879 i < num_segments;
4880 i ++, segment ++)
4881 {
4882 unsigned int section_count;
4883 asection ** sections;
4884 asection * output_section;
4885 unsigned int isec;
4886 bfd_vma matching_lma;
4887 bfd_vma suggested_lma;
4888 unsigned int j;
4889 bfd_size_type amt;
4890
4891 if (segment->p_type == PT_NULL)
4892 continue;
4893
4894 /* Compute how many sections might be placed into this segment. */
4895 for (section = ibfd->sections, section_count = 0;
4896 section != NULL;
4897 section = section->next)
4898 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4899 ++section_count;
4900
4901 /* Allocate a segment map big enough to contain
4902 all of the sections we have selected. */
4903 amt = sizeof (struct elf_segment_map);
4904 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4905 map = bfd_alloc (obfd, amt);
4906 if (map == NULL)
4907 return FALSE;
4908
4909 /* Initialise the fields of the segment map. Default to
4910 using the physical address of the segment in the input BFD. */
4911 map->next = NULL;
4912 map->p_type = segment->p_type;
4913 map->p_flags = segment->p_flags;
4914 map->p_flags_valid = 1;
4915 map->p_paddr = segment->p_paddr;
4916 map->p_paddr_valid = 1;
4917
4918 /* Determine if this segment contains the ELF file header
4919 and if it contains the program headers themselves. */
4920 map->includes_filehdr = (segment->p_offset == 0
4921 && segment->p_filesz >= iehdr->e_ehsize);
4922
4923 map->includes_phdrs = 0;
4924
4925 if (! phdr_included || segment->p_type != PT_LOAD)
4926 {
4927 map->includes_phdrs =
4928 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4929 && (segment->p_offset + segment->p_filesz
4930 >= ((bfd_vma) iehdr->e_phoff
4931 + iehdr->e_phnum * iehdr->e_phentsize)));
4932
4933 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4934 phdr_included = TRUE;
4935 }
4936
4937 if (section_count == 0)
4938 {
4939 /* Special segments, such as the PT_PHDR segment, may contain
4940 no sections, but ordinary, loadable segments should contain
4941 something. They are allowed by the ELF spec however, so only
4942 a warning is produced. */
4943 if (segment->p_type == PT_LOAD)
4944 (*_bfd_error_handler)
4945 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4946 bfd_archive_filename (ibfd));
4947
4948 map->count = 0;
4949 *pointer_to_map = map;
4950 pointer_to_map = &map->next;
4951
4952 continue;
4953 }
4954
4955 /* Now scan the sections in the input BFD again and attempt
4956 to add their corresponding output sections to the segment map.
4957 The problem here is how to handle an output section which has
4958 been moved (ie had its LMA changed). There are four possibilities:
4959
4960 1. None of the sections have been moved.
4961 In this case we can continue to use the segment LMA from the
4962 input BFD.
4963
4964 2. All of the sections have been moved by the same amount.
4965 In this case we can change the segment's LMA to match the LMA
4966 of the first section.
4967
4968 3. Some of the sections have been moved, others have not.
4969 In this case those sections which have not been moved can be
4970 placed in the current segment which will have to have its size,
4971 and possibly its LMA changed, and a new segment or segments will
4972 have to be created to contain the other sections.
4973
4974 4. The sections have been moved, but not by the same amount.
4975 In this case we can change the segment's LMA to match the LMA
4976 of the first section and we will have to create a new segment
4977 or segments to contain the other sections.
4978
4979 In order to save time, we allocate an array to hold the section
4980 pointers that we are interested in. As these sections get assigned
4981 to a segment, they are removed from this array. */
4982
4983 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4984 to work around this long long bug. */
4985 amt = section_count * sizeof (asection *);
4986 sections = bfd_malloc (amt);
4987 if (sections == NULL)
4988 return FALSE;
4989
4990 /* Step One: Scan for segment vs section LMA conflicts.
4991 Also add the sections to the section array allocated above.
4992 Also add the sections to the current segment. In the common
4993 case, where the sections have not been moved, this means that
4994 we have completely filled the segment, and there is nothing
4995 more to do. */
4996 isec = 0;
4997 matching_lma = 0;
4998 suggested_lma = 0;
4999
5000 for (j = 0, section = ibfd->sections;
5001 section != NULL;
5002 section = section->next)
5003 {
5004 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5005 {
5006 output_section = section->output_section;
5007
5008 sections[j ++] = section;
5009
5010 /* The Solaris native linker always sets p_paddr to 0.
5011 We try to catch that case here, and set it to the
5012 correct value. Note - some backends require that
5013 p_paddr be left as zero. */
5014 if (segment->p_paddr == 0
5015 && segment->p_vaddr != 0
5016 && (! bed->want_p_paddr_set_to_zero)
5017 && isec == 0
5018 && output_section->lma != 0
5019 && (output_section->vma == (segment->p_vaddr
5020 + (map->includes_filehdr
5021 ? iehdr->e_ehsize
5022 : 0)
5023 + (map->includes_phdrs
5024 ? (iehdr->e_phnum
5025 * iehdr->e_phentsize)
5026 : 0))))
5027 map->p_paddr = segment->p_vaddr;
5028
5029 /* Match up the physical address of the segment with the
5030 LMA address of the output section. */
5031 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5032 || IS_COREFILE_NOTE (segment, section)
5033 || (bed->want_p_paddr_set_to_zero &&
5034 IS_CONTAINED_BY_VMA (output_section, segment))
5035 )
5036 {
5037 if (matching_lma == 0)
5038 matching_lma = output_section->lma;
5039
5040 /* We assume that if the section fits within the segment
5041 then it does not overlap any other section within that
5042 segment. */
5043 map->sections[isec ++] = output_section;
5044 }
5045 else if (suggested_lma == 0)
5046 suggested_lma = output_section->lma;
5047 }
5048 }
5049
5050 BFD_ASSERT (j == section_count);
5051
5052 /* Step Two: Adjust the physical address of the current segment,
5053 if necessary. */
5054 if (isec == section_count)
5055 {
5056 /* All of the sections fitted within the segment as currently
5057 specified. This is the default case. Add the segment to
5058 the list of built segments and carry on to process the next
5059 program header in the input BFD. */
5060 map->count = section_count;
5061 *pointer_to_map = map;
5062 pointer_to_map = &map->next;
5063
5064 free (sections);
5065 continue;
5066 }
5067 else
5068 {
5069 if (matching_lma != 0)
5070 {
5071 /* At least one section fits inside the current segment.
5072 Keep it, but modify its physical address to match the
5073 LMA of the first section that fitted. */
5074 map->p_paddr = matching_lma;
5075 }
5076 else
5077 {
5078 /* None of the sections fitted inside the current segment.
5079 Change the current segment's physical address to match
5080 the LMA of the first section. */
5081 map->p_paddr = suggested_lma;
5082 }
5083
5084 /* Offset the segment physical address from the lma
5085 to allow for space taken up by elf headers. */
5086 if (map->includes_filehdr)
5087 map->p_paddr -= iehdr->e_ehsize;
5088
5089 if (map->includes_phdrs)
5090 {
5091 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5092
5093 /* iehdr->e_phnum is just an estimate of the number
5094 of program headers that we will need. Make a note
5095 here of the number we used and the segment we chose
5096 to hold these headers, so that we can adjust the
5097 offset when we know the correct value. */
5098 phdr_adjust_num = iehdr->e_phnum;
5099 phdr_adjust_seg = map;
5100 }
5101 }
5102
5103 /* Step Three: Loop over the sections again, this time assigning
5104 those that fit to the current segment and removing them from the
5105 sections array; but making sure not to leave large gaps. Once all
5106 possible sections have been assigned to the current segment it is
5107 added to the list of built segments and if sections still remain
5108 to be assigned, a new segment is constructed before repeating
5109 the loop. */
5110 isec = 0;
5111 do
5112 {
5113 map->count = 0;
5114 suggested_lma = 0;
5115
5116 /* Fill the current segment with sections that fit. */
5117 for (j = 0; j < section_count; j++)
5118 {
5119 section = sections[j];
5120
5121 if (section == NULL)
5122 continue;
5123
5124 output_section = section->output_section;
5125
5126 BFD_ASSERT (output_section != NULL);
5127
5128 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5129 || IS_COREFILE_NOTE (segment, section))
5130 {
5131 if (map->count == 0)
5132 {
5133 /* If the first section in a segment does not start at
5134 the beginning of the segment, then something is
5135 wrong. */
5136 if (output_section->lma !=
5137 (map->p_paddr
5138 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5139 + (map->includes_phdrs
5140 ? iehdr->e_phnum * iehdr->e_phentsize
5141 : 0)))
5142 abort ();
5143 }
5144 else
5145 {
5146 asection * prev_sec;
5147
5148 prev_sec = map->sections[map->count - 1];
5149
5150 /* If the gap between the end of the previous section
5151 and the start of this section is more than
5152 maxpagesize then we need to start a new segment. */
5153 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
5154 maxpagesize)
5155 < BFD_ALIGN (output_section->lma, maxpagesize))
5156 || ((prev_sec->lma + prev_sec->_raw_size)
5157 > output_section->lma))
5158 {
5159 if (suggested_lma == 0)
5160 suggested_lma = output_section->lma;
5161
5162 continue;
5163 }
5164 }
5165
5166 map->sections[map->count++] = output_section;
5167 ++isec;
5168 sections[j] = NULL;
5169 section->segment_mark = TRUE;
5170 }
5171 else if (suggested_lma == 0)
5172 suggested_lma = output_section->lma;
5173 }
5174
5175 BFD_ASSERT (map->count > 0);
5176
5177 /* Add the current segment to the list of built segments. */
5178 *pointer_to_map = map;
5179 pointer_to_map = &map->next;
5180
5181 if (isec < section_count)
5182 {
5183 /* We still have not allocated all of the sections to
5184 segments. Create a new segment here, initialise it
5185 and carry on looping. */
5186 amt = sizeof (struct elf_segment_map);
5187 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5188 map = bfd_alloc (obfd, amt);
5189 if (map == NULL)
5190 {
5191 free (sections);
5192 return FALSE;
5193 }
5194
5195 /* Initialise the fields of the segment map. Set the physical
5196 physical address to the LMA of the first section that has
5197 not yet been assigned. */
5198 map->next = NULL;
5199 map->p_type = segment->p_type;
5200 map->p_flags = segment->p_flags;
5201 map->p_flags_valid = 1;
5202 map->p_paddr = suggested_lma;
5203 map->p_paddr_valid = 1;
5204 map->includes_filehdr = 0;
5205 map->includes_phdrs = 0;
5206 }
5207 }
5208 while (isec < section_count);
5209
5210 free (sections);
5211 }
5212
5213 /* The Solaris linker creates program headers in which all the
5214 p_paddr fields are zero. When we try to objcopy or strip such a
5215 file, we get confused. Check for this case, and if we find it
5216 reset the p_paddr_valid fields. */
5217 for (map = map_first; map != NULL; map = map->next)
5218 if (map->p_paddr != 0)
5219 break;
5220 if (map == NULL)
5221 for (map = map_first; map != NULL; map = map->next)
5222 map->p_paddr_valid = 0;
5223
5224 elf_tdata (obfd)->segment_map = map_first;
5225
5226 /* If we had to estimate the number of program headers that were
5227 going to be needed, then check our estimate now and adjust
5228 the offset if necessary. */
5229 if (phdr_adjust_seg != NULL)
5230 {
5231 unsigned int count;
5232
5233 for (count = 0, map = map_first; map != NULL; map = map->next)
5234 count++;
5235
5236 if (count > phdr_adjust_num)
5237 phdr_adjust_seg->p_paddr
5238 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5239 }
5240
5241 #if 0
5242 /* Final Step: Sort the segments into ascending order of physical
5243 address. */
5244 if (map_first != NULL)
5245 {
5246 struct elf_segment_map *prev;
5247
5248 prev = map_first;
5249 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5250 {
5251 /* Yes I know - its a bubble sort.... */
5252 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5253 {
5254 /* Swap map and map->next. */
5255 prev->next = map->next;
5256 map->next = map->next->next;
5257 prev->next->next = map;
5258
5259 /* Restart loop. */
5260 map = map_first;
5261 }
5262 }
5263 }
5264 #endif
5265
5266 #undef SEGMENT_END
5267 #undef SECTION_SIZE
5268 #undef IS_CONTAINED_BY_VMA
5269 #undef IS_CONTAINED_BY_LMA
5270 #undef IS_COREFILE_NOTE
5271 #undef IS_SOLARIS_PT_INTERP
5272 #undef INCLUDE_SECTION_IN_SEGMENT
5273 #undef SEGMENT_AFTER_SEGMENT
5274 #undef SEGMENT_OVERLAPS
5275 return TRUE;
5276 }
5277
5278 /* Copy private section information. This copies over the entsize
5279 field, and sometimes the info field. */
5280
5281 bfd_boolean
5282 _bfd_elf_copy_private_section_data (bfd *ibfd,
5283 asection *isec,
5284 bfd *obfd,
5285 asection *osec)
5286 {
5287 Elf_Internal_Shdr *ihdr, *ohdr;
5288
5289 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5290 || obfd->xvec->flavour != bfd_target_elf_flavour)
5291 return TRUE;
5292
5293 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5294 {
5295 asection *s;
5296
5297 /* Only set up the segments if there are no more SEC_ALLOC
5298 sections. FIXME: This won't do the right thing if objcopy is
5299 used to remove the last SEC_ALLOC section, since objcopy
5300 won't call this routine in that case. */
5301 for (s = isec->next; s != NULL; s = s->next)
5302 if ((s->flags & SEC_ALLOC) != 0)
5303 break;
5304 if (s == NULL)
5305 {
5306 if (! copy_private_bfd_data (ibfd, obfd))
5307 return FALSE;
5308 }
5309 }
5310
5311 ihdr = &elf_section_data (isec)->this_hdr;
5312 ohdr = &elf_section_data (osec)->this_hdr;
5313
5314 ohdr->sh_entsize = ihdr->sh_entsize;
5315
5316 if (ihdr->sh_type == SHT_SYMTAB
5317 || ihdr->sh_type == SHT_DYNSYM
5318 || ihdr->sh_type == SHT_GNU_verneed
5319 || ihdr->sh_type == SHT_GNU_verdef)
5320 ohdr->sh_info = ihdr->sh_info;
5321
5322 /* Set things up for objcopy. The output SHT_GROUP section will
5323 have its elf_next_in_group pointing back to the input group
5324 members. */
5325 elf_next_in_group (osec) = elf_next_in_group (isec);
5326 elf_group_name (osec) = elf_group_name (isec);
5327
5328 osec->use_rela_p = isec->use_rela_p;
5329
5330 return TRUE;
5331 }
5332
5333 /* Copy private symbol information. If this symbol is in a section
5334 which we did not map into a BFD section, try to map the section
5335 index correctly. We use special macro definitions for the mapped
5336 section indices; these definitions are interpreted by the
5337 swap_out_syms function. */
5338
5339 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5340 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5341 #define MAP_STRTAB (SHN_HIOS + 3)
5342 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5343 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5344
5345 bfd_boolean
5346 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5347 asymbol *isymarg,
5348 bfd *obfd,
5349 asymbol *osymarg)
5350 {
5351 elf_symbol_type *isym, *osym;
5352
5353 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5354 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5355 return TRUE;
5356
5357 isym = elf_symbol_from (ibfd, isymarg);
5358 osym = elf_symbol_from (obfd, osymarg);
5359
5360 if (isym != NULL
5361 && osym != NULL
5362 && bfd_is_abs_section (isym->symbol.section))
5363 {
5364 unsigned int shndx;
5365
5366 shndx = isym->internal_elf_sym.st_shndx;
5367 if (shndx == elf_onesymtab (ibfd))
5368 shndx = MAP_ONESYMTAB;
5369 else if (shndx == elf_dynsymtab (ibfd))
5370 shndx = MAP_DYNSYMTAB;
5371 else if (shndx == elf_tdata (ibfd)->strtab_section)
5372 shndx = MAP_STRTAB;
5373 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5374 shndx = MAP_SHSTRTAB;
5375 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5376 shndx = MAP_SYM_SHNDX;
5377 osym->internal_elf_sym.st_shndx = shndx;
5378 }
5379
5380 return TRUE;
5381 }
5382
5383 /* Swap out the symbols. */
5384
5385 static bfd_boolean
5386 swap_out_syms (bfd *abfd,
5387 struct bfd_strtab_hash **sttp,
5388 int relocatable_p)
5389 {
5390 const struct elf_backend_data *bed;
5391 int symcount;
5392 asymbol **syms;
5393 struct bfd_strtab_hash *stt;
5394 Elf_Internal_Shdr *symtab_hdr;
5395 Elf_Internal_Shdr *symtab_shndx_hdr;
5396 Elf_Internal_Shdr *symstrtab_hdr;
5397 char *outbound_syms;
5398 char *outbound_shndx;
5399 int idx;
5400 bfd_size_type amt;
5401 bfd_boolean name_local_sections;
5402
5403 if (!elf_map_symbols (abfd))
5404 return FALSE;
5405
5406 /* Dump out the symtabs. */
5407 stt = _bfd_elf_stringtab_init ();
5408 if (stt == NULL)
5409 return FALSE;
5410
5411 bed = get_elf_backend_data (abfd);
5412 symcount = bfd_get_symcount (abfd);
5413 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5414 symtab_hdr->sh_type = SHT_SYMTAB;
5415 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5416 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5417 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5418 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5419
5420 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5421 symstrtab_hdr->sh_type = SHT_STRTAB;
5422
5423 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5424 outbound_syms = bfd_alloc (abfd, amt);
5425 if (outbound_syms == NULL)
5426 {
5427 _bfd_stringtab_free (stt);
5428 return FALSE;
5429 }
5430 symtab_hdr->contents = outbound_syms;
5431
5432 outbound_shndx = NULL;
5433 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5434 if (symtab_shndx_hdr->sh_name != 0)
5435 {
5436 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5437 outbound_shndx = bfd_zalloc (abfd, amt);
5438 if (outbound_shndx == NULL)
5439 {
5440 _bfd_stringtab_free (stt);
5441 return FALSE;
5442 }
5443
5444 symtab_shndx_hdr->contents = outbound_shndx;
5445 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5446 symtab_shndx_hdr->sh_size = amt;
5447 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5448 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5449 }
5450
5451 /* Now generate the data (for "contents"). */
5452 {
5453 /* Fill in zeroth symbol and swap it out. */
5454 Elf_Internal_Sym sym;
5455 sym.st_name = 0;
5456 sym.st_value = 0;
5457 sym.st_size = 0;
5458 sym.st_info = 0;
5459 sym.st_other = 0;
5460 sym.st_shndx = SHN_UNDEF;
5461 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5462 outbound_syms += bed->s->sizeof_sym;
5463 if (outbound_shndx != NULL)
5464 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5465 }
5466
5467 name_local_sections
5468 = (bed->elf_backend_name_local_section_symbols
5469 && bed->elf_backend_name_local_section_symbols (abfd));
5470
5471 syms = bfd_get_outsymbols (abfd);
5472 for (idx = 0; idx < symcount; idx++)
5473 {
5474 Elf_Internal_Sym sym;
5475 bfd_vma value = syms[idx]->value;
5476 elf_symbol_type *type_ptr;
5477 flagword flags = syms[idx]->flags;
5478 int type;
5479
5480 if (!name_local_sections
5481 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5482 {
5483 /* Local section symbols have no name. */
5484 sym.st_name = 0;
5485 }
5486 else
5487 {
5488 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5489 syms[idx]->name,
5490 TRUE, FALSE);
5491 if (sym.st_name == (unsigned long) -1)
5492 {
5493 _bfd_stringtab_free (stt);
5494 return FALSE;
5495 }
5496 }
5497
5498 type_ptr = elf_symbol_from (abfd, syms[idx]);
5499
5500 if ((flags & BSF_SECTION_SYM) == 0
5501 && bfd_is_com_section (syms[idx]->section))
5502 {
5503 /* ELF common symbols put the alignment into the `value' field,
5504 and the size into the `size' field. This is backwards from
5505 how BFD handles it, so reverse it here. */
5506 sym.st_size = value;
5507 if (type_ptr == NULL
5508 || type_ptr->internal_elf_sym.st_value == 0)
5509 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5510 else
5511 sym.st_value = type_ptr->internal_elf_sym.st_value;
5512 sym.st_shndx = _bfd_elf_section_from_bfd_section
5513 (abfd, syms[idx]->section);
5514 }
5515 else
5516 {
5517 asection *sec = syms[idx]->section;
5518 int shndx;
5519
5520 if (sec->output_section)
5521 {
5522 value += sec->output_offset;
5523 sec = sec->output_section;
5524 }
5525
5526 /* Don't add in the section vma for relocatable output. */
5527 if (! relocatable_p)
5528 value += sec->vma;
5529 sym.st_value = value;
5530 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5531
5532 if (bfd_is_abs_section (sec)
5533 && type_ptr != NULL
5534 && type_ptr->internal_elf_sym.st_shndx != 0)
5535 {
5536 /* This symbol is in a real ELF section which we did
5537 not create as a BFD section. Undo the mapping done
5538 by copy_private_symbol_data. */
5539 shndx = type_ptr->internal_elf_sym.st_shndx;
5540 switch (shndx)
5541 {
5542 case MAP_ONESYMTAB:
5543 shndx = elf_onesymtab (abfd);
5544 break;
5545 case MAP_DYNSYMTAB:
5546 shndx = elf_dynsymtab (abfd);
5547 break;
5548 case MAP_STRTAB:
5549 shndx = elf_tdata (abfd)->strtab_section;
5550 break;
5551 case MAP_SHSTRTAB:
5552 shndx = elf_tdata (abfd)->shstrtab_section;
5553 break;
5554 case MAP_SYM_SHNDX:
5555 shndx = elf_tdata (abfd)->symtab_shndx_section;
5556 break;
5557 default:
5558 break;
5559 }
5560 }
5561 else
5562 {
5563 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5564
5565 if (shndx == -1)
5566 {
5567 asection *sec2;
5568
5569 /* Writing this would be a hell of a lot easier if
5570 we had some decent documentation on bfd, and
5571 knew what to expect of the library, and what to
5572 demand of applications. For example, it
5573 appears that `objcopy' might not set the
5574 section of a symbol to be a section that is
5575 actually in the output file. */
5576 sec2 = bfd_get_section_by_name (abfd, sec->name);
5577 if (sec2 == NULL)
5578 {
5579 _bfd_error_handler (_("\
5580 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5581 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5582 sec->name);
5583 bfd_set_error (bfd_error_invalid_operation);
5584 _bfd_stringtab_free (stt);
5585 return FALSE;
5586 }
5587
5588 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5589 BFD_ASSERT (shndx != -1);
5590 }
5591 }
5592
5593 sym.st_shndx = shndx;
5594 }
5595
5596 if ((flags & BSF_THREAD_LOCAL) != 0)
5597 type = STT_TLS;
5598 else if ((flags & BSF_FUNCTION) != 0)
5599 type = STT_FUNC;
5600 else if ((flags & BSF_OBJECT) != 0)
5601 type = STT_OBJECT;
5602 else
5603 type = STT_NOTYPE;
5604
5605 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5606 type = STT_TLS;
5607
5608 /* Processor-specific types. */
5609 if (type_ptr != NULL
5610 && bed->elf_backend_get_symbol_type)
5611 type = ((*bed->elf_backend_get_symbol_type)
5612 (&type_ptr->internal_elf_sym, type));
5613
5614 if (flags & BSF_SECTION_SYM)
5615 {
5616 if (flags & BSF_GLOBAL)
5617 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5618 else
5619 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5620 }
5621 else if (bfd_is_com_section (syms[idx]->section))
5622 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5623 else if (bfd_is_und_section (syms[idx]->section))
5624 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5625 ? STB_WEAK
5626 : STB_GLOBAL),
5627 type);
5628 else if (flags & BSF_FILE)
5629 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5630 else
5631 {
5632 int bind = STB_LOCAL;
5633
5634 if (flags & BSF_LOCAL)
5635 bind = STB_LOCAL;
5636 else if (flags & BSF_WEAK)
5637 bind = STB_WEAK;
5638 else if (flags & BSF_GLOBAL)
5639 bind = STB_GLOBAL;
5640
5641 sym.st_info = ELF_ST_INFO (bind, type);
5642 }
5643
5644 if (type_ptr != NULL)
5645 sym.st_other = type_ptr->internal_elf_sym.st_other;
5646 else
5647 sym.st_other = 0;
5648
5649 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5650 outbound_syms += bed->s->sizeof_sym;
5651 if (outbound_shndx != NULL)
5652 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5653 }
5654
5655 *sttp = stt;
5656 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5657 symstrtab_hdr->sh_type = SHT_STRTAB;
5658
5659 symstrtab_hdr->sh_flags = 0;
5660 symstrtab_hdr->sh_addr = 0;
5661 symstrtab_hdr->sh_entsize = 0;
5662 symstrtab_hdr->sh_link = 0;
5663 symstrtab_hdr->sh_info = 0;
5664 symstrtab_hdr->sh_addralign = 1;
5665
5666 return TRUE;
5667 }
5668
5669 /* Return the number of bytes required to hold the symtab vector.
5670
5671 Note that we base it on the count plus 1, since we will null terminate
5672 the vector allocated based on this size. However, the ELF symbol table
5673 always has a dummy entry as symbol #0, so it ends up even. */
5674
5675 long
5676 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
5677 {
5678 long symcount;
5679 long symtab_size;
5680 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5681
5682 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5683 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5684 if (symcount > 0)
5685 symtab_size -= sizeof (asymbol *);
5686
5687 return symtab_size;
5688 }
5689
5690 long
5691 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
5692 {
5693 long symcount;
5694 long symtab_size;
5695 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5696
5697 if (elf_dynsymtab (abfd) == 0)
5698 {
5699 bfd_set_error (bfd_error_invalid_operation);
5700 return -1;
5701 }
5702
5703 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5704 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5705 if (symcount > 0)
5706 symtab_size -= sizeof (asymbol *);
5707
5708 return symtab_size;
5709 }
5710
5711 long
5712 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
5713 sec_ptr asect)
5714 {
5715 return (asect->reloc_count + 1) * sizeof (arelent *);
5716 }
5717
5718 /* Canonicalize the relocs. */
5719
5720 long
5721 _bfd_elf_canonicalize_reloc (bfd *abfd,
5722 sec_ptr section,
5723 arelent **relptr,
5724 asymbol **symbols)
5725 {
5726 arelent *tblptr;
5727 unsigned int i;
5728 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5729
5730 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5731 return -1;
5732
5733 tblptr = section->relocation;
5734 for (i = 0; i < section->reloc_count; i++)
5735 *relptr++ = tblptr++;
5736
5737 *relptr = NULL;
5738
5739 return section->reloc_count;
5740 }
5741
5742 long
5743 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
5744 {
5745 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5746 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
5747
5748 if (symcount >= 0)
5749 bfd_get_symcount (abfd) = symcount;
5750 return symcount;
5751 }
5752
5753 long
5754 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
5755 asymbol **allocation)
5756 {
5757 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5758 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
5759
5760 if (symcount >= 0)
5761 bfd_get_dynamic_symcount (abfd) = symcount;
5762 return symcount;
5763 }
5764
5765 /* Return the size required for the dynamic reloc entries. Any
5766 section that was actually installed in the BFD, and has type
5767 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5768 considered to be a dynamic reloc section. */
5769
5770 long
5771 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
5772 {
5773 long ret;
5774 asection *s;
5775
5776 if (elf_dynsymtab (abfd) == 0)
5777 {
5778 bfd_set_error (bfd_error_invalid_operation);
5779 return -1;
5780 }
5781
5782 ret = sizeof (arelent *);
5783 for (s = abfd->sections; s != NULL; s = s->next)
5784 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5785 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5786 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5787 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5788 * sizeof (arelent *));
5789
5790 return ret;
5791 }
5792
5793 /* Canonicalize the dynamic relocation entries. Note that we return
5794 the dynamic relocations as a single block, although they are
5795 actually associated with particular sections; the interface, which
5796 was designed for SunOS style shared libraries, expects that there
5797 is only one set of dynamic relocs. Any section that was actually
5798 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5799 the dynamic symbol table, is considered to be a dynamic reloc
5800 section. */
5801
5802 long
5803 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
5804 arelent **storage,
5805 asymbol **syms)
5806 {
5807 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
5808 asection *s;
5809 long ret;
5810
5811 if (elf_dynsymtab (abfd) == 0)
5812 {
5813 bfd_set_error (bfd_error_invalid_operation);
5814 return -1;
5815 }
5816
5817 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5818 ret = 0;
5819 for (s = abfd->sections; s != NULL; s = s->next)
5820 {
5821 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5822 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5823 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5824 {
5825 arelent *p;
5826 long count, i;
5827
5828 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
5829 return -1;
5830 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5831 p = s->relocation;
5832 for (i = 0; i < count; i++)
5833 *storage++ = p++;
5834 ret += count;
5835 }
5836 }
5837
5838 *storage = NULL;
5839
5840 return ret;
5841 }
5842 \f
5843 /* Read in the version information. */
5844
5845 bfd_boolean
5846 _bfd_elf_slurp_version_tables (bfd *abfd)
5847 {
5848 bfd_byte *contents = NULL;
5849 bfd_size_type amt;
5850
5851 if (elf_dynverdef (abfd) != 0)
5852 {
5853 Elf_Internal_Shdr *hdr;
5854 Elf_External_Verdef *everdef;
5855 Elf_Internal_Verdef *iverdef;
5856 Elf_Internal_Verdef *iverdefarr;
5857 Elf_Internal_Verdef iverdefmem;
5858 unsigned int i;
5859 unsigned int maxidx;
5860
5861 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5862
5863 contents = bfd_malloc (hdr->sh_size);
5864 if (contents == NULL)
5865 goto error_return;
5866 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5867 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5868 goto error_return;
5869
5870 /* We know the number of entries in the section but not the maximum
5871 index. Therefore we have to run through all entries and find
5872 the maximum. */
5873 everdef = (Elf_External_Verdef *) contents;
5874 maxidx = 0;
5875 for (i = 0; i < hdr->sh_info; ++i)
5876 {
5877 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5878
5879 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5880 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5881
5882 everdef = ((Elf_External_Verdef *)
5883 ((bfd_byte *) everdef + iverdefmem.vd_next));
5884 }
5885
5886 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5887 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
5888 if (elf_tdata (abfd)->verdef == NULL)
5889 goto error_return;
5890
5891 elf_tdata (abfd)->cverdefs = maxidx;
5892
5893 everdef = (Elf_External_Verdef *) contents;
5894 iverdefarr = elf_tdata (abfd)->verdef;
5895 for (i = 0; i < hdr->sh_info; i++)
5896 {
5897 Elf_External_Verdaux *everdaux;
5898 Elf_Internal_Verdaux *iverdaux;
5899 unsigned int j;
5900
5901 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5902
5903 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5904 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5905
5906 iverdef->vd_bfd = abfd;
5907
5908 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5909 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
5910 if (iverdef->vd_auxptr == NULL)
5911 goto error_return;
5912
5913 everdaux = ((Elf_External_Verdaux *)
5914 ((bfd_byte *) everdef + iverdef->vd_aux));
5915 iverdaux = iverdef->vd_auxptr;
5916 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5917 {
5918 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5919
5920 iverdaux->vda_nodename =
5921 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5922 iverdaux->vda_name);
5923 if (iverdaux->vda_nodename == NULL)
5924 goto error_return;
5925
5926 if (j + 1 < iverdef->vd_cnt)
5927 iverdaux->vda_nextptr = iverdaux + 1;
5928 else
5929 iverdaux->vda_nextptr = NULL;
5930
5931 everdaux = ((Elf_External_Verdaux *)
5932 ((bfd_byte *) everdaux + iverdaux->vda_next));
5933 }
5934
5935 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5936
5937 if (i + 1 < hdr->sh_info)
5938 iverdef->vd_nextdef = iverdef + 1;
5939 else
5940 iverdef->vd_nextdef = NULL;
5941
5942 everdef = ((Elf_External_Verdef *)
5943 ((bfd_byte *) everdef + iverdef->vd_next));
5944 }
5945
5946 free (contents);
5947 contents = NULL;
5948 }
5949
5950 if (elf_dynverref (abfd) != 0)
5951 {
5952 Elf_Internal_Shdr *hdr;
5953 Elf_External_Verneed *everneed;
5954 Elf_Internal_Verneed *iverneed;
5955 unsigned int i;
5956
5957 hdr = &elf_tdata (abfd)->dynverref_hdr;
5958
5959 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5960 elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
5961 if (elf_tdata (abfd)->verref == NULL)
5962 goto error_return;
5963
5964 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5965
5966 contents = bfd_malloc (hdr->sh_size);
5967 if (contents == NULL)
5968 goto error_return;
5969 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5970 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5971 goto error_return;
5972
5973 everneed = (Elf_External_Verneed *) contents;
5974 iverneed = elf_tdata (abfd)->verref;
5975 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5976 {
5977 Elf_External_Vernaux *evernaux;
5978 Elf_Internal_Vernaux *ivernaux;
5979 unsigned int j;
5980
5981 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5982
5983 iverneed->vn_bfd = abfd;
5984
5985 iverneed->vn_filename =
5986 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5987 iverneed->vn_file);
5988 if (iverneed->vn_filename == NULL)
5989 goto error_return;
5990
5991 amt = iverneed->vn_cnt;
5992 amt *= sizeof (Elf_Internal_Vernaux);
5993 iverneed->vn_auxptr = bfd_alloc (abfd, amt);
5994
5995 evernaux = ((Elf_External_Vernaux *)
5996 ((bfd_byte *) everneed + iverneed->vn_aux));
5997 ivernaux = iverneed->vn_auxptr;
5998 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5999 {
6000 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6001
6002 ivernaux->vna_nodename =
6003 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6004 ivernaux->vna_name);
6005 if (ivernaux->vna_nodename == NULL)
6006 goto error_return;
6007
6008 if (j + 1 < iverneed->vn_cnt)
6009 ivernaux->vna_nextptr = ivernaux + 1;
6010 else
6011 ivernaux->vna_nextptr = NULL;
6012
6013 evernaux = ((Elf_External_Vernaux *)
6014 ((bfd_byte *) evernaux + ivernaux->vna_next));
6015 }
6016
6017 if (i + 1 < hdr->sh_info)
6018 iverneed->vn_nextref = iverneed + 1;
6019 else
6020 iverneed->vn_nextref = NULL;
6021
6022 everneed = ((Elf_External_Verneed *)
6023 ((bfd_byte *) everneed + iverneed->vn_next));
6024 }
6025
6026 free (contents);
6027 contents = NULL;
6028 }
6029
6030 return TRUE;
6031
6032 error_return:
6033 if (contents != NULL)
6034 free (contents);
6035 return FALSE;
6036 }
6037 \f
6038 asymbol *
6039 _bfd_elf_make_empty_symbol (bfd *abfd)
6040 {
6041 elf_symbol_type *newsym;
6042 bfd_size_type amt = sizeof (elf_symbol_type);
6043
6044 newsym = bfd_zalloc (abfd, amt);
6045 if (!newsym)
6046 return NULL;
6047 else
6048 {
6049 newsym->symbol.the_bfd = abfd;
6050 return &newsym->symbol;
6051 }
6052 }
6053
6054 void
6055 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6056 asymbol *symbol,
6057 symbol_info *ret)
6058 {
6059 bfd_symbol_info (symbol, ret);
6060 }
6061
6062 /* Return whether a symbol name implies a local symbol. Most targets
6063 use this function for the is_local_label_name entry point, but some
6064 override it. */
6065
6066 bfd_boolean
6067 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6068 const char *name)
6069 {
6070 /* Normal local symbols start with ``.L''. */
6071 if (name[0] == '.' && name[1] == 'L')
6072 return TRUE;
6073
6074 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6075 DWARF debugging symbols starting with ``..''. */
6076 if (name[0] == '.' && name[1] == '.')
6077 return TRUE;
6078
6079 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6080 emitting DWARF debugging output. I suspect this is actually a
6081 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6082 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6083 underscore to be emitted on some ELF targets). For ease of use,
6084 we treat such symbols as local. */
6085 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6086 return TRUE;
6087
6088 return FALSE;
6089 }
6090
6091 alent *
6092 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6093 asymbol *symbol ATTRIBUTE_UNUSED)
6094 {
6095 abort ();
6096 return NULL;
6097 }
6098
6099 bfd_boolean
6100 _bfd_elf_set_arch_mach (bfd *abfd,
6101 enum bfd_architecture arch,
6102 unsigned long machine)
6103 {
6104 /* If this isn't the right architecture for this backend, and this
6105 isn't the generic backend, fail. */
6106 if (arch != get_elf_backend_data (abfd)->arch
6107 && arch != bfd_arch_unknown
6108 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6109 return FALSE;
6110
6111 return bfd_default_set_arch_mach (abfd, arch, machine);
6112 }
6113
6114 /* Find the function to a particular section and offset,
6115 for error reporting. */
6116
6117 static bfd_boolean
6118 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6119 asection *section,
6120 asymbol **symbols,
6121 bfd_vma offset,
6122 const char **filename_ptr,
6123 const char **functionname_ptr)
6124 {
6125 const char *filename;
6126 asymbol *func;
6127 bfd_vma low_func;
6128 asymbol **p;
6129
6130 filename = NULL;
6131 func = NULL;
6132 low_func = 0;
6133
6134 for (p = symbols; *p != NULL; p++)
6135 {
6136 elf_symbol_type *q;
6137
6138 q = (elf_symbol_type *) *p;
6139
6140 if (bfd_get_section (&q->symbol) != section)
6141 continue;
6142
6143 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6144 {
6145 default:
6146 break;
6147 case STT_FILE:
6148 filename = bfd_asymbol_name (&q->symbol);
6149 break;
6150 case STT_NOTYPE:
6151 case STT_FUNC:
6152 if (q->symbol.section == section
6153 && q->symbol.value >= low_func
6154 && q->symbol.value <= offset)
6155 {
6156 func = (asymbol *) q;
6157 low_func = q->symbol.value;
6158 }
6159 break;
6160 }
6161 }
6162
6163 if (func == NULL)
6164 return FALSE;
6165
6166 if (filename_ptr)
6167 *filename_ptr = filename;
6168 if (functionname_ptr)
6169 *functionname_ptr = bfd_asymbol_name (func);
6170
6171 return TRUE;
6172 }
6173
6174 /* Find the nearest line to a particular section and offset,
6175 for error reporting. */
6176
6177 bfd_boolean
6178 _bfd_elf_find_nearest_line (bfd *abfd,
6179 asection *section,
6180 asymbol **symbols,
6181 bfd_vma offset,
6182 const char **filename_ptr,
6183 const char **functionname_ptr,
6184 unsigned int *line_ptr)
6185 {
6186 bfd_boolean found;
6187
6188 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6189 filename_ptr, functionname_ptr,
6190 line_ptr))
6191 {
6192 if (!*functionname_ptr)
6193 elf_find_function (abfd, section, symbols, offset,
6194 *filename_ptr ? NULL : filename_ptr,
6195 functionname_ptr);
6196
6197 return TRUE;
6198 }
6199
6200 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6201 filename_ptr, functionname_ptr,
6202 line_ptr, 0,
6203 &elf_tdata (abfd)->dwarf2_find_line_info))
6204 {
6205 if (!*functionname_ptr)
6206 elf_find_function (abfd, section, symbols, offset,
6207 *filename_ptr ? NULL : filename_ptr,
6208 functionname_ptr);
6209
6210 return TRUE;
6211 }
6212
6213 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6214 &found, filename_ptr,
6215 functionname_ptr, line_ptr,
6216 &elf_tdata (abfd)->line_info))
6217 return FALSE;
6218 if (found && (*functionname_ptr || *line_ptr))
6219 return TRUE;
6220
6221 if (symbols == NULL)
6222 return FALSE;
6223
6224 if (! elf_find_function (abfd, section, symbols, offset,
6225 filename_ptr, functionname_ptr))
6226 return FALSE;
6227
6228 *line_ptr = 0;
6229 return TRUE;
6230 }
6231
6232 int
6233 _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
6234 {
6235 int ret;
6236
6237 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6238 if (! reloc)
6239 ret += get_program_header_size (abfd);
6240 return ret;
6241 }
6242
6243 bfd_boolean
6244 _bfd_elf_set_section_contents (bfd *abfd,
6245 sec_ptr section,
6246 const void *location,
6247 file_ptr offset,
6248 bfd_size_type count)
6249 {
6250 Elf_Internal_Shdr *hdr;
6251 bfd_signed_vma pos;
6252
6253 if (! abfd->output_has_begun
6254 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6255 return FALSE;
6256
6257 hdr = &elf_section_data (section)->this_hdr;
6258 pos = hdr->sh_offset + offset;
6259 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6260 || bfd_bwrite (location, count, abfd) != count)
6261 return FALSE;
6262
6263 return TRUE;
6264 }
6265
6266 void
6267 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6268 arelent *cache_ptr ATTRIBUTE_UNUSED,
6269 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
6270 {
6271 abort ();
6272 }
6273
6274 /* Try to convert a non-ELF reloc into an ELF one. */
6275
6276 bfd_boolean
6277 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
6278 {
6279 /* Check whether we really have an ELF howto. */
6280
6281 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6282 {
6283 bfd_reloc_code_real_type code;
6284 reloc_howto_type *howto;
6285
6286 /* Alien reloc: Try to determine its type to replace it with an
6287 equivalent ELF reloc. */
6288
6289 if (areloc->howto->pc_relative)
6290 {
6291 switch (areloc->howto->bitsize)
6292 {
6293 case 8:
6294 code = BFD_RELOC_8_PCREL;
6295 break;
6296 case 12:
6297 code = BFD_RELOC_12_PCREL;
6298 break;
6299 case 16:
6300 code = BFD_RELOC_16_PCREL;
6301 break;
6302 case 24:
6303 code = BFD_RELOC_24_PCREL;
6304 break;
6305 case 32:
6306 code = BFD_RELOC_32_PCREL;
6307 break;
6308 case 64:
6309 code = BFD_RELOC_64_PCREL;
6310 break;
6311 default:
6312 goto fail;
6313 }
6314
6315 howto = bfd_reloc_type_lookup (abfd, code);
6316
6317 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6318 {
6319 if (howto->pcrel_offset)
6320 areloc->addend += areloc->address;
6321 else
6322 areloc->addend -= areloc->address; /* addend is unsigned!! */
6323 }
6324 }
6325 else
6326 {
6327 switch (areloc->howto->bitsize)
6328 {
6329 case 8:
6330 code = BFD_RELOC_8;
6331 break;
6332 case 14:
6333 code = BFD_RELOC_14;
6334 break;
6335 case 16:
6336 code = BFD_RELOC_16;
6337 break;
6338 case 26:
6339 code = BFD_RELOC_26;
6340 break;
6341 case 32:
6342 code = BFD_RELOC_32;
6343 break;
6344 case 64:
6345 code = BFD_RELOC_64;
6346 break;
6347 default:
6348 goto fail;
6349 }
6350
6351 howto = bfd_reloc_type_lookup (abfd, code);
6352 }
6353
6354 if (howto)
6355 areloc->howto = howto;
6356 else
6357 goto fail;
6358 }
6359
6360 return TRUE;
6361
6362 fail:
6363 (*_bfd_error_handler)
6364 (_("%s: unsupported relocation type %s"),
6365 bfd_archive_filename (abfd), areloc->howto->name);
6366 bfd_set_error (bfd_error_bad_value);
6367 return FALSE;
6368 }
6369
6370 bfd_boolean
6371 _bfd_elf_close_and_cleanup (bfd *abfd)
6372 {
6373 if (bfd_get_format (abfd) == bfd_object)
6374 {
6375 if (elf_shstrtab (abfd) != NULL)
6376 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6377 }
6378
6379 return _bfd_generic_close_and_cleanup (abfd);
6380 }
6381
6382 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6383 in the relocation's offset. Thus we cannot allow any sort of sanity
6384 range-checking to interfere. There is nothing else to do in processing
6385 this reloc. */
6386
6387 bfd_reloc_status_type
6388 _bfd_elf_rel_vtable_reloc_fn
6389 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
6390 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
6391 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
6392 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
6393 {
6394 return bfd_reloc_ok;
6395 }
6396 \f
6397 /* Elf core file support. Much of this only works on native
6398 toolchains, since we rely on knowing the
6399 machine-dependent procfs structure in order to pick
6400 out details about the corefile. */
6401
6402 #ifdef HAVE_SYS_PROCFS_H
6403 # include <sys/procfs.h>
6404 #endif
6405
6406 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6407
6408 static int
6409 elfcore_make_pid (bfd *abfd)
6410 {
6411 return ((elf_tdata (abfd)->core_lwpid << 16)
6412 + (elf_tdata (abfd)->core_pid));
6413 }
6414
6415 /* If there isn't a section called NAME, make one, using
6416 data from SECT. Note, this function will generate a
6417 reference to NAME, so you shouldn't deallocate or
6418 overwrite it. */
6419
6420 static bfd_boolean
6421 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
6422 {
6423 asection *sect2;
6424
6425 if (bfd_get_section_by_name (abfd, name) != NULL)
6426 return TRUE;
6427
6428 sect2 = bfd_make_section (abfd, name);
6429 if (sect2 == NULL)
6430 return FALSE;
6431
6432 sect2->_raw_size = sect->_raw_size;
6433 sect2->filepos = sect->filepos;
6434 sect2->flags = sect->flags;
6435 sect2->alignment_power = sect->alignment_power;
6436 return TRUE;
6437 }
6438
6439 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6440 actually creates up to two pseudosections:
6441 - For the single-threaded case, a section named NAME, unless
6442 such a section already exists.
6443 - For the multi-threaded case, a section named "NAME/PID", where
6444 PID is elfcore_make_pid (abfd).
6445 Both pseudosections have identical contents. */
6446 bfd_boolean
6447 _bfd_elfcore_make_pseudosection (bfd *abfd,
6448 char *name,
6449 size_t size,
6450 ufile_ptr filepos)
6451 {
6452 char buf[100];
6453 char *threaded_name;
6454 size_t len;
6455 asection *sect;
6456
6457 /* Build the section name. */
6458
6459 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6460 len = strlen (buf) + 1;
6461 threaded_name = bfd_alloc (abfd, len);
6462 if (threaded_name == NULL)
6463 return FALSE;
6464 memcpy (threaded_name, buf, len);
6465
6466 sect = bfd_make_section_anyway (abfd, threaded_name);
6467 if (sect == NULL)
6468 return FALSE;
6469 sect->_raw_size = size;
6470 sect->filepos = filepos;
6471 sect->flags = SEC_HAS_CONTENTS;
6472 sect->alignment_power = 2;
6473
6474 return elfcore_maybe_make_sect (abfd, name, sect);
6475 }
6476
6477 /* prstatus_t exists on:
6478 solaris 2.5+
6479 linux 2.[01] + glibc
6480 unixware 4.2
6481 */
6482
6483 #if defined (HAVE_PRSTATUS_T)
6484
6485 static bfd_boolean
6486 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6487 {
6488 size_t raw_size;
6489 int offset;
6490
6491 if (note->descsz == sizeof (prstatus_t))
6492 {
6493 prstatus_t prstat;
6494
6495 raw_size = sizeof (prstat.pr_reg);
6496 offset = offsetof (prstatus_t, pr_reg);
6497 memcpy (&prstat, note->descdata, sizeof (prstat));
6498
6499 /* Do not overwrite the core signal if it
6500 has already been set by another thread. */
6501 if (elf_tdata (abfd)->core_signal == 0)
6502 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6503 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6504
6505 /* pr_who exists on:
6506 solaris 2.5+
6507 unixware 4.2
6508 pr_who doesn't exist on:
6509 linux 2.[01]
6510 */
6511 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6512 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6513 #endif
6514 }
6515 #if defined (HAVE_PRSTATUS32_T)
6516 else if (note->descsz == sizeof (prstatus32_t))
6517 {
6518 /* 64-bit host, 32-bit corefile */
6519 prstatus32_t prstat;
6520
6521 raw_size = sizeof (prstat.pr_reg);
6522 offset = offsetof (prstatus32_t, pr_reg);
6523 memcpy (&prstat, note->descdata, sizeof (prstat));
6524
6525 /* Do not overwrite the core signal if it
6526 has already been set by another thread. */
6527 if (elf_tdata (abfd)->core_signal == 0)
6528 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6529 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6530
6531 /* pr_who exists on:
6532 solaris 2.5+
6533 unixware 4.2
6534 pr_who doesn't exist on:
6535 linux 2.[01]
6536 */
6537 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6538 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6539 #endif
6540 }
6541 #endif /* HAVE_PRSTATUS32_T */
6542 else
6543 {
6544 /* Fail - we don't know how to handle any other
6545 note size (ie. data object type). */
6546 return TRUE;
6547 }
6548
6549 /* Make a ".reg/999" section and a ".reg" section. */
6550 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6551 raw_size, note->descpos + offset);
6552 }
6553 #endif /* defined (HAVE_PRSTATUS_T) */
6554
6555 /* Create a pseudosection containing the exact contents of NOTE. */
6556 static bfd_boolean
6557 elfcore_make_note_pseudosection (bfd *abfd,
6558 char *name,
6559 Elf_Internal_Note *note)
6560 {
6561 return _bfd_elfcore_make_pseudosection (abfd, name,
6562 note->descsz, note->descpos);
6563 }
6564
6565 /* There isn't a consistent prfpregset_t across platforms,
6566 but it doesn't matter, because we don't have to pick this
6567 data structure apart. */
6568
6569 static bfd_boolean
6570 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
6571 {
6572 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6573 }
6574
6575 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6576 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6577 literally. */
6578
6579 static bfd_boolean
6580 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
6581 {
6582 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6583 }
6584
6585 #if defined (HAVE_PRPSINFO_T)
6586 typedef prpsinfo_t elfcore_psinfo_t;
6587 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6588 typedef prpsinfo32_t elfcore_psinfo32_t;
6589 #endif
6590 #endif
6591
6592 #if defined (HAVE_PSINFO_T)
6593 typedef psinfo_t elfcore_psinfo_t;
6594 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6595 typedef psinfo32_t elfcore_psinfo32_t;
6596 #endif
6597 #endif
6598
6599 /* return a malloc'ed copy of a string at START which is at
6600 most MAX bytes long, possibly without a terminating '\0'.
6601 the copy will always have a terminating '\0'. */
6602
6603 char *
6604 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
6605 {
6606 char *dups;
6607 char *end = memchr (start, '\0', max);
6608 size_t len;
6609
6610 if (end == NULL)
6611 len = max;
6612 else
6613 len = end - start;
6614
6615 dups = bfd_alloc (abfd, len + 1);
6616 if (dups == NULL)
6617 return NULL;
6618
6619 memcpy (dups, start, len);
6620 dups[len] = '\0';
6621
6622 return dups;
6623 }
6624
6625 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6626 static bfd_boolean
6627 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6628 {
6629 if (note->descsz == sizeof (elfcore_psinfo_t))
6630 {
6631 elfcore_psinfo_t psinfo;
6632
6633 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6634
6635 elf_tdata (abfd)->core_program
6636 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6637 sizeof (psinfo.pr_fname));
6638
6639 elf_tdata (abfd)->core_command
6640 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6641 sizeof (psinfo.pr_psargs));
6642 }
6643 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6644 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6645 {
6646 /* 64-bit host, 32-bit corefile */
6647 elfcore_psinfo32_t psinfo;
6648
6649 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6650
6651 elf_tdata (abfd)->core_program
6652 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6653 sizeof (psinfo.pr_fname));
6654
6655 elf_tdata (abfd)->core_command
6656 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6657 sizeof (psinfo.pr_psargs));
6658 }
6659 #endif
6660
6661 else
6662 {
6663 /* Fail - we don't know how to handle any other
6664 note size (ie. data object type). */
6665 return TRUE;
6666 }
6667
6668 /* Note that for some reason, a spurious space is tacked
6669 onto the end of the args in some (at least one anyway)
6670 implementations, so strip it off if it exists. */
6671
6672 {
6673 char *command = elf_tdata (abfd)->core_command;
6674 int n = strlen (command);
6675
6676 if (0 < n && command[n - 1] == ' ')
6677 command[n - 1] = '\0';
6678 }
6679
6680 return TRUE;
6681 }
6682 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6683
6684 #if defined (HAVE_PSTATUS_T)
6685 static bfd_boolean
6686 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
6687 {
6688 if (note->descsz == sizeof (pstatus_t)
6689 #if defined (HAVE_PXSTATUS_T)
6690 || note->descsz == sizeof (pxstatus_t)
6691 #endif
6692 )
6693 {
6694 pstatus_t pstat;
6695
6696 memcpy (&pstat, note->descdata, sizeof (pstat));
6697
6698 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6699 }
6700 #if defined (HAVE_PSTATUS32_T)
6701 else if (note->descsz == sizeof (pstatus32_t))
6702 {
6703 /* 64-bit host, 32-bit corefile */
6704 pstatus32_t pstat;
6705
6706 memcpy (&pstat, note->descdata, sizeof (pstat));
6707
6708 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6709 }
6710 #endif
6711 /* Could grab some more details from the "representative"
6712 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6713 NT_LWPSTATUS note, presumably. */
6714
6715 return TRUE;
6716 }
6717 #endif /* defined (HAVE_PSTATUS_T) */
6718
6719 #if defined (HAVE_LWPSTATUS_T)
6720 static bfd_boolean
6721 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
6722 {
6723 lwpstatus_t lwpstat;
6724 char buf[100];
6725 char *name;
6726 size_t len;
6727 asection *sect;
6728
6729 if (note->descsz != sizeof (lwpstat)
6730 #if defined (HAVE_LWPXSTATUS_T)
6731 && note->descsz != sizeof (lwpxstatus_t)
6732 #endif
6733 )
6734 return TRUE;
6735
6736 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6737
6738 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6739 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6740
6741 /* Make a ".reg/999" section. */
6742
6743 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6744 len = strlen (buf) + 1;
6745 name = bfd_alloc (abfd, len);
6746 if (name == NULL)
6747 return FALSE;
6748 memcpy (name, buf, len);
6749
6750 sect = bfd_make_section_anyway (abfd, name);
6751 if (sect == NULL)
6752 return FALSE;
6753
6754 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6755 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6756 sect->filepos = note->descpos
6757 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6758 #endif
6759
6760 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6761 sect->_raw_size = sizeof (lwpstat.pr_reg);
6762 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6763 #endif
6764
6765 sect->flags = SEC_HAS_CONTENTS;
6766 sect->alignment_power = 2;
6767
6768 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6769 return FALSE;
6770
6771 /* Make a ".reg2/999" section */
6772
6773 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6774 len = strlen (buf) + 1;
6775 name = bfd_alloc (abfd, len);
6776 if (name == NULL)
6777 return FALSE;
6778 memcpy (name, buf, len);
6779
6780 sect = bfd_make_section_anyway (abfd, name);
6781 if (sect == NULL)
6782 return FALSE;
6783
6784 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6785 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6786 sect->filepos = note->descpos
6787 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6788 #endif
6789
6790 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6791 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6792 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6793 #endif
6794
6795 sect->flags = SEC_HAS_CONTENTS;
6796 sect->alignment_power = 2;
6797
6798 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6799 }
6800 #endif /* defined (HAVE_LWPSTATUS_T) */
6801
6802 #if defined (HAVE_WIN32_PSTATUS_T)
6803 static bfd_boolean
6804 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
6805 {
6806 char buf[30];
6807 char *name;
6808 size_t len;
6809 asection *sect;
6810 win32_pstatus_t pstatus;
6811
6812 if (note->descsz < sizeof (pstatus))
6813 return TRUE;
6814
6815 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6816
6817 switch (pstatus.data_type)
6818 {
6819 case NOTE_INFO_PROCESS:
6820 /* FIXME: need to add ->core_command. */
6821 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6822 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6823 break;
6824
6825 case NOTE_INFO_THREAD:
6826 /* Make a ".reg/999" section. */
6827 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6828
6829 len = strlen (buf) + 1;
6830 name = bfd_alloc (abfd, len);
6831 if (name == NULL)
6832 return FALSE;
6833
6834 memcpy (name, buf, len);
6835
6836 sect = bfd_make_section_anyway (abfd, name);
6837 if (sect == NULL)
6838 return FALSE;
6839
6840 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6841 sect->filepos = (note->descpos
6842 + offsetof (struct win32_pstatus,
6843 data.thread_info.thread_context));
6844 sect->flags = SEC_HAS_CONTENTS;
6845 sect->alignment_power = 2;
6846
6847 if (pstatus.data.thread_info.is_active_thread)
6848 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6849 return FALSE;
6850 break;
6851
6852 case NOTE_INFO_MODULE:
6853 /* Make a ".module/xxxxxxxx" section. */
6854 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6855
6856 len = strlen (buf) + 1;
6857 name = bfd_alloc (abfd, len);
6858 if (name == NULL)
6859 return FALSE;
6860
6861 memcpy (name, buf, len);
6862
6863 sect = bfd_make_section_anyway (abfd, name);
6864
6865 if (sect == NULL)
6866 return FALSE;
6867
6868 sect->_raw_size = note->descsz;
6869 sect->filepos = note->descpos;
6870 sect->flags = SEC_HAS_CONTENTS;
6871 sect->alignment_power = 2;
6872 break;
6873
6874 default:
6875 return TRUE;
6876 }
6877
6878 return TRUE;
6879 }
6880 #endif /* HAVE_WIN32_PSTATUS_T */
6881
6882 static bfd_boolean
6883 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
6884 {
6885 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6886
6887 switch (note->type)
6888 {
6889 default:
6890 return TRUE;
6891
6892 case NT_PRSTATUS:
6893 if (bed->elf_backend_grok_prstatus)
6894 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6895 return TRUE;
6896 #if defined (HAVE_PRSTATUS_T)
6897 return elfcore_grok_prstatus (abfd, note);
6898 #else
6899 return TRUE;
6900 #endif
6901
6902 #if defined (HAVE_PSTATUS_T)
6903 case NT_PSTATUS:
6904 return elfcore_grok_pstatus (abfd, note);
6905 #endif
6906
6907 #if defined (HAVE_LWPSTATUS_T)
6908 case NT_LWPSTATUS:
6909 return elfcore_grok_lwpstatus (abfd, note);
6910 #endif
6911
6912 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6913 return elfcore_grok_prfpreg (abfd, note);
6914
6915 #if defined (HAVE_WIN32_PSTATUS_T)
6916 case NT_WIN32PSTATUS:
6917 return elfcore_grok_win32pstatus (abfd, note);
6918 #endif
6919
6920 case NT_PRXFPREG: /* Linux SSE extension */
6921 if (note->namesz == 6
6922 && strcmp (note->namedata, "LINUX") == 0)
6923 return elfcore_grok_prxfpreg (abfd, note);
6924 else
6925 return TRUE;
6926
6927 case NT_PRPSINFO:
6928 case NT_PSINFO:
6929 if (bed->elf_backend_grok_psinfo)
6930 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6931 return TRUE;
6932 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6933 return elfcore_grok_psinfo (abfd, note);
6934 #else
6935 return TRUE;
6936 #endif
6937
6938 case NT_AUXV:
6939 {
6940 asection *sect = bfd_make_section_anyway (abfd, ".auxv");
6941
6942 if (sect == NULL)
6943 return FALSE;
6944 sect->_raw_size = note->descsz;
6945 sect->filepos = note->descpos;
6946 sect->flags = SEC_HAS_CONTENTS;
6947 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
6948
6949 return TRUE;
6950 }
6951 }
6952 }
6953
6954 static bfd_boolean
6955 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
6956 {
6957 char *cp;
6958
6959 cp = strchr (note->namedata, '@');
6960 if (cp != NULL)
6961 {
6962 *lwpidp = atoi(cp + 1);
6963 return TRUE;
6964 }
6965 return FALSE;
6966 }
6967
6968 static bfd_boolean
6969 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
6970 {
6971
6972 /* Signal number at offset 0x08. */
6973 elf_tdata (abfd)->core_signal
6974 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6975
6976 /* Process ID at offset 0x50. */
6977 elf_tdata (abfd)->core_pid
6978 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6979
6980 /* Command name at 0x7c (max 32 bytes, including nul). */
6981 elf_tdata (abfd)->core_command
6982 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6983
6984 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
6985 note);
6986 }
6987
6988 static bfd_boolean
6989 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
6990 {
6991 int lwp;
6992
6993 if (elfcore_netbsd_get_lwpid (note, &lwp))
6994 elf_tdata (abfd)->core_lwpid = lwp;
6995
6996 if (note->type == NT_NETBSDCORE_PROCINFO)
6997 {
6998 /* NetBSD-specific core "procinfo". Note that we expect to
6999 find this note before any of the others, which is fine,
7000 since the kernel writes this note out first when it
7001 creates a core file. */
7002
7003 return elfcore_grok_netbsd_procinfo (abfd, note);
7004 }
7005
7006 /* As of Jan 2002 there are no other machine-independent notes
7007 defined for NetBSD core files. If the note type is less
7008 than the start of the machine-dependent note types, we don't
7009 understand it. */
7010
7011 if (note->type < NT_NETBSDCORE_FIRSTMACH)
7012 return TRUE;
7013
7014
7015 switch (bfd_get_arch (abfd))
7016 {
7017 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7018 PT_GETFPREGS == mach+2. */
7019
7020 case bfd_arch_alpha:
7021 case bfd_arch_sparc:
7022 switch (note->type)
7023 {
7024 case NT_NETBSDCORE_FIRSTMACH+0:
7025 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7026
7027 case NT_NETBSDCORE_FIRSTMACH+2:
7028 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7029
7030 default:
7031 return TRUE;
7032 }
7033
7034 /* On all other arch's, PT_GETREGS == mach+1 and
7035 PT_GETFPREGS == mach+3. */
7036
7037 default:
7038 switch (note->type)
7039 {
7040 case NT_NETBSDCORE_FIRSTMACH+1:
7041 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7042
7043 case NT_NETBSDCORE_FIRSTMACH+3:
7044 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7045
7046 default:
7047 return TRUE;
7048 }
7049 }
7050 /* NOTREACHED */
7051 }
7052
7053 static bfd_boolean
7054 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
7055 {
7056 void *ddata = note->descdata;
7057 char buf[100];
7058 char *name;
7059 asection *sect;
7060 short sig;
7061 unsigned flags;
7062
7063 /* nto_procfs_status 'pid' field is at offset 0. */
7064 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7065
7066 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7067 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7068
7069 /* nto_procfs_status 'flags' field is at offset 8. */
7070 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
7071
7072 /* nto_procfs_status 'what' field is at offset 14. */
7073 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7074 {
7075 elf_tdata (abfd)->core_signal = sig;
7076 elf_tdata (abfd)->core_lwpid = *tid;
7077 }
7078
7079 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7080 do not come from signals so we make sure we set the current
7081 thread just in case. */
7082 if (flags & 0x00000080)
7083 elf_tdata (abfd)->core_lwpid = *tid;
7084
7085 /* Make a ".qnx_core_status/%d" section. */
7086 sprintf (buf, ".qnx_core_status/%d", *tid);
7087
7088 name = bfd_alloc (abfd, strlen (buf) + 1);
7089 if (name == NULL)
7090 return FALSE;
7091 strcpy (name, buf);
7092
7093 sect = bfd_make_section_anyway (abfd, name);
7094 if (sect == NULL)
7095 return FALSE;
7096
7097 sect->_raw_size = note->descsz;
7098 sect->filepos = note->descpos;
7099 sect->flags = SEC_HAS_CONTENTS;
7100 sect->alignment_power = 2;
7101
7102 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7103 }
7104
7105 static bfd_boolean
7106 elfcore_grok_nto_gregs (bfd *abfd, Elf_Internal_Note *note, pid_t tid)
7107 {
7108 char buf[100];
7109 char *name;
7110 asection *sect;
7111
7112 /* Make a ".reg/%d" section. */
7113 sprintf (buf, ".reg/%d", tid);
7114
7115 name = bfd_alloc (abfd, strlen (buf) + 1);
7116 if (name == NULL)
7117 return FALSE;
7118 strcpy (name, buf);
7119
7120 sect = bfd_make_section_anyway (abfd, name);
7121 if (sect == NULL)
7122 return FALSE;
7123
7124 sect->_raw_size = note->descsz;
7125 sect->filepos = note->descpos;
7126 sect->flags = SEC_HAS_CONTENTS;
7127 sect->alignment_power = 2;
7128
7129 /* This is the current thread. */
7130 if (elf_tdata (abfd)->core_lwpid == tid)
7131 return elfcore_maybe_make_sect (abfd, ".reg", sect);
7132
7133 return TRUE;
7134 }
7135
7136 #define BFD_QNT_CORE_INFO 7
7137 #define BFD_QNT_CORE_STATUS 8
7138 #define BFD_QNT_CORE_GREG 9
7139 #define BFD_QNT_CORE_FPREG 10
7140
7141 static bfd_boolean
7142 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7143 {
7144 /* Every GREG section has a STATUS section before it. Store the
7145 tid from the previous call to pass down to the next gregs
7146 function. */
7147 static pid_t tid = 1;
7148
7149 switch (note->type)
7150 {
7151 case BFD_QNT_CORE_INFO: return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7152 case BFD_QNT_CORE_STATUS: return elfcore_grok_nto_status (abfd, note, &tid);
7153 case BFD_QNT_CORE_GREG: return elfcore_grok_nto_gregs (abfd, note, tid);
7154 case BFD_QNT_CORE_FPREG: return elfcore_grok_prfpreg (abfd, note);
7155 default: return TRUE;
7156 }
7157 }
7158
7159 /* Function: elfcore_write_note
7160
7161 Inputs:
7162 buffer to hold note
7163 name of note
7164 type of note
7165 data for note
7166 size of data for note
7167
7168 Return:
7169 End of buffer containing note. */
7170
7171 char *
7172 elfcore_write_note (bfd *abfd,
7173 char *buf,
7174 int *bufsiz,
7175 const char *name,
7176 int type,
7177 const void *input,
7178 int size)
7179 {
7180 Elf_External_Note *xnp;
7181 size_t namesz;
7182 size_t pad;
7183 size_t newspace;
7184 char *p, *dest;
7185
7186 namesz = 0;
7187 pad = 0;
7188 if (name != NULL)
7189 {
7190 const struct elf_backend_data *bed;
7191
7192 namesz = strlen (name) + 1;
7193 bed = get_elf_backend_data (abfd);
7194 pad = -namesz & ((1 << bed->s->log_file_align) - 1);
7195 }
7196
7197 newspace = 12 + namesz + pad + size;
7198
7199 p = realloc (buf, *bufsiz + newspace);
7200 dest = p + *bufsiz;
7201 *bufsiz += newspace;
7202 xnp = (Elf_External_Note *) dest;
7203 H_PUT_32 (abfd, namesz, xnp->namesz);
7204 H_PUT_32 (abfd, size, xnp->descsz);
7205 H_PUT_32 (abfd, type, xnp->type);
7206 dest = xnp->name;
7207 if (name != NULL)
7208 {
7209 memcpy (dest, name, namesz);
7210 dest += namesz;
7211 while (pad != 0)
7212 {
7213 *dest++ = '\0';
7214 --pad;
7215 }
7216 }
7217 memcpy (dest, input, size);
7218 return p;
7219 }
7220
7221 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7222 char *
7223 elfcore_write_prpsinfo (bfd *abfd,
7224 char *buf,
7225 int *bufsiz,
7226 const char *fname,
7227 const char *psargs)
7228 {
7229 int note_type;
7230 char *note_name = "CORE";
7231
7232 #if defined (HAVE_PSINFO_T)
7233 psinfo_t data;
7234 note_type = NT_PSINFO;
7235 #else
7236 prpsinfo_t data;
7237 note_type = NT_PRPSINFO;
7238 #endif
7239
7240 memset (&data, 0, sizeof (data));
7241 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7242 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7243 return elfcore_write_note (abfd, buf, bufsiz,
7244 note_name, note_type, &data, sizeof (data));
7245 }
7246 #endif /* PSINFO_T or PRPSINFO_T */
7247
7248 #if defined (HAVE_PRSTATUS_T)
7249 char *
7250 elfcore_write_prstatus (bfd *abfd,
7251 char *buf,
7252 int *bufsiz,
7253 long pid,
7254 int cursig,
7255 const void *gregs)
7256 {
7257 prstatus_t prstat;
7258 char *note_name = "CORE";
7259
7260 memset (&prstat, 0, sizeof (prstat));
7261 prstat.pr_pid = pid;
7262 prstat.pr_cursig = cursig;
7263 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7264 return elfcore_write_note (abfd, buf, bufsiz,
7265 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7266 }
7267 #endif /* HAVE_PRSTATUS_T */
7268
7269 #if defined (HAVE_LWPSTATUS_T)
7270 char *
7271 elfcore_write_lwpstatus (bfd *abfd,
7272 char *buf,
7273 int *bufsiz,
7274 long pid,
7275 int cursig,
7276 const void *gregs)
7277 {
7278 lwpstatus_t lwpstat;
7279 char *note_name = "CORE";
7280
7281 memset (&lwpstat, 0, sizeof (lwpstat));
7282 lwpstat.pr_lwpid = pid >> 16;
7283 lwpstat.pr_cursig = cursig;
7284 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7285 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7286 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7287 #if !defined(gregs)
7288 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7289 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7290 #else
7291 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7292 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7293 #endif
7294 #endif
7295 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7296 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7297 }
7298 #endif /* HAVE_LWPSTATUS_T */
7299
7300 #if defined (HAVE_PSTATUS_T)
7301 char *
7302 elfcore_write_pstatus (bfd *abfd,
7303 char *buf,
7304 int *bufsiz,
7305 long pid,
7306 int cursig,
7307 const void *gregs)
7308 {
7309 pstatus_t pstat;
7310 char *note_name = "CORE";
7311
7312 memset (&pstat, 0, sizeof (pstat));
7313 pstat.pr_pid = pid & 0xffff;
7314 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7315 NT_PSTATUS, &pstat, sizeof (pstat));
7316 return buf;
7317 }
7318 #endif /* HAVE_PSTATUS_T */
7319
7320 char *
7321 elfcore_write_prfpreg (bfd *abfd,
7322 char *buf,
7323 int *bufsiz,
7324 const void *fpregs,
7325 int size)
7326 {
7327 char *note_name = "CORE";
7328 return elfcore_write_note (abfd, buf, bufsiz,
7329 note_name, NT_FPREGSET, fpregs, size);
7330 }
7331
7332 char *
7333 elfcore_write_prxfpreg (bfd *abfd,
7334 char *buf,
7335 int *bufsiz,
7336 const void *xfpregs,
7337 int size)
7338 {
7339 char *note_name = "LINUX";
7340 return elfcore_write_note (abfd, buf, bufsiz,
7341 note_name, NT_PRXFPREG, xfpregs, size);
7342 }
7343
7344 static bfd_boolean
7345 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
7346 {
7347 char *buf;
7348 char *p;
7349
7350 if (size <= 0)
7351 return TRUE;
7352
7353 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7354 return FALSE;
7355
7356 buf = bfd_malloc (size);
7357 if (buf == NULL)
7358 return FALSE;
7359
7360 if (bfd_bread (buf, size, abfd) != size)
7361 {
7362 error:
7363 free (buf);
7364 return FALSE;
7365 }
7366
7367 p = buf;
7368 while (p < buf + size)
7369 {
7370 /* FIXME: bad alignment assumption. */
7371 Elf_External_Note *xnp = (Elf_External_Note *) p;
7372 Elf_Internal_Note in;
7373
7374 in.type = H_GET_32 (abfd, xnp->type);
7375
7376 in.namesz = H_GET_32 (abfd, xnp->namesz);
7377 in.namedata = xnp->name;
7378
7379 in.descsz = H_GET_32 (abfd, xnp->descsz);
7380 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7381 in.descpos = offset + (in.descdata - buf);
7382
7383 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7384 {
7385 if (! elfcore_grok_netbsd_note (abfd, &in))
7386 goto error;
7387 }
7388 else if (strncmp (in.namedata, "QNX", 3) == 0)
7389 {
7390 if (! elfcore_grok_nto_note (abfd, &in))
7391 goto error;
7392 }
7393 else
7394 {
7395 if (! elfcore_grok_note (abfd, &in))
7396 goto error;
7397 }
7398
7399 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7400 }
7401
7402 free (buf);
7403 return TRUE;
7404 }
7405 \f
7406 /* Providing external access to the ELF program header table. */
7407
7408 /* Return an upper bound on the number of bytes required to store a
7409 copy of ABFD's program header table entries. Return -1 if an error
7410 occurs; bfd_get_error will return an appropriate code. */
7411
7412 long
7413 bfd_get_elf_phdr_upper_bound (bfd *abfd)
7414 {
7415 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7416 {
7417 bfd_set_error (bfd_error_wrong_format);
7418 return -1;
7419 }
7420
7421 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7422 }
7423
7424 /* Copy ABFD's program header table entries to *PHDRS. The entries
7425 will be stored as an array of Elf_Internal_Phdr structures, as
7426 defined in include/elf/internal.h. To find out how large the
7427 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7428
7429 Return the number of program header table entries read, or -1 if an
7430 error occurs; bfd_get_error will return an appropriate code. */
7431
7432 int
7433 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
7434 {
7435 int num_phdrs;
7436
7437 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7438 {
7439 bfd_set_error (bfd_error_wrong_format);
7440 return -1;
7441 }
7442
7443 num_phdrs = elf_elfheader (abfd)->e_phnum;
7444 memcpy (phdrs, elf_tdata (abfd)->phdr,
7445 num_phdrs * sizeof (Elf_Internal_Phdr));
7446
7447 return num_phdrs;
7448 }
7449
7450 void
7451 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
7452 {
7453 #ifdef BFD64
7454 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7455
7456 i_ehdrp = elf_elfheader (abfd);
7457 if (i_ehdrp == NULL)
7458 sprintf_vma (buf, value);
7459 else
7460 {
7461 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7462 {
7463 #if BFD_HOST_64BIT_LONG
7464 sprintf (buf, "%016lx", value);
7465 #else
7466 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7467 _bfd_int64_low (value));
7468 #endif
7469 }
7470 else
7471 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7472 }
7473 #else
7474 sprintf_vma (buf, value);
7475 #endif
7476 }
7477
7478 void
7479 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
7480 {
7481 #ifdef BFD64
7482 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7483
7484 i_ehdrp = elf_elfheader (abfd);
7485 if (i_ehdrp == NULL)
7486 fprintf_vma ((FILE *) stream, value);
7487 else
7488 {
7489 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7490 {
7491 #if BFD_HOST_64BIT_LONG
7492 fprintf ((FILE *) stream, "%016lx", value);
7493 #else
7494 fprintf ((FILE *) stream, "%08lx%08lx",
7495 _bfd_int64_high (value), _bfd_int64_low (value));
7496 #endif
7497 }
7498 else
7499 fprintf ((FILE *) stream, "%08lx",
7500 (unsigned long) (value & 0xffffffff));
7501 }
7502 #else
7503 fprintf_vma ((FILE *) stream, value);
7504 #endif
7505 }
7506
7507 enum elf_reloc_type_class
7508 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
7509 {
7510 return reloc_class_normal;
7511 }
7512
7513 /* For RELA architectures, return the relocation value for a
7514 relocation against a local symbol. */
7515
7516 bfd_vma
7517 _bfd_elf_rela_local_sym (bfd *abfd,
7518 Elf_Internal_Sym *sym,
7519 asection **psec,
7520 Elf_Internal_Rela *rel)
7521 {
7522 asection *sec = *psec;
7523 bfd_vma relocation;
7524
7525 relocation = (sec->output_section->vma
7526 + sec->output_offset
7527 + sym->st_value);
7528 if ((sec->flags & SEC_MERGE)
7529 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7530 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7531 {
7532 rel->r_addend =
7533 _bfd_merged_section_offset (abfd, psec,
7534 elf_section_data (sec)->sec_info,
7535 sym->st_value + rel->r_addend,
7536 0);
7537 sec = *psec;
7538 rel->r_addend -= relocation;
7539 rel->r_addend += sec->output_section->vma + sec->output_offset;
7540 }
7541 return relocation;
7542 }
7543
7544 bfd_vma
7545 _bfd_elf_rel_local_sym (bfd *abfd,
7546 Elf_Internal_Sym *sym,
7547 asection **psec,
7548 bfd_vma addend)
7549 {
7550 asection *sec = *psec;
7551
7552 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7553 return sym->st_value + addend;
7554
7555 return _bfd_merged_section_offset (abfd, psec,
7556 elf_section_data (sec)->sec_info,
7557 sym->st_value + addend, 0);
7558 }
7559
7560 bfd_vma
7561 _bfd_elf_section_offset (bfd *abfd,
7562 struct bfd_link_info *info,
7563 asection *sec,
7564 bfd_vma offset)
7565 {
7566 struct bfd_elf_section_data *sec_data;
7567
7568 sec_data = elf_section_data (sec);
7569 switch (sec->sec_info_type)
7570 {
7571 case ELF_INFO_TYPE_STABS:
7572 return _bfd_stab_section_offset (abfd,
7573 &elf_hash_table (info)->merge_info,
7574 sec, &sec_data->sec_info, offset);
7575 case ELF_INFO_TYPE_EH_FRAME:
7576 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7577 default:
7578 return offset;
7579 }
7580 }
7581 \f
7582 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7583 reconstruct an ELF file by reading the segments out of remote memory
7584 based on the ELF file header at EHDR_VMA and the ELF program headers it
7585 points to. If not null, *LOADBASEP is filled in with the difference
7586 between the VMAs from which the segments were read, and the VMAs the
7587 file headers (and hence BFD's idea of each section's VMA) put them at.
7588
7589 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7590 remote memory at target address VMA into the local buffer at MYADDR; it
7591 should return zero on success or an `errno' code on failure. TEMPL must
7592 be a BFD for an ELF target with the word size and byte order found in
7593 the remote memory. */
7594
7595 bfd *
7596 bfd_elf_bfd_from_remote_memory
7597 (bfd *templ,
7598 bfd_vma ehdr_vma,
7599 bfd_vma *loadbasep,
7600 int (*target_read_memory) (bfd_vma, char *, int))
7601 {
7602 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7603 (templ, ehdr_vma, loadbasep, target_read_memory);
7604 }
7605 \f
7606 long
7607 _bfd_elf_get_synthetic_symtab (bfd *abfd, asymbol **dynsyms, asymbol **ret)
7608 {
7609 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7610 asection *relplt;
7611 asymbol *s;
7612 const char *relplt_name;
7613 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7614 arelent *p;
7615 long count, i, n;
7616 size_t size;
7617 Elf_Internal_Shdr *hdr;
7618 char *names;
7619 asection *plt;
7620
7621 *ret = NULL;
7622 if (!bed->plt_sym_val)
7623 return 0;
7624
7625 relplt_name = bed->relplt_name;
7626 if (relplt_name == NULL)
7627 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
7628 relplt = bfd_get_section_by_name (abfd, relplt_name);
7629 if (relplt == NULL)
7630 return 0;
7631
7632 hdr = &elf_section_data (relplt)->this_hdr;
7633 if (hdr->sh_link != elf_dynsymtab (abfd)
7634 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
7635 return 0;
7636
7637 plt = bfd_get_section_by_name (abfd, ".plt");
7638 if (plt == NULL)
7639 return 0;
7640
7641 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7642 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
7643 return -1;
7644
7645 count = relplt->_raw_size / hdr->sh_entsize;
7646 size = count * sizeof (asymbol);
7647 p = relplt->relocation;
7648 for (i = 0; i < count; i++, s++, p++)
7649 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
7650
7651 s = *ret = bfd_malloc (size);
7652 if (s == NULL)
7653 return -1;
7654
7655 names = (char *) (s + count);
7656 p = relplt->relocation;
7657 n = 0;
7658 for (i = 0; i < count; i++, s++, p++)
7659 {
7660 size_t len;
7661 bfd_vma addr;
7662
7663 addr = bed->plt_sym_val (i, plt, p);
7664 if (addr == (bfd_vma) -1)
7665 continue;
7666
7667 *s = **p->sym_ptr_ptr;
7668 s->section = plt;
7669 s->value = addr - plt->vma;
7670 s->name = names;
7671 len = strlen ((*p->sym_ptr_ptr)->name);
7672 memcpy (names, (*p->sym_ptr_ptr)->name, len);
7673 names += len;
7674 memcpy (names, "@plt", sizeof ("@plt"));
7675 names += sizeof ("@plt");
7676 ++n;
7677 }
7678
7679 return n;
7680 }
This page took 0.205738 seconds and 4 git commands to generate.