bfd/
[binutils.git] / bfd / elf.c
blobae3af35ee2cea7bcdcdd1e6aa18c7439fb939ab2
1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
11 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
25 SECTION
26 ELF backends
28 BFD support for ELF formats is being worked on.
29 Currently, the best supported back ends are for sparc and i386
30 (running svr4 or Solaris 2).
32 Documentation of the internals of the support code still needs
33 to be written. The code is changing quickly enough that we
34 haven't bothered yet. */
36 /* For sparc64-cross-sparc32. */
37 #define _SYSCALL32
38 #include "sysdep.h"
39 #include "bfd.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44 #include "libiberty.h"
45 #include "safe-ctype.h"
47 static int elf_sort_sections (const void *, const void *);
48 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
49 static bfd_boolean prep_headers (bfd *);
50 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
51 static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ;
53 /* Swap version information in and out. The version information is
54 currently size independent. If that ever changes, this code will
55 need to move into elfcode.h. */
57 /* Swap in a Verdef structure. */
59 void
60 _bfd_elf_swap_verdef_in (bfd *abfd,
61 const Elf_External_Verdef *src,
62 Elf_Internal_Verdef *dst)
64 dst->vd_version = H_GET_16 (abfd, src->vd_version);
65 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
66 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
67 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
68 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
69 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
70 dst->vd_next = H_GET_32 (abfd, src->vd_next);
73 /* Swap out a Verdef structure. */
75 void
76 _bfd_elf_swap_verdef_out (bfd *abfd,
77 const Elf_Internal_Verdef *src,
78 Elf_External_Verdef *dst)
80 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
81 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
82 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
83 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
84 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
85 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
86 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
89 /* Swap in a Verdaux structure. */
91 void
92 _bfd_elf_swap_verdaux_in (bfd *abfd,
93 const Elf_External_Verdaux *src,
94 Elf_Internal_Verdaux *dst)
96 dst->vda_name = H_GET_32 (abfd, src->vda_name);
97 dst->vda_next = H_GET_32 (abfd, src->vda_next);
100 /* Swap out a Verdaux structure. */
102 void
103 _bfd_elf_swap_verdaux_out (bfd *abfd,
104 const Elf_Internal_Verdaux *src,
105 Elf_External_Verdaux *dst)
107 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
108 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
111 /* Swap in a Verneed structure. */
113 void
114 _bfd_elf_swap_verneed_in (bfd *abfd,
115 const Elf_External_Verneed *src,
116 Elf_Internal_Verneed *dst)
118 dst->vn_version = H_GET_16 (abfd, src->vn_version);
119 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
120 dst->vn_file = H_GET_32 (abfd, src->vn_file);
121 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
122 dst->vn_next = H_GET_32 (abfd, src->vn_next);
125 /* Swap out a Verneed structure. */
127 void
128 _bfd_elf_swap_verneed_out (bfd *abfd,
129 const Elf_Internal_Verneed *src,
130 Elf_External_Verneed *dst)
132 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
133 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
134 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
135 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
136 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
139 /* Swap in a Vernaux structure. */
141 void
142 _bfd_elf_swap_vernaux_in (bfd *abfd,
143 const Elf_External_Vernaux *src,
144 Elf_Internal_Vernaux *dst)
146 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
147 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
148 dst->vna_other = H_GET_16 (abfd, src->vna_other);
149 dst->vna_name = H_GET_32 (abfd, src->vna_name);
150 dst->vna_next = H_GET_32 (abfd, src->vna_next);
153 /* Swap out a Vernaux structure. */
155 void
156 _bfd_elf_swap_vernaux_out (bfd *abfd,
157 const Elf_Internal_Vernaux *src,
158 Elf_External_Vernaux *dst)
160 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
161 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
162 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
163 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
164 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
167 /* Swap in a Versym structure. */
169 void
170 _bfd_elf_swap_versym_in (bfd *abfd,
171 const Elf_External_Versym *src,
172 Elf_Internal_Versym *dst)
174 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
177 /* Swap out a Versym structure. */
179 void
180 _bfd_elf_swap_versym_out (bfd *abfd,
181 const Elf_Internal_Versym *src,
182 Elf_External_Versym *dst)
184 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
187 /* Standard ELF hash function. Do not change this function; you will
188 cause invalid hash tables to be generated. */
190 unsigned long
191 bfd_elf_hash (const char *namearg)
193 const unsigned char *name = (const unsigned char *) namearg;
194 unsigned long h = 0;
195 unsigned long g;
196 int ch;
198 while ((ch = *name++) != '\0')
200 h = (h << 4) + ch;
201 if ((g = (h & 0xf0000000)) != 0)
203 h ^= g >> 24;
204 /* The ELF ABI says `h &= ~g', but this is equivalent in
205 this case and on some machines one insn instead of two. */
206 h ^= g;
209 return h & 0xffffffff;
212 /* DT_GNU_HASH hash function. Do not change this function; you will
213 cause invalid hash tables to be generated. */
215 unsigned long
216 bfd_elf_gnu_hash (const char *namearg)
218 const unsigned char *name = (const unsigned char *) namearg;
219 unsigned long h = 5381;
220 unsigned char ch;
222 while ((ch = *name++) != '\0')
223 h = (h << 5) + h + ch;
224 return h & 0xffffffff;
227 bfd_boolean
228 bfd_elf_mkobject (bfd *abfd)
230 if (abfd->tdata.any == NULL)
232 abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
233 if (abfd->tdata.any == NULL)
234 return FALSE;
237 elf_tdata (abfd)->program_header_size = (bfd_size_type) -1;
239 return TRUE;
242 bfd_boolean
243 bfd_elf_mkcorefile (bfd *abfd)
245 /* I think this can be done just like an object file. */
246 return bfd_elf_mkobject (abfd);
249 char *
250 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252 Elf_Internal_Shdr **i_shdrp;
253 bfd_byte *shstrtab = NULL;
254 file_ptr offset;
255 bfd_size_type shstrtabsize;
257 i_shdrp = elf_elfsections (abfd);
258 if (i_shdrp == 0
259 || shindex >= elf_numsections (abfd)
260 || i_shdrp[shindex] == 0)
261 return NULL;
263 shstrtab = i_shdrp[shindex]->contents;
264 if (shstrtab == NULL)
266 /* No cached one, attempt to read, and cache what we read. */
267 offset = i_shdrp[shindex]->sh_offset;
268 shstrtabsize = i_shdrp[shindex]->sh_size;
270 /* Allocate and clear an extra byte at the end, to prevent crashes
271 in case the string table is not terminated. */
272 if (shstrtabsize + 1 == 0
273 || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL
274 || bfd_seek (abfd, offset, SEEK_SET) != 0)
275 shstrtab = NULL;
276 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
278 if (bfd_get_error () != bfd_error_system_call)
279 bfd_set_error (bfd_error_file_truncated);
280 shstrtab = NULL;
282 else
283 shstrtab[shstrtabsize] = '\0';
284 i_shdrp[shindex]->contents = shstrtab;
286 return (char *) shstrtab;
289 char *
290 bfd_elf_string_from_elf_section (bfd *abfd,
291 unsigned int shindex,
292 unsigned int strindex)
294 Elf_Internal_Shdr *hdr;
296 if (strindex == 0)
297 return "";
299 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
300 return NULL;
302 hdr = elf_elfsections (abfd)[shindex];
304 if (hdr->contents == NULL
305 && bfd_elf_get_str_section (abfd, shindex) == NULL)
306 return NULL;
308 if (strindex >= hdr->sh_size)
310 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
311 (*_bfd_error_handler)
312 (_("%B: invalid string offset %u >= %lu for section `%s'"),
313 abfd, strindex, (unsigned long) hdr->sh_size,
314 (shindex == shstrndx && strindex == hdr->sh_name
315 ? ".shstrtab"
316 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
317 return "";
320 return ((char *) hdr->contents) + strindex;
323 /* Read and convert symbols to internal format.
324 SYMCOUNT specifies the number of symbols to read, starting from
325 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
326 are non-NULL, they are used to store the internal symbols, external
327 symbols, and symbol section index extensions, respectively. */
329 Elf_Internal_Sym *
330 bfd_elf_get_elf_syms (bfd *ibfd,
331 Elf_Internal_Shdr *symtab_hdr,
332 size_t symcount,
333 size_t symoffset,
334 Elf_Internal_Sym *intsym_buf,
335 void *extsym_buf,
336 Elf_External_Sym_Shndx *extshndx_buf)
338 Elf_Internal_Shdr *shndx_hdr;
339 void *alloc_ext;
340 const bfd_byte *esym;
341 Elf_External_Sym_Shndx *alloc_extshndx;
342 Elf_External_Sym_Shndx *shndx;
343 Elf_Internal_Sym *isym;
344 Elf_Internal_Sym *isymend;
345 const struct elf_backend_data *bed;
346 size_t extsym_size;
347 bfd_size_type amt;
348 file_ptr pos;
350 if (symcount == 0)
351 return intsym_buf;
353 /* Normal syms might have section extension entries. */
354 shndx_hdr = NULL;
355 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
356 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
358 /* Read the symbols. */
359 alloc_ext = NULL;
360 alloc_extshndx = NULL;
361 bed = get_elf_backend_data (ibfd);
362 extsym_size = bed->s->sizeof_sym;
363 amt = symcount * extsym_size;
364 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
365 if (extsym_buf == NULL)
367 alloc_ext = bfd_malloc2 (symcount, extsym_size);
368 extsym_buf = alloc_ext;
370 if (extsym_buf == NULL
371 || bfd_seek (ibfd, pos, SEEK_SET) != 0
372 || bfd_bread (extsym_buf, amt, ibfd) != amt)
374 intsym_buf = NULL;
375 goto out;
378 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
379 extshndx_buf = NULL;
380 else
382 amt = symcount * sizeof (Elf_External_Sym_Shndx);
383 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
384 if (extshndx_buf == NULL)
386 alloc_extshndx = bfd_malloc2 (symcount,
387 sizeof (Elf_External_Sym_Shndx));
388 extshndx_buf = alloc_extshndx;
390 if (extshndx_buf == NULL
391 || bfd_seek (ibfd, pos, SEEK_SET) != 0
392 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
394 intsym_buf = NULL;
395 goto out;
399 if (intsym_buf == NULL)
401 intsym_buf = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
402 if (intsym_buf == NULL)
403 goto out;
406 /* Convert the symbols to internal form. */
407 isymend = intsym_buf + symcount;
408 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
409 isym < isymend;
410 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
411 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
413 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
414 (*_bfd_error_handler) (_("%B symbol number %lu references "
415 "nonexistent SHT_SYMTAB_SHNDX section"),
416 ibfd, (unsigned long) symoffset);
417 intsym_buf = NULL;
418 goto out;
421 out:
422 if (alloc_ext != NULL)
423 free (alloc_ext);
424 if (alloc_extshndx != NULL)
425 free (alloc_extshndx);
427 return intsym_buf;
430 /* Look up a symbol name. */
431 const char *
432 bfd_elf_sym_name (bfd *abfd,
433 Elf_Internal_Shdr *symtab_hdr,
434 Elf_Internal_Sym *isym,
435 asection *sym_sec)
437 const char *name;
438 unsigned int iname = isym->st_name;
439 unsigned int shindex = symtab_hdr->sh_link;
441 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
442 /* Check for a bogus st_shndx to avoid crashing. */
443 && isym->st_shndx < elf_numsections (abfd)
444 && !(isym->st_shndx >= SHN_LORESERVE && isym->st_shndx <= SHN_HIRESERVE))
446 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
447 shindex = elf_elfheader (abfd)->e_shstrndx;
450 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
451 if (name == NULL)
452 name = "(null)";
453 else if (sym_sec && *name == '\0')
454 name = bfd_section_name (abfd, sym_sec);
456 return name;
459 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
460 sections. The first element is the flags, the rest are section
461 pointers. */
463 typedef union elf_internal_group {
464 Elf_Internal_Shdr *shdr;
465 unsigned int flags;
466 } Elf_Internal_Group;
468 /* Return the name of the group signature symbol. Why isn't the
469 signature just a string? */
471 static const char *
472 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
474 Elf_Internal_Shdr *hdr;
475 unsigned char esym[sizeof (Elf64_External_Sym)];
476 Elf_External_Sym_Shndx eshndx;
477 Elf_Internal_Sym isym;
479 /* First we need to ensure the symbol table is available. Make sure
480 that it is a symbol table section. */
481 hdr = elf_elfsections (abfd) [ghdr->sh_link];
482 if (hdr->sh_type != SHT_SYMTAB
483 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
484 return NULL;
486 /* Go read the symbol. */
487 hdr = &elf_tdata (abfd)->symtab_hdr;
488 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
489 &isym, esym, &eshndx) == NULL)
490 return NULL;
492 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
495 /* Set next_in_group list pointer, and group name for NEWSECT. */
497 static bfd_boolean
498 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
500 unsigned int num_group = elf_tdata (abfd)->num_group;
502 /* If num_group is zero, read in all SHT_GROUP sections. The count
503 is set to -1 if there are no SHT_GROUP sections. */
504 if (num_group == 0)
506 unsigned int i, shnum;
508 /* First count the number of groups. If we have a SHT_GROUP
509 section with just a flag word (ie. sh_size is 4), ignore it. */
510 shnum = elf_numsections (abfd);
511 num_group = 0;
513 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
514 ( (shdr)->sh_type == SHT_GROUP \
515 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
516 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
517 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
519 for (i = 0; i < shnum; i++)
521 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
523 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
524 num_group += 1;
527 if (num_group == 0)
529 num_group = (unsigned) -1;
530 elf_tdata (abfd)->num_group = num_group;
532 else
534 /* We keep a list of elf section headers for group sections,
535 so we can find them quickly. */
536 bfd_size_type amt;
538 elf_tdata (abfd)->num_group = num_group;
539 elf_tdata (abfd)->group_sect_ptr
540 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
541 if (elf_tdata (abfd)->group_sect_ptr == NULL)
542 return FALSE;
544 num_group = 0;
545 for (i = 0; i < shnum; i++)
547 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
549 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
551 unsigned char *src;
552 Elf_Internal_Group *dest;
554 /* Add to list of sections. */
555 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
556 num_group += 1;
558 /* Read the raw contents. */
559 BFD_ASSERT (sizeof (*dest) >= 4);
560 amt = shdr->sh_size * sizeof (*dest) / 4;
561 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
562 sizeof (*dest) / 4);
563 /* PR binutils/4110: Handle corrupt group headers. */
564 if (shdr->contents == NULL)
566 _bfd_error_handler
567 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
568 bfd_set_error (bfd_error_bad_value);
569 return FALSE;
572 memset (shdr->contents, 0, amt);
574 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
575 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
576 != shdr->sh_size))
577 return FALSE;
579 /* Translate raw contents, a flag word followed by an
580 array of elf section indices all in target byte order,
581 to the flag word followed by an array of elf section
582 pointers. */
583 src = shdr->contents + shdr->sh_size;
584 dest = (Elf_Internal_Group *) (shdr->contents + amt);
585 while (1)
587 unsigned int idx;
589 src -= 4;
590 --dest;
591 idx = H_GET_32 (abfd, src);
592 if (src == shdr->contents)
594 dest->flags = idx;
595 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
596 shdr->bfd_section->flags
597 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
598 break;
600 if (idx >= shnum)
602 ((*_bfd_error_handler)
603 (_("%B: invalid SHT_GROUP entry"), abfd));
604 idx = 0;
606 dest->shdr = elf_elfsections (abfd)[idx];
613 if (num_group != (unsigned) -1)
615 unsigned int i;
617 for (i = 0; i < num_group; i++)
619 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
620 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
621 unsigned int n_elt = shdr->sh_size / 4;
623 /* Look through this group's sections to see if current
624 section is a member. */
625 while (--n_elt != 0)
626 if ((++idx)->shdr == hdr)
628 asection *s = NULL;
630 /* We are a member of this group. Go looking through
631 other members to see if any others are linked via
632 next_in_group. */
633 idx = (Elf_Internal_Group *) shdr->contents;
634 n_elt = shdr->sh_size / 4;
635 while (--n_elt != 0)
636 if ((s = (++idx)->shdr->bfd_section) != NULL
637 && elf_next_in_group (s) != NULL)
638 break;
639 if (n_elt != 0)
641 /* Snarf the group name from other member, and
642 insert current section in circular list. */
643 elf_group_name (newsect) = elf_group_name (s);
644 elf_next_in_group (newsect) = elf_next_in_group (s);
645 elf_next_in_group (s) = newsect;
647 else
649 const char *gname;
651 gname = group_signature (abfd, shdr);
652 if (gname == NULL)
653 return FALSE;
654 elf_group_name (newsect) = gname;
656 /* Start a circular list with one element. */
657 elf_next_in_group (newsect) = newsect;
660 /* If the group section has been created, point to the
661 new member. */
662 if (shdr->bfd_section != NULL)
663 elf_next_in_group (shdr->bfd_section) = newsect;
665 i = num_group - 1;
666 break;
671 if (elf_group_name (newsect) == NULL)
673 (*_bfd_error_handler) (_("%B: no group info for section %A"),
674 abfd, newsect);
676 return TRUE;
679 bfd_boolean
680 _bfd_elf_setup_sections (bfd *abfd)
682 unsigned int i;
683 unsigned int num_group = elf_tdata (abfd)->num_group;
684 bfd_boolean result = TRUE;
685 asection *s;
687 /* Process SHF_LINK_ORDER. */
688 for (s = abfd->sections; s != NULL; s = s->next)
690 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
691 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
693 unsigned int elfsec = this_hdr->sh_link;
694 /* FIXME: The old Intel compiler and old strip/objcopy may
695 not set the sh_link or sh_info fields. Hence we could
696 get the situation where elfsec is 0. */
697 if (elfsec == 0)
699 const struct elf_backend_data *bed
700 = get_elf_backend_data (abfd);
701 if (bed->link_order_error_handler)
702 bed->link_order_error_handler
703 (_("%B: warning: sh_link not set for section `%A'"),
704 abfd, s);
706 else
708 asection *link;
710 this_hdr = elf_elfsections (abfd)[elfsec];
712 /* PR 1991, 2008:
713 Some strip/objcopy may leave an incorrect value in
714 sh_link. We don't want to proceed. */
715 link = this_hdr->bfd_section;
716 if (link == NULL)
718 (*_bfd_error_handler)
719 (_("%B: sh_link [%d] in section `%A' is incorrect"),
720 s->owner, s, elfsec);
721 result = FALSE;
724 elf_linked_to_section (s) = link;
729 /* Process section groups. */
730 if (num_group == (unsigned) -1)
731 return result;
733 for (i = 0; i < num_group; i++)
735 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
736 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
737 unsigned int n_elt = shdr->sh_size / 4;
739 while (--n_elt != 0)
740 if ((++idx)->shdr->bfd_section)
741 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
742 else if (idx->shdr->sh_type == SHT_RELA
743 || idx->shdr->sh_type == SHT_REL)
744 /* We won't include relocation sections in section groups in
745 output object files. We adjust the group section size here
746 so that relocatable link will work correctly when
747 relocation sections are in section group in input object
748 files. */
749 shdr->bfd_section->size -= 4;
750 else
752 /* There are some unknown sections in the group. */
753 (*_bfd_error_handler)
754 (_("%B: unknown [%d] section `%s' in group [%s]"),
755 abfd,
756 (unsigned int) idx->shdr->sh_type,
757 bfd_elf_string_from_elf_section (abfd,
758 (elf_elfheader (abfd)
759 ->e_shstrndx),
760 idx->shdr->sh_name),
761 shdr->bfd_section->name);
762 result = FALSE;
765 return result;
768 bfd_boolean
769 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
771 return elf_next_in_group (sec) != NULL;
774 /* Make a BFD section from an ELF section. We store a pointer to the
775 BFD section in the bfd_section field of the header. */
777 bfd_boolean
778 _bfd_elf_make_section_from_shdr (bfd *abfd,
779 Elf_Internal_Shdr *hdr,
780 const char *name,
781 int shindex)
783 asection *newsect;
784 flagword flags;
785 const struct elf_backend_data *bed;
787 if (hdr->bfd_section != NULL)
789 BFD_ASSERT (strcmp (name,
790 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
791 return TRUE;
794 newsect = bfd_make_section_anyway (abfd, name);
795 if (newsect == NULL)
796 return FALSE;
798 hdr->bfd_section = newsect;
799 elf_section_data (newsect)->this_hdr = *hdr;
800 elf_section_data (newsect)->this_idx = shindex;
802 /* Always use the real type/flags. */
803 elf_section_type (newsect) = hdr->sh_type;
804 elf_section_flags (newsect) = hdr->sh_flags;
806 newsect->filepos = hdr->sh_offset;
808 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
809 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
810 || ! bfd_set_section_alignment (abfd, newsect,
811 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
812 return FALSE;
814 flags = SEC_NO_FLAGS;
815 if (hdr->sh_type != SHT_NOBITS)
816 flags |= SEC_HAS_CONTENTS;
817 if (hdr->sh_type == SHT_GROUP)
818 flags |= SEC_GROUP | SEC_EXCLUDE;
819 if ((hdr->sh_flags & SHF_ALLOC) != 0)
821 flags |= SEC_ALLOC;
822 if (hdr->sh_type != SHT_NOBITS)
823 flags |= SEC_LOAD;
825 if ((hdr->sh_flags & SHF_WRITE) == 0)
826 flags |= SEC_READONLY;
827 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
828 flags |= SEC_CODE;
829 else if ((flags & SEC_LOAD) != 0)
830 flags |= SEC_DATA;
831 if ((hdr->sh_flags & SHF_MERGE) != 0)
833 flags |= SEC_MERGE;
834 newsect->entsize = hdr->sh_entsize;
835 if ((hdr->sh_flags & SHF_STRINGS) != 0)
836 flags |= SEC_STRINGS;
838 if (hdr->sh_flags & SHF_GROUP)
839 if (!setup_group (abfd, hdr, newsect))
840 return FALSE;
841 if ((hdr->sh_flags & SHF_TLS) != 0)
842 flags |= SEC_THREAD_LOCAL;
844 if ((flags & SEC_ALLOC) == 0)
846 /* The debugging sections appear to be recognized only by name,
847 not any sort of flag. Their SEC_ALLOC bits are cleared. */
848 static const struct
850 const char *name;
851 int len;
852 } debug_sections [] =
854 { STRING_COMMA_LEN ("debug") }, /* 'd' */
855 { NULL, 0 }, /* 'e' */
856 { NULL, 0 }, /* 'f' */
857 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
858 { NULL, 0 }, /* 'h' */
859 { NULL, 0 }, /* 'i' */
860 { NULL, 0 }, /* 'j' */
861 { NULL, 0 }, /* 'k' */
862 { STRING_COMMA_LEN ("line") }, /* 'l' */
863 { NULL, 0 }, /* 'm' */
864 { NULL, 0 }, /* 'n' */
865 { NULL, 0 }, /* 'o' */
866 { NULL, 0 }, /* 'p' */
867 { NULL, 0 }, /* 'q' */
868 { NULL, 0 }, /* 'r' */
869 { STRING_COMMA_LEN ("stab") } /* 's' */
872 if (name [0] == '.')
874 int i = name [1] - 'd';
875 if (i >= 0
876 && i < (int) ARRAY_SIZE (debug_sections)
877 && debug_sections [i].name != NULL
878 && strncmp (&name [1], debug_sections [i].name,
879 debug_sections [i].len) == 0)
880 flags |= SEC_DEBUGGING;
884 /* As a GNU extension, if the name begins with .gnu.linkonce, we
885 only link a single copy of the section. This is used to support
886 g++. g++ will emit each template expansion in its own section.
887 The symbols will be defined as weak, so that multiple definitions
888 are permitted. The GNU linker extension is to actually discard
889 all but one of the sections. */
890 if (CONST_STRNEQ (name, ".gnu.linkonce")
891 && elf_next_in_group (newsect) == NULL)
892 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
894 bed = get_elf_backend_data (abfd);
895 if (bed->elf_backend_section_flags)
896 if (! bed->elf_backend_section_flags (&flags, hdr))
897 return FALSE;
899 if (! bfd_set_section_flags (abfd, newsect, flags))
900 return FALSE;
902 if ((flags & SEC_ALLOC) != 0)
904 Elf_Internal_Phdr *phdr;
905 unsigned int i;
907 /* Look through the phdrs to see if we need to adjust the lma.
908 If all the p_paddr fields are zero, we ignore them, since
909 some ELF linkers produce such output. */
910 phdr = elf_tdata (abfd)->phdr;
911 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
913 if (phdr->p_paddr != 0)
914 break;
916 if (i < elf_elfheader (abfd)->e_phnum)
918 phdr = elf_tdata (abfd)->phdr;
919 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
921 /* This section is part of this segment if its file
922 offset plus size lies within the segment's memory
923 span and, if the section is loaded, the extent of the
924 loaded data lies within the extent of the segment.
926 Note - we used to check the p_paddr field as well, and
927 refuse to set the LMA if it was 0. This is wrong
928 though, as a perfectly valid initialised segment can
929 have a p_paddr of zero. Some architectures, eg ARM,
930 place special significance on the address 0 and
931 executables need to be able to have a segment which
932 covers this address. */
933 if (phdr->p_type == PT_LOAD
934 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
935 && (hdr->sh_offset + hdr->sh_size
936 <= phdr->p_offset + phdr->p_memsz)
937 && ((flags & SEC_LOAD) == 0
938 || (hdr->sh_offset + hdr->sh_size
939 <= phdr->p_offset + phdr->p_filesz)))
941 if ((flags & SEC_LOAD) == 0)
942 newsect->lma = (phdr->p_paddr
943 + hdr->sh_addr - phdr->p_vaddr);
944 else
945 /* We used to use the same adjustment for SEC_LOAD
946 sections, but that doesn't work if the segment
947 is packed with code from multiple VMAs.
948 Instead we calculate the section LMA based on
949 the segment LMA. It is assumed that the
950 segment will contain sections with contiguous
951 LMAs, even if the VMAs are not. */
952 newsect->lma = (phdr->p_paddr
953 + hdr->sh_offset - phdr->p_offset);
955 /* With contiguous segments, we can't tell from file
956 offsets whether a section with zero size should
957 be placed at the end of one segment or the
958 beginning of the next. Decide based on vaddr. */
959 if (hdr->sh_addr >= phdr->p_vaddr
960 && (hdr->sh_addr + hdr->sh_size
961 <= phdr->p_vaddr + phdr->p_memsz))
962 break;
968 return TRUE;
972 INTERNAL_FUNCTION
973 bfd_elf_find_section
975 SYNOPSIS
976 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
978 DESCRIPTION
979 Helper functions for GDB to locate the string tables.
980 Since BFD hides string tables from callers, GDB needs to use an
981 internal hook to find them. Sun's .stabstr, in particular,
982 isn't even pointed to by the .stab section, so ordinary
983 mechanisms wouldn't work to find it, even if we had some.
986 struct elf_internal_shdr *
987 bfd_elf_find_section (bfd *abfd, char *name)
989 Elf_Internal_Shdr **i_shdrp;
990 char *shstrtab;
991 unsigned int max;
992 unsigned int i;
994 i_shdrp = elf_elfsections (abfd);
995 if (i_shdrp != NULL)
997 shstrtab = bfd_elf_get_str_section (abfd,
998 elf_elfheader (abfd)->e_shstrndx);
999 if (shstrtab != NULL)
1001 max = elf_numsections (abfd);
1002 for (i = 1; i < max; i++)
1003 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
1004 return i_shdrp[i];
1007 return 0;
1010 const char *const bfd_elf_section_type_names[] = {
1011 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1012 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1013 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1016 /* ELF relocs are against symbols. If we are producing relocatable
1017 output, and the reloc is against an external symbol, and nothing
1018 has given us any additional addend, the resulting reloc will also
1019 be against the same symbol. In such a case, we don't want to
1020 change anything about the way the reloc is handled, since it will
1021 all be done at final link time. Rather than put special case code
1022 into bfd_perform_relocation, all the reloc types use this howto
1023 function. It just short circuits the reloc if producing
1024 relocatable output against an external symbol. */
1026 bfd_reloc_status_type
1027 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1028 arelent *reloc_entry,
1029 asymbol *symbol,
1030 void *data ATTRIBUTE_UNUSED,
1031 asection *input_section,
1032 bfd *output_bfd,
1033 char **error_message ATTRIBUTE_UNUSED)
1035 if (output_bfd != NULL
1036 && (symbol->flags & BSF_SECTION_SYM) == 0
1037 && (! reloc_entry->howto->partial_inplace
1038 || reloc_entry->addend == 0))
1040 reloc_entry->address += input_section->output_offset;
1041 return bfd_reloc_ok;
1044 return bfd_reloc_continue;
1047 /* Copy the program header and other data from one object module to
1048 another. */
1050 bfd_boolean
1051 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1053 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1054 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1055 return TRUE;
1057 BFD_ASSERT (!elf_flags_init (obfd)
1058 || (elf_elfheader (obfd)->e_flags
1059 == elf_elfheader (ibfd)->e_flags));
1061 elf_gp (obfd) = elf_gp (ibfd);
1062 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1063 elf_flags_init (obfd) = TRUE;
1065 /* Copy object attributes. */
1066 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1068 return TRUE;
1071 static const char *
1072 get_segment_type (unsigned int p_type)
1074 const char *pt;
1075 switch (p_type)
1077 case PT_NULL: pt = "NULL"; break;
1078 case PT_LOAD: pt = "LOAD"; break;
1079 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1080 case PT_INTERP: pt = "INTERP"; break;
1081 case PT_NOTE: pt = "NOTE"; break;
1082 case PT_SHLIB: pt = "SHLIB"; break;
1083 case PT_PHDR: pt = "PHDR"; break;
1084 case PT_TLS: pt = "TLS"; break;
1085 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1086 case PT_GNU_STACK: pt = "STACK"; break;
1087 case PT_GNU_RELRO: pt = "RELRO"; break;
1088 default: pt = NULL; break;
1090 return pt;
1093 /* Print out the program headers. */
1095 bfd_boolean
1096 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1098 FILE *f = farg;
1099 Elf_Internal_Phdr *p;
1100 asection *s;
1101 bfd_byte *dynbuf = NULL;
1103 p = elf_tdata (abfd)->phdr;
1104 if (p != NULL)
1106 unsigned int i, c;
1108 fprintf (f, _("\nProgram Header:\n"));
1109 c = elf_elfheader (abfd)->e_phnum;
1110 for (i = 0; i < c; i++, p++)
1112 const char *pt = get_segment_type (p->p_type);
1113 char buf[20];
1115 if (pt == NULL)
1117 sprintf (buf, "0x%lx", p->p_type);
1118 pt = buf;
1120 fprintf (f, "%8s off 0x", pt);
1121 bfd_fprintf_vma (abfd, f, p->p_offset);
1122 fprintf (f, " vaddr 0x");
1123 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1124 fprintf (f, " paddr 0x");
1125 bfd_fprintf_vma (abfd, f, p->p_paddr);
1126 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1127 fprintf (f, " filesz 0x");
1128 bfd_fprintf_vma (abfd, f, p->p_filesz);
1129 fprintf (f, " memsz 0x");
1130 bfd_fprintf_vma (abfd, f, p->p_memsz);
1131 fprintf (f, " flags %c%c%c",
1132 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1133 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1134 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1135 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1136 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1137 fprintf (f, "\n");
1141 s = bfd_get_section_by_name (abfd, ".dynamic");
1142 if (s != NULL)
1144 int elfsec;
1145 unsigned long shlink;
1146 bfd_byte *extdyn, *extdynend;
1147 size_t extdynsize;
1148 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1150 fprintf (f, _("\nDynamic Section:\n"));
1152 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1153 goto error_return;
1155 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1156 if (elfsec == -1)
1157 goto error_return;
1158 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1160 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1161 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1163 extdyn = dynbuf;
1164 extdynend = extdyn + s->size;
1165 for (; extdyn < extdynend; extdyn += extdynsize)
1167 Elf_Internal_Dyn dyn;
1168 const char *name;
1169 char ab[20];
1170 bfd_boolean stringp;
1172 (*swap_dyn_in) (abfd, extdyn, &dyn);
1174 if (dyn.d_tag == DT_NULL)
1175 break;
1177 stringp = FALSE;
1178 switch (dyn.d_tag)
1180 default:
1181 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1182 name = ab;
1183 break;
1185 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1186 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1187 case DT_PLTGOT: name = "PLTGOT"; break;
1188 case DT_HASH: name = "HASH"; break;
1189 case DT_STRTAB: name = "STRTAB"; break;
1190 case DT_SYMTAB: name = "SYMTAB"; break;
1191 case DT_RELA: name = "RELA"; break;
1192 case DT_RELASZ: name = "RELASZ"; break;
1193 case DT_RELAENT: name = "RELAENT"; break;
1194 case DT_STRSZ: name = "STRSZ"; break;
1195 case DT_SYMENT: name = "SYMENT"; break;
1196 case DT_INIT: name = "INIT"; break;
1197 case DT_FINI: name = "FINI"; break;
1198 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1199 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1200 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1201 case DT_REL: name = "REL"; break;
1202 case DT_RELSZ: name = "RELSZ"; break;
1203 case DT_RELENT: name = "RELENT"; break;
1204 case DT_PLTREL: name = "PLTREL"; break;
1205 case DT_DEBUG: name = "DEBUG"; break;
1206 case DT_TEXTREL: name = "TEXTREL"; break;
1207 case DT_JMPREL: name = "JMPREL"; break;
1208 case DT_BIND_NOW: name = "BIND_NOW"; break;
1209 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1210 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1211 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1212 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1213 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1214 case DT_FLAGS: name = "FLAGS"; break;
1215 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1216 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1217 case DT_CHECKSUM: name = "CHECKSUM"; break;
1218 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1219 case DT_MOVEENT: name = "MOVEENT"; break;
1220 case DT_MOVESZ: name = "MOVESZ"; break;
1221 case DT_FEATURE: name = "FEATURE"; break;
1222 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1223 case DT_SYMINSZ: name = "SYMINSZ"; break;
1224 case DT_SYMINENT: name = "SYMINENT"; break;
1225 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1226 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1227 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1228 case DT_PLTPAD: name = "PLTPAD"; break;
1229 case DT_MOVETAB: name = "MOVETAB"; break;
1230 case DT_SYMINFO: name = "SYMINFO"; break;
1231 case DT_RELACOUNT: name = "RELACOUNT"; break;
1232 case DT_RELCOUNT: name = "RELCOUNT"; break;
1233 case DT_FLAGS_1: name = "FLAGS_1"; break;
1234 case DT_VERSYM: name = "VERSYM"; break;
1235 case DT_VERDEF: name = "VERDEF"; break;
1236 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1237 case DT_VERNEED: name = "VERNEED"; break;
1238 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1239 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1240 case DT_USED: name = "USED"; break;
1241 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1242 case DT_GNU_HASH: name = "GNU_HASH"; break;
1245 fprintf (f, " %-11s ", name);
1246 if (! stringp)
1247 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1248 else
1250 const char *string;
1251 unsigned int tagv = dyn.d_un.d_val;
1253 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1254 if (string == NULL)
1255 goto error_return;
1256 fprintf (f, "%s", string);
1258 fprintf (f, "\n");
1261 free (dynbuf);
1262 dynbuf = NULL;
1265 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1266 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1268 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1269 return FALSE;
1272 if (elf_dynverdef (abfd) != 0)
1274 Elf_Internal_Verdef *t;
1276 fprintf (f, _("\nVersion definitions:\n"));
1277 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1279 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1280 t->vd_flags, t->vd_hash,
1281 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1282 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1284 Elf_Internal_Verdaux *a;
1286 fprintf (f, "\t");
1287 for (a = t->vd_auxptr->vda_nextptr;
1288 a != NULL;
1289 a = a->vda_nextptr)
1290 fprintf (f, "%s ",
1291 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1292 fprintf (f, "\n");
1297 if (elf_dynverref (abfd) != 0)
1299 Elf_Internal_Verneed *t;
1301 fprintf (f, _("\nVersion References:\n"));
1302 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1304 Elf_Internal_Vernaux *a;
1306 fprintf (f, _(" required from %s:\n"),
1307 t->vn_filename ? t->vn_filename : "<corrupt>");
1308 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1309 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1310 a->vna_flags, a->vna_other,
1311 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1315 return TRUE;
1317 error_return:
1318 if (dynbuf != NULL)
1319 free (dynbuf);
1320 return FALSE;
1323 /* Display ELF-specific fields of a symbol. */
1325 void
1326 bfd_elf_print_symbol (bfd *abfd,
1327 void *filep,
1328 asymbol *symbol,
1329 bfd_print_symbol_type how)
1331 FILE *file = filep;
1332 switch (how)
1334 case bfd_print_symbol_name:
1335 fprintf (file, "%s", symbol->name);
1336 break;
1337 case bfd_print_symbol_more:
1338 fprintf (file, "elf ");
1339 bfd_fprintf_vma (abfd, file, symbol->value);
1340 fprintf (file, " %lx", (long) symbol->flags);
1341 break;
1342 case bfd_print_symbol_all:
1344 const char *section_name;
1345 const char *name = NULL;
1346 const struct elf_backend_data *bed;
1347 unsigned char st_other;
1348 bfd_vma val;
1350 section_name = symbol->section ? symbol->section->name : "(*none*)";
1352 bed = get_elf_backend_data (abfd);
1353 if (bed->elf_backend_print_symbol_all)
1354 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1356 if (name == NULL)
1358 name = symbol->name;
1359 bfd_print_symbol_vandf (abfd, file, symbol);
1362 fprintf (file, " %s\t", section_name);
1363 /* Print the "other" value for a symbol. For common symbols,
1364 we've already printed the size; now print the alignment.
1365 For other symbols, we have no specified alignment, and
1366 we've printed the address; now print the size. */
1367 if (symbol->section && bfd_is_com_section (symbol->section))
1368 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1369 else
1370 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1371 bfd_fprintf_vma (abfd, file, val);
1373 /* If we have version information, print it. */
1374 if (elf_tdata (abfd)->dynversym_section != 0
1375 && (elf_tdata (abfd)->dynverdef_section != 0
1376 || elf_tdata (abfd)->dynverref_section != 0))
1378 unsigned int vernum;
1379 const char *version_string;
1381 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1383 if (vernum == 0)
1384 version_string = "";
1385 else if (vernum == 1)
1386 version_string = "Base";
1387 else if (vernum <= elf_tdata (abfd)->cverdefs)
1388 version_string =
1389 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1390 else
1392 Elf_Internal_Verneed *t;
1394 version_string = "";
1395 for (t = elf_tdata (abfd)->verref;
1396 t != NULL;
1397 t = t->vn_nextref)
1399 Elf_Internal_Vernaux *a;
1401 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1403 if (a->vna_other == vernum)
1405 version_string = a->vna_nodename;
1406 break;
1412 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1413 fprintf (file, " %-11s", version_string);
1414 else
1416 int i;
1418 fprintf (file, " (%s)", version_string);
1419 for (i = 10 - strlen (version_string); i > 0; --i)
1420 putc (' ', file);
1424 /* If the st_other field is not zero, print it. */
1425 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1427 switch (st_other)
1429 case 0: break;
1430 case STV_INTERNAL: fprintf (file, " .internal"); break;
1431 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1432 case STV_PROTECTED: fprintf (file, " .protected"); break;
1433 default:
1434 /* Some other non-defined flags are also present, so print
1435 everything hex. */
1436 fprintf (file, " 0x%02x", (unsigned int) st_other);
1439 fprintf (file, " %s", name);
1441 break;
1445 /* Allocate an ELF string table--force the first byte to be zero. */
1447 struct bfd_strtab_hash *
1448 _bfd_elf_stringtab_init (void)
1450 struct bfd_strtab_hash *ret;
1452 ret = _bfd_stringtab_init ();
1453 if (ret != NULL)
1455 bfd_size_type loc;
1457 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1458 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1459 if (loc == (bfd_size_type) -1)
1461 _bfd_stringtab_free (ret);
1462 ret = NULL;
1465 return ret;
1468 /* ELF .o/exec file reading */
1470 /* Create a new bfd section from an ELF section header. */
1472 bfd_boolean
1473 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1475 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1476 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1477 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1478 const char *name;
1480 name = bfd_elf_string_from_elf_section (abfd,
1481 elf_elfheader (abfd)->e_shstrndx,
1482 hdr->sh_name);
1483 if (name == NULL)
1484 return FALSE;
1486 switch (hdr->sh_type)
1488 case SHT_NULL:
1489 /* Inactive section. Throw it away. */
1490 return TRUE;
1492 case SHT_PROGBITS: /* Normal section with contents. */
1493 case SHT_NOBITS: /* .bss section. */
1494 case SHT_HASH: /* .hash section. */
1495 case SHT_NOTE: /* .note section. */
1496 case SHT_INIT_ARRAY: /* .init_array section. */
1497 case SHT_FINI_ARRAY: /* .fini_array section. */
1498 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1499 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1500 case SHT_GNU_HASH: /* .gnu.hash section. */
1501 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1503 case SHT_DYNAMIC: /* Dynamic linking information. */
1504 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1505 return FALSE;
1506 if (hdr->sh_link > elf_numsections (abfd)
1507 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1508 return FALSE;
1509 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1511 Elf_Internal_Shdr *dynsymhdr;
1513 /* The shared libraries distributed with hpux11 have a bogus
1514 sh_link field for the ".dynamic" section. Find the
1515 string table for the ".dynsym" section instead. */
1516 if (elf_dynsymtab (abfd) != 0)
1518 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1519 hdr->sh_link = dynsymhdr->sh_link;
1521 else
1523 unsigned int i, num_sec;
1525 num_sec = elf_numsections (abfd);
1526 for (i = 1; i < num_sec; i++)
1528 dynsymhdr = elf_elfsections (abfd)[i];
1529 if (dynsymhdr->sh_type == SHT_DYNSYM)
1531 hdr->sh_link = dynsymhdr->sh_link;
1532 break;
1537 break;
1539 case SHT_SYMTAB: /* A symbol table */
1540 if (elf_onesymtab (abfd) == shindex)
1541 return TRUE;
1543 if (hdr->sh_entsize != bed->s->sizeof_sym)
1544 return FALSE;
1545 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1546 elf_onesymtab (abfd) = shindex;
1547 elf_tdata (abfd)->symtab_hdr = *hdr;
1548 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1549 abfd->flags |= HAS_SYMS;
1551 /* Sometimes a shared object will map in the symbol table. If
1552 SHF_ALLOC is set, and this is a shared object, then we also
1553 treat this section as a BFD section. We can not base the
1554 decision purely on SHF_ALLOC, because that flag is sometimes
1555 set in a relocatable object file, which would confuse the
1556 linker. */
1557 if ((hdr->sh_flags & SHF_ALLOC) != 0
1558 && (abfd->flags & DYNAMIC) != 0
1559 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1560 shindex))
1561 return FALSE;
1563 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1564 can't read symbols without that section loaded as well. It
1565 is most likely specified by the next section header. */
1566 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1568 unsigned int i, num_sec;
1570 num_sec = elf_numsections (abfd);
1571 for (i = shindex + 1; i < num_sec; i++)
1573 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1574 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1575 && hdr2->sh_link == shindex)
1576 break;
1578 if (i == num_sec)
1579 for (i = 1; i < shindex; i++)
1581 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1582 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1583 && hdr2->sh_link == shindex)
1584 break;
1586 if (i != shindex)
1587 return bfd_section_from_shdr (abfd, i);
1589 return TRUE;
1591 case SHT_DYNSYM: /* A dynamic symbol table */
1592 if (elf_dynsymtab (abfd) == shindex)
1593 return TRUE;
1595 if (hdr->sh_entsize != bed->s->sizeof_sym)
1596 return FALSE;
1597 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1598 elf_dynsymtab (abfd) = shindex;
1599 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1600 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1601 abfd->flags |= HAS_SYMS;
1603 /* Besides being a symbol table, we also treat this as a regular
1604 section, so that objcopy can handle it. */
1605 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1607 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1608 if (elf_symtab_shndx (abfd) == shindex)
1609 return TRUE;
1611 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1612 elf_symtab_shndx (abfd) = shindex;
1613 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1614 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1615 return TRUE;
1617 case SHT_STRTAB: /* A string table */
1618 if (hdr->bfd_section != NULL)
1619 return TRUE;
1620 if (ehdr->e_shstrndx == shindex)
1622 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1623 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1624 return TRUE;
1626 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1628 symtab_strtab:
1629 elf_tdata (abfd)->strtab_hdr = *hdr;
1630 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1631 return TRUE;
1633 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1635 dynsymtab_strtab:
1636 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1637 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1638 elf_elfsections (abfd)[shindex] = hdr;
1639 /* We also treat this as a regular section, so that objcopy
1640 can handle it. */
1641 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1642 shindex);
1645 /* If the string table isn't one of the above, then treat it as a
1646 regular section. We need to scan all the headers to be sure,
1647 just in case this strtab section appeared before the above. */
1648 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1650 unsigned int i, num_sec;
1652 num_sec = elf_numsections (abfd);
1653 for (i = 1; i < num_sec; i++)
1655 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1656 if (hdr2->sh_link == shindex)
1658 /* Prevent endless recursion on broken objects. */
1659 if (i == shindex)
1660 return FALSE;
1661 if (! bfd_section_from_shdr (abfd, i))
1662 return FALSE;
1663 if (elf_onesymtab (abfd) == i)
1664 goto symtab_strtab;
1665 if (elf_dynsymtab (abfd) == i)
1666 goto dynsymtab_strtab;
1670 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1672 case SHT_REL:
1673 case SHT_RELA:
1674 /* *These* do a lot of work -- but build no sections! */
1676 asection *target_sect;
1677 Elf_Internal_Shdr *hdr2;
1678 unsigned int num_sec = elf_numsections (abfd);
1680 if (hdr->sh_entsize
1681 != (bfd_size_type) (hdr->sh_type == SHT_REL
1682 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1683 return FALSE;
1685 /* Check for a bogus link to avoid crashing. */
1686 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1687 || hdr->sh_link >= num_sec)
1689 ((*_bfd_error_handler)
1690 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1691 abfd, hdr->sh_link, name, shindex));
1692 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1693 shindex);
1696 /* For some incomprehensible reason Oracle distributes
1697 libraries for Solaris in which some of the objects have
1698 bogus sh_link fields. It would be nice if we could just
1699 reject them, but, unfortunately, some people need to use
1700 them. We scan through the section headers; if we find only
1701 one suitable symbol table, we clobber the sh_link to point
1702 to it. I hope this doesn't break anything. */
1703 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1704 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1706 unsigned int scan;
1707 int found;
1709 found = 0;
1710 for (scan = 1; scan < num_sec; scan++)
1712 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1713 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1715 if (found != 0)
1717 found = 0;
1718 break;
1720 found = scan;
1723 if (found != 0)
1724 hdr->sh_link = found;
1727 /* Get the symbol table. */
1728 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1729 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1730 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1731 return FALSE;
1733 /* If this reloc section does not use the main symbol table we
1734 don't treat it as a reloc section. BFD can't adequately
1735 represent such a section, so at least for now, we don't
1736 try. We just present it as a normal section. We also
1737 can't use it as a reloc section if it points to the null
1738 section, an invalid section, or another reloc section. */
1739 if (hdr->sh_link != elf_onesymtab (abfd)
1740 || hdr->sh_info == SHN_UNDEF
1741 || (hdr->sh_info >= SHN_LORESERVE && hdr->sh_info <= SHN_HIRESERVE)
1742 || hdr->sh_info >= num_sec
1743 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1744 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1745 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1746 shindex);
1748 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1749 return FALSE;
1750 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1751 if (target_sect == NULL)
1752 return FALSE;
1754 if ((target_sect->flags & SEC_RELOC) == 0
1755 || target_sect->reloc_count == 0)
1756 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1757 else
1759 bfd_size_type amt;
1760 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1761 amt = sizeof (*hdr2);
1762 hdr2 = bfd_alloc (abfd, amt);
1763 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1765 *hdr2 = *hdr;
1766 elf_elfsections (abfd)[shindex] = hdr2;
1767 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1768 target_sect->flags |= SEC_RELOC;
1769 target_sect->relocation = NULL;
1770 target_sect->rel_filepos = hdr->sh_offset;
1771 /* In the section to which the relocations apply, mark whether
1772 its relocations are of the REL or RELA variety. */
1773 if (hdr->sh_size != 0)
1774 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1775 abfd->flags |= HAS_RELOC;
1776 return TRUE;
1779 case SHT_GNU_verdef:
1780 elf_dynverdef (abfd) = shindex;
1781 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1782 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1784 case SHT_GNU_versym:
1785 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1786 return FALSE;
1787 elf_dynversym (abfd) = shindex;
1788 elf_tdata (abfd)->dynversym_hdr = *hdr;
1789 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1791 case SHT_GNU_verneed:
1792 elf_dynverref (abfd) = shindex;
1793 elf_tdata (abfd)->dynverref_hdr = *hdr;
1794 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1796 case SHT_SHLIB:
1797 return TRUE;
1799 case SHT_GROUP:
1800 /* We need a BFD section for objcopy and relocatable linking,
1801 and it's handy to have the signature available as the section
1802 name. */
1803 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1804 return FALSE;
1805 name = group_signature (abfd, hdr);
1806 if (name == NULL)
1807 return FALSE;
1808 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1809 return FALSE;
1810 if (hdr->contents != NULL)
1812 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1813 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1814 asection *s;
1816 if (idx->flags & GRP_COMDAT)
1817 hdr->bfd_section->flags
1818 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1820 /* We try to keep the same section order as it comes in. */
1821 idx += n_elt;
1822 while (--n_elt != 0)
1824 --idx;
1826 if (idx->shdr != NULL
1827 && (s = idx->shdr->bfd_section) != NULL
1828 && elf_next_in_group (s) != NULL)
1830 elf_next_in_group (hdr->bfd_section) = s;
1831 break;
1835 break;
1837 default:
1838 /* Possibly an attributes section. */
1839 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1840 || hdr->sh_type == bed->obj_attrs_section_type)
1842 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1843 return FALSE;
1844 _bfd_elf_parse_attributes (abfd, hdr);
1845 return TRUE;
1848 /* Check for any processor-specific section types. */
1849 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1850 return TRUE;
1852 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1854 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1855 /* FIXME: How to properly handle allocated section reserved
1856 for applications? */
1857 (*_bfd_error_handler)
1858 (_("%B: don't know how to handle allocated, application "
1859 "specific section `%s' [0x%8x]"),
1860 abfd, name, hdr->sh_type);
1861 else
1862 /* Allow sections reserved for applications. */
1863 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1864 shindex);
1866 else if (hdr->sh_type >= SHT_LOPROC
1867 && hdr->sh_type <= SHT_HIPROC)
1868 /* FIXME: We should handle this section. */
1869 (*_bfd_error_handler)
1870 (_("%B: don't know how to handle processor specific section "
1871 "`%s' [0x%8x]"),
1872 abfd, name, hdr->sh_type);
1873 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1875 /* Unrecognised OS-specific sections. */
1876 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1877 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1878 required to correctly process the section and the file should
1879 be rejected with an error message. */
1880 (*_bfd_error_handler)
1881 (_("%B: don't know how to handle OS specific section "
1882 "`%s' [0x%8x]"),
1883 abfd, name, hdr->sh_type);
1884 else
1885 /* Otherwise it should be processed. */
1886 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1888 else
1889 /* FIXME: We should handle this section. */
1890 (*_bfd_error_handler)
1891 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1892 abfd, name, hdr->sh_type);
1894 return FALSE;
1897 return TRUE;
1900 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1901 Return SEC for sections that have no elf section, and NULL on error. */
1903 asection *
1904 bfd_section_from_r_symndx (bfd *abfd,
1905 struct sym_sec_cache *cache,
1906 asection *sec,
1907 unsigned long r_symndx)
1909 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1910 asection *s;
1912 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1914 Elf_Internal_Shdr *symtab_hdr;
1915 unsigned char esym[sizeof (Elf64_External_Sym)];
1916 Elf_External_Sym_Shndx eshndx;
1917 Elf_Internal_Sym isym;
1919 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1920 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1921 &isym, esym, &eshndx) == NULL)
1922 return NULL;
1924 if (cache->abfd != abfd)
1926 memset (cache->indx, -1, sizeof (cache->indx));
1927 cache->abfd = abfd;
1929 cache->indx[ent] = r_symndx;
1930 cache->shndx[ent] = isym.st_shndx;
1933 s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
1934 if (s != NULL)
1935 return s;
1937 return sec;
1940 /* Given an ELF section number, retrieve the corresponding BFD
1941 section. */
1943 asection *
1944 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
1946 if (index >= elf_numsections (abfd))
1947 return NULL;
1948 return elf_elfsections (abfd)[index]->bfd_section;
1951 static const struct bfd_elf_special_section special_sections_b[] =
1953 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1954 { NULL, 0, 0, 0, 0 }
1957 static const struct bfd_elf_special_section special_sections_c[] =
1959 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1960 { NULL, 0, 0, 0, 0 }
1963 static const struct bfd_elf_special_section special_sections_d[] =
1965 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1966 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1967 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
1968 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
1969 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
1970 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
1971 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
1972 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
1973 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
1974 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
1975 { NULL, 0, 0, 0, 0 }
1978 static const struct bfd_elf_special_section special_sections_f[] =
1980 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
1981 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
1982 { NULL, 0, 0, 0, 0 }
1985 static const struct bfd_elf_special_section special_sections_g[] =
1987 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1988 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1989 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
1990 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
1991 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
1992 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
1993 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
1994 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
1995 { NULL, 0, 0, 0, 0 }
1998 static const struct bfd_elf_special_section special_sections_h[] =
2000 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2001 { NULL, 0, 0, 0, 0 }
2004 static const struct bfd_elf_special_section special_sections_i[] =
2006 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2007 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2008 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2009 { NULL, 0, 0, 0, 0 }
2012 static const struct bfd_elf_special_section special_sections_l[] =
2014 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2015 { NULL, 0, 0, 0, 0 }
2018 static const struct bfd_elf_special_section special_sections_n[] =
2020 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2021 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2022 { NULL, 0, 0, 0, 0 }
2025 static const struct bfd_elf_special_section special_sections_p[] =
2027 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2028 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2029 { NULL, 0, 0, 0, 0 }
2032 static const struct bfd_elf_special_section special_sections_r[] =
2034 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2035 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2036 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2037 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2038 { NULL, 0, 0, 0, 0 }
2041 static const struct bfd_elf_special_section special_sections_s[] =
2043 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2044 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2045 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2046 /* See struct bfd_elf_special_section declaration for the semantics of
2047 this special case where .prefix_length != strlen (.prefix). */
2048 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2049 { NULL, 0, 0, 0, 0 }
2052 static const struct bfd_elf_special_section special_sections_t[] =
2054 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2055 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2056 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2057 { NULL, 0, 0, 0, 0 }
2060 static const struct bfd_elf_special_section *special_sections[] =
2062 special_sections_b, /* 'b' */
2063 special_sections_c, /* 'b' */
2064 special_sections_d, /* 'd' */
2065 NULL, /* 'e' */
2066 special_sections_f, /* 'f' */
2067 special_sections_g, /* 'g' */
2068 special_sections_h, /* 'h' */
2069 special_sections_i, /* 'i' */
2070 NULL, /* 'j' */
2071 NULL, /* 'k' */
2072 special_sections_l, /* 'l' */
2073 NULL, /* 'm' */
2074 special_sections_n, /* 'n' */
2075 NULL, /* 'o' */
2076 special_sections_p, /* 'p' */
2077 NULL, /* 'q' */
2078 special_sections_r, /* 'r' */
2079 special_sections_s, /* 's' */
2080 special_sections_t, /* 't' */
2083 const struct bfd_elf_special_section *
2084 _bfd_elf_get_special_section (const char *name,
2085 const struct bfd_elf_special_section *spec,
2086 unsigned int rela)
2088 int i;
2089 int len;
2091 len = strlen (name);
2093 for (i = 0; spec[i].prefix != NULL; i++)
2095 int suffix_len;
2096 int prefix_len = spec[i].prefix_length;
2098 if (len < prefix_len)
2099 continue;
2100 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2101 continue;
2103 suffix_len = spec[i].suffix_length;
2104 if (suffix_len <= 0)
2106 if (name[prefix_len] != 0)
2108 if (suffix_len == 0)
2109 continue;
2110 if (name[prefix_len] != '.'
2111 && (suffix_len == -2
2112 || (rela && spec[i].type == SHT_REL)))
2113 continue;
2116 else
2118 if (len < prefix_len + suffix_len)
2119 continue;
2120 if (memcmp (name + len - suffix_len,
2121 spec[i].prefix + prefix_len,
2122 suffix_len) != 0)
2123 continue;
2125 return &spec[i];
2128 return NULL;
2131 const struct bfd_elf_special_section *
2132 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2134 int i;
2135 const struct bfd_elf_special_section *spec;
2136 const struct elf_backend_data *bed;
2138 /* See if this is one of the special sections. */
2139 if (sec->name == NULL)
2140 return NULL;
2142 bed = get_elf_backend_data (abfd);
2143 spec = bed->special_sections;
2144 if (spec)
2146 spec = _bfd_elf_get_special_section (sec->name,
2147 bed->special_sections,
2148 sec->use_rela_p);
2149 if (spec != NULL)
2150 return spec;
2153 if (sec->name[0] != '.')
2154 return NULL;
2156 i = sec->name[1] - 'b';
2157 if (i < 0 || i > 't' - 'b')
2158 return NULL;
2160 spec = special_sections[i];
2162 if (spec == NULL)
2163 return NULL;
2165 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2168 bfd_boolean
2169 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2171 struct bfd_elf_section_data *sdata;
2172 const struct elf_backend_data *bed;
2173 const struct bfd_elf_special_section *ssect;
2175 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2176 if (sdata == NULL)
2178 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2179 if (sdata == NULL)
2180 return FALSE;
2181 sec->used_by_bfd = sdata;
2184 /* Indicate whether or not this section should use RELA relocations. */
2185 bed = get_elf_backend_data (abfd);
2186 sec->use_rela_p = bed->default_use_rela_p;
2188 /* When we read a file, we don't need to set ELF section type and
2189 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2190 anyway. We will set ELF section type and flags for all linker
2191 created sections. If user specifies BFD section flags, we will
2192 set ELF section type and flags based on BFD section flags in
2193 elf_fake_sections. */
2194 if ((!sec->flags && abfd->direction != read_direction)
2195 || (sec->flags & SEC_LINKER_CREATED) != 0)
2197 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2198 if (ssect != NULL)
2200 elf_section_type (sec) = ssect->type;
2201 elf_section_flags (sec) = ssect->attr;
2205 return _bfd_generic_new_section_hook (abfd, sec);
2208 /* Create a new bfd section from an ELF program header.
2210 Since program segments have no names, we generate a synthetic name
2211 of the form segment<NUM>, where NUM is generally the index in the
2212 program header table. For segments that are split (see below) we
2213 generate the names segment<NUM>a and segment<NUM>b.
2215 Note that some program segments may have a file size that is different than
2216 (less than) the memory size. All this means is that at execution the
2217 system must allocate the amount of memory specified by the memory size,
2218 but only initialize it with the first "file size" bytes read from the
2219 file. This would occur for example, with program segments consisting
2220 of combined data+bss.
2222 To handle the above situation, this routine generates TWO bfd sections
2223 for the single program segment. The first has the length specified by
2224 the file size of the segment, and the second has the length specified
2225 by the difference between the two sizes. In effect, the segment is split
2226 into its initialized and uninitialized parts.
2230 bfd_boolean
2231 _bfd_elf_make_section_from_phdr (bfd *abfd,
2232 Elf_Internal_Phdr *hdr,
2233 int index,
2234 const char *typename)
2236 asection *newsect;
2237 char *name;
2238 char namebuf[64];
2239 size_t len;
2240 int split;
2242 split = ((hdr->p_memsz > 0)
2243 && (hdr->p_filesz > 0)
2244 && (hdr->p_memsz > hdr->p_filesz));
2246 if (hdr->p_filesz > 0)
2248 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2249 len = strlen (namebuf) + 1;
2250 name = bfd_alloc (abfd, len);
2251 if (!name)
2252 return FALSE;
2253 memcpy (name, namebuf, len);
2254 newsect = bfd_make_section (abfd, name);
2255 if (newsect == NULL)
2256 return FALSE;
2257 newsect->vma = hdr->p_vaddr;
2258 newsect->lma = hdr->p_paddr;
2259 newsect->size = hdr->p_filesz;
2260 newsect->filepos = hdr->p_offset;
2261 newsect->flags |= SEC_HAS_CONTENTS;
2262 newsect->alignment_power = bfd_log2 (hdr->p_align);
2263 if (hdr->p_type == PT_LOAD)
2265 newsect->flags |= SEC_ALLOC;
2266 newsect->flags |= SEC_LOAD;
2267 if (hdr->p_flags & PF_X)
2269 /* FIXME: all we known is that it has execute PERMISSION,
2270 may be data. */
2271 newsect->flags |= SEC_CODE;
2274 if (!(hdr->p_flags & PF_W))
2276 newsect->flags |= SEC_READONLY;
2280 if (hdr->p_memsz > hdr->p_filesz)
2282 bfd_vma align;
2284 sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : "");
2285 len = strlen (namebuf) + 1;
2286 name = bfd_alloc (abfd, len);
2287 if (!name)
2288 return FALSE;
2289 memcpy (name, namebuf, len);
2290 newsect = bfd_make_section (abfd, name);
2291 if (newsect == NULL)
2292 return FALSE;
2293 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2294 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2295 newsect->size = hdr->p_memsz - hdr->p_filesz;
2296 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2297 align = newsect->vma & -newsect->vma;
2298 if (align == 0 || align > hdr->p_align)
2299 align = hdr->p_align;
2300 newsect->alignment_power = bfd_log2 (align);
2301 if (hdr->p_type == PT_LOAD)
2303 /* Hack for gdb. Segments that have not been modified do
2304 not have their contents written to a core file, on the
2305 assumption that a debugger can find the contents in the
2306 executable. We flag this case by setting the fake
2307 section size to zero. Note that "real" bss sections will
2308 always have their contents dumped to the core file. */
2309 if (bfd_get_format (abfd) == bfd_core)
2310 newsect->size = 0;
2311 newsect->flags |= SEC_ALLOC;
2312 if (hdr->p_flags & PF_X)
2313 newsect->flags |= SEC_CODE;
2315 if (!(hdr->p_flags & PF_W))
2316 newsect->flags |= SEC_READONLY;
2319 return TRUE;
2322 bfd_boolean
2323 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2325 const struct elf_backend_data *bed;
2327 switch (hdr->p_type)
2329 case PT_NULL:
2330 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2332 case PT_LOAD:
2333 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2335 case PT_DYNAMIC:
2336 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2338 case PT_INTERP:
2339 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2341 case PT_NOTE:
2342 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2343 return FALSE;
2344 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2345 return FALSE;
2346 return TRUE;
2348 case PT_SHLIB:
2349 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2351 case PT_PHDR:
2352 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2354 case PT_GNU_EH_FRAME:
2355 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2356 "eh_frame_hdr");
2358 case PT_GNU_STACK:
2359 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2361 case PT_GNU_RELRO:
2362 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2364 default:
2365 /* Check for any processor-specific program segment types. */
2366 bed = get_elf_backend_data (abfd);
2367 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2371 /* Initialize REL_HDR, the section-header for new section, containing
2372 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2373 relocations; otherwise, we use REL relocations. */
2375 bfd_boolean
2376 _bfd_elf_init_reloc_shdr (bfd *abfd,
2377 Elf_Internal_Shdr *rel_hdr,
2378 asection *asect,
2379 bfd_boolean use_rela_p)
2381 char *name;
2382 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2383 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2385 name = bfd_alloc (abfd, amt);
2386 if (name == NULL)
2387 return FALSE;
2388 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2389 rel_hdr->sh_name =
2390 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2391 FALSE);
2392 if (rel_hdr->sh_name == (unsigned int) -1)
2393 return FALSE;
2394 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2395 rel_hdr->sh_entsize = (use_rela_p
2396 ? bed->s->sizeof_rela
2397 : bed->s->sizeof_rel);
2398 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2399 rel_hdr->sh_flags = 0;
2400 rel_hdr->sh_addr = 0;
2401 rel_hdr->sh_size = 0;
2402 rel_hdr->sh_offset = 0;
2404 return TRUE;
2407 /* Set up an ELF internal section header for a section. */
2409 static void
2410 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2412 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2413 bfd_boolean *failedptr = failedptrarg;
2414 Elf_Internal_Shdr *this_hdr;
2415 unsigned int sh_type;
2417 if (*failedptr)
2419 /* We already failed; just get out of the bfd_map_over_sections
2420 loop. */
2421 return;
2424 this_hdr = &elf_section_data (asect)->this_hdr;
2426 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2427 asect->name, FALSE);
2428 if (this_hdr->sh_name == (unsigned int) -1)
2430 *failedptr = TRUE;
2431 return;
2434 /* Don't clear sh_flags. Assembler may set additional bits. */
2436 if ((asect->flags & SEC_ALLOC) != 0
2437 || asect->user_set_vma)
2438 this_hdr->sh_addr = asect->vma;
2439 else
2440 this_hdr->sh_addr = 0;
2442 this_hdr->sh_offset = 0;
2443 this_hdr->sh_size = asect->size;
2444 this_hdr->sh_link = 0;
2445 this_hdr->sh_addralign = 1 << asect->alignment_power;
2446 /* The sh_entsize and sh_info fields may have been set already by
2447 copy_private_section_data. */
2449 this_hdr->bfd_section = asect;
2450 this_hdr->contents = NULL;
2452 /* If the section type is unspecified, we set it based on
2453 asect->flags. */
2454 if (this_hdr->sh_type == SHT_NULL)
2456 if ((asect->flags & SEC_GROUP) != 0)
2457 this_hdr->sh_type = SHT_GROUP;
2458 else if ((asect->flags & SEC_ALLOC) != 0
2459 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2460 || (asect->flags & SEC_NEVER_LOAD) != 0))
2461 this_hdr->sh_type = SHT_NOBITS;
2462 else
2463 this_hdr->sh_type = SHT_PROGBITS;
2466 switch (this_hdr->sh_type)
2468 default:
2469 break;
2471 case SHT_STRTAB:
2472 case SHT_INIT_ARRAY:
2473 case SHT_FINI_ARRAY:
2474 case SHT_PREINIT_ARRAY:
2475 case SHT_NOTE:
2476 case SHT_NOBITS:
2477 case SHT_PROGBITS:
2478 break;
2480 case SHT_HASH:
2481 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2482 break;
2484 case SHT_DYNSYM:
2485 this_hdr->sh_entsize = bed->s->sizeof_sym;
2486 break;
2488 case SHT_DYNAMIC:
2489 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2490 break;
2492 case SHT_RELA:
2493 if (get_elf_backend_data (abfd)->may_use_rela_p)
2494 this_hdr->sh_entsize = bed->s->sizeof_rela;
2495 break;
2497 case SHT_REL:
2498 if (get_elf_backend_data (abfd)->may_use_rel_p)
2499 this_hdr->sh_entsize = bed->s->sizeof_rel;
2500 break;
2502 case SHT_GNU_versym:
2503 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2504 break;
2506 case SHT_GNU_verdef:
2507 this_hdr->sh_entsize = 0;
2508 /* objcopy or strip will copy over sh_info, but may not set
2509 cverdefs. The linker will set cverdefs, but sh_info will be
2510 zero. */
2511 if (this_hdr->sh_info == 0)
2512 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2513 else
2514 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2515 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2516 break;
2518 case SHT_GNU_verneed:
2519 this_hdr->sh_entsize = 0;
2520 /* objcopy or strip will copy over sh_info, but may not set
2521 cverrefs. The linker will set cverrefs, but sh_info will be
2522 zero. */
2523 if (this_hdr->sh_info == 0)
2524 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2525 else
2526 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2527 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2528 break;
2530 case SHT_GROUP:
2531 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2532 break;
2534 case SHT_GNU_HASH:
2535 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2536 break;
2539 if ((asect->flags & SEC_ALLOC) != 0)
2540 this_hdr->sh_flags |= SHF_ALLOC;
2541 if ((asect->flags & SEC_READONLY) == 0)
2542 this_hdr->sh_flags |= SHF_WRITE;
2543 if ((asect->flags & SEC_CODE) != 0)
2544 this_hdr->sh_flags |= SHF_EXECINSTR;
2545 if ((asect->flags & SEC_MERGE) != 0)
2547 this_hdr->sh_flags |= SHF_MERGE;
2548 this_hdr->sh_entsize = asect->entsize;
2549 if ((asect->flags & SEC_STRINGS) != 0)
2550 this_hdr->sh_flags |= SHF_STRINGS;
2552 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2553 this_hdr->sh_flags |= SHF_GROUP;
2554 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2556 this_hdr->sh_flags |= SHF_TLS;
2557 if (asect->size == 0
2558 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2560 struct bfd_link_order *o = asect->map_tail.link_order;
2562 this_hdr->sh_size = 0;
2563 if (o != NULL)
2565 this_hdr->sh_size = o->offset + o->size;
2566 if (this_hdr->sh_size != 0)
2567 this_hdr->sh_type = SHT_NOBITS;
2572 /* Check for processor-specific section types. */
2573 sh_type = this_hdr->sh_type;
2574 if (bed->elf_backend_fake_sections
2575 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2576 *failedptr = TRUE;
2578 if (sh_type == SHT_NOBITS && asect->size != 0)
2580 /* Don't change the header type from NOBITS if we are being
2581 called for objcopy --only-keep-debug. */
2582 this_hdr->sh_type = sh_type;
2585 /* If the section has relocs, set up a section header for the
2586 SHT_REL[A] section. If two relocation sections are required for
2587 this section, it is up to the processor-specific back-end to
2588 create the other. */
2589 if ((asect->flags & SEC_RELOC) != 0
2590 && !_bfd_elf_init_reloc_shdr (abfd,
2591 &elf_section_data (asect)->rel_hdr,
2592 asect,
2593 asect->use_rela_p))
2594 *failedptr = TRUE;
2597 /* Fill in the contents of a SHT_GROUP section. */
2599 void
2600 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2602 bfd_boolean *failedptr = failedptrarg;
2603 unsigned long symindx;
2604 asection *elt, *first;
2605 unsigned char *loc;
2606 bfd_boolean gas;
2608 /* Ignore linker created group section. See elfNN_ia64_object_p in
2609 elfxx-ia64.c. */
2610 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2611 || *failedptr)
2612 return;
2614 symindx = 0;
2615 if (elf_group_id (sec) != NULL)
2616 symindx = elf_group_id (sec)->udata.i;
2618 if (symindx == 0)
2620 /* If called from the assembler, swap_out_syms will have set up
2621 elf_section_syms; If called for "ld -r", use target_index. */
2622 if (elf_section_syms (abfd) != NULL)
2623 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2624 else
2625 symindx = sec->target_index;
2627 elf_section_data (sec)->this_hdr.sh_info = symindx;
2629 /* The contents won't be allocated for "ld -r" or objcopy. */
2630 gas = TRUE;
2631 if (sec->contents == NULL)
2633 gas = FALSE;
2634 sec->contents = bfd_alloc (abfd, sec->size);
2636 /* Arrange for the section to be written out. */
2637 elf_section_data (sec)->this_hdr.contents = sec->contents;
2638 if (sec->contents == NULL)
2640 *failedptr = TRUE;
2641 return;
2645 loc = sec->contents + sec->size;
2647 /* Get the pointer to the first section in the group that gas
2648 squirreled away here. objcopy arranges for this to be set to the
2649 start of the input section group. */
2650 first = elt = elf_next_in_group (sec);
2652 /* First element is a flag word. Rest of section is elf section
2653 indices for all the sections of the group. Write them backwards
2654 just to keep the group in the same order as given in .section
2655 directives, not that it matters. */
2656 while (elt != NULL)
2658 asection *s;
2659 unsigned int idx;
2661 loc -= 4;
2662 s = elt;
2663 if (!gas)
2664 s = s->output_section;
2665 idx = 0;
2666 if (s != NULL)
2667 idx = elf_section_data (s)->this_idx;
2668 H_PUT_32 (abfd, idx, loc);
2669 elt = elf_next_in_group (elt);
2670 if (elt == first)
2671 break;
2674 if ((loc -= 4) != sec->contents)
2675 abort ();
2677 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2680 /* Assign all ELF section numbers. The dummy first section is handled here
2681 too. The link/info pointers for the standard section types are filled
2682 in here too, while we're at it. */
2684 static bfd_boolean
2685 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2687 struct elf_obj_tdata *t = elf_tdata (abfd);
2688 asection *sec;
2689 unsigned int section_number, secn;
2690 Elf_Internal_Shdr **i_shdrp;
2691 struct bfd_elf_section_data *d;
2693 section_number = 1;
2695 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2697 /* SHT_GROUP sections are in relocatable files only. */
2698 if (link_info == NULL || link_info->relocatable)
2700 /* Put SHT_GROUP sections first. */
2701 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2703 d = elf_section_data (sec);
2705 if (d->this_hdr.sh_type == SHT_GROUP)
2707 if (sec->flags & SEC_LINKER_CREATED)
2709 /* Remove the linker created SHT_GROUP sections. */
2710 bfd_section_list_remove (abfd, sec);
2711 abfd->section_count--;
2713 else
2715 if (section_number == SHN_LORESERVE)
2716 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2717 d->this_idx = section_number++;
2723 for (sec = abfd->sections; sec; sec = sec->next)
2725 d = elf_section_data (sec);
2727 if (d->this_hdr.sh_type != SHT_GROUP)
2729 if (section_number == SHN_LORESERVE)
2730 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2731 d->this_idx = section_number++;
2733 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2734 if ((sec->flags & SEC_RELOC) == 0)
2735 d->rel_idx = 0;
2736 else
2738 if (section_number == SHN_LORESERVE)
2739 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2740 d->rel_idx = section_number++;
2741 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2744 if (d->rel_hdr2)
2746 if (section_number == SHN_LORESERVE)
2747 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2748 d->rel_idx2 = section_number++;
2749 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2751 else
2752 d->rel_idx2 = 0;
2755 if (section_number == SHN_LORESERVE)
2756 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2757 t->shstrtab_section = section_number++;
2758 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2759 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2761 if (bfd_get_symcount (abfd) > 0)
2763 if (section_number == SHN_LORESERVE)
2764 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2765 t->symtab_section = section_number++;
2766 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2767 if (section_number > SHN_LORESERVE - 2)
2769 if (section_number == SHN_LORESERVE)
2770 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2771 t->symtab_shndx_section = section_number++;
2772 t->symtab_shndx_hdr.sh_name
2773 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2774 ".symtab_shndx", FALSE);
2775 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2776 return FALSE;
2778 if (section_number == SHN_LORESERVE)
2779 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2780 t->strtab_section = section_number++;
2781 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2784 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2785 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2787 elf_numsections (abfd) = section_number;
2788 elf_elfheader (abfd)->e_shnum = section_number;
2789 if (section_number > SHN_LORESERVE)
2790 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2792 /* Set up the list of section header pointers, in agreement with the
2793 indices. */
2794 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
2795 if (i_shdrp == NULL)
2796 return FALSE;
2798 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2799 if (i_shdrp[0] == NULL)
2801 bfd_release (abfd, i_shdrp);
2802 return FALSE;
2805 elf_elfsections (abfd) = i_shdrp;
2807 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2808 if (bfd_get_symcount (abfd) > 0)
2810 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2811 if (elf_numsections (abfd) > SHN_LORESERVE)
2813 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2814 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2816 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2817 t->symtab_hdr.sh_link = t->strtab_section;
2820 for (sec = abfd->sections; sec; sec = sec->next)
2822 struct bfd_elf_section_data *d = elf_section_data (sec);
2823 asection *s;
2824 const char *name;
2826 i_shdrp[d->this_idx] = &d->this_hdr;
2827 if (d->rel_idx != 0)
2828 i_shdrp[d->rel_idx] = &d->rel_hdr;
2829 if (d->rel_idx2 != 0)
2830 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2832 /* Fill in the sh_link and sh_info fields while we're at it. */
2834 /* sh_link of a reloc section is the section index of the symbol
2835 table. sh_info is the section index of the section to which
2836 the relocation entries apply. */
2837 if (d->rel_idx != 0)
2839 d->rel_hdr.sh_link = t->symtab_section;
2840 d->rel_hdr.sh_info = d->this_idx;
2842 if (d->rel_idx2 != 0)
2844 d->rel_hdr2->sh_link = t->symtab_section;
2845 d->rel_hdr2->sh_info = d->this_idx;
2848 /* We need to set up sh_link for SHF_LINK_ORDER. */
2849 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2851 s = elf_linked_to_section (sec);
2852 if (s)
2854 /* elf_linked_to_section points to the input section. */
2855 if (link_info != NULL)
2857 /* Check discarded linkonce section. */
2858 if (elf_discarded_section (s))
2860 asection *kept;
2861 (*_bfd_error_handler)
2862 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2863 abfd, d->this_hdr.bfd_section,
2864 s, s->owner);
2865 /* Point to the kept section if it has the same
2866 size as the discarded one. */
2867 kept = _bfd_elf_check_kept_section (s, link_info);
2868 if (kept == NULL)
2870 bfd_set_error (bfd_error_bad_value);
2871 return FALSE;
2873 s = kept;
2876 s = s->output_section;
2877 BFD_ASSERT (s != NULL);
2879 else
2881 /* Handle objcopy. */
2882 if (s->output_section == NULL)
2884 (*_bfd_error_handler)
2885 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2886 abfd, d->this_hdr.bfd_section, s, s->owner);
2887 bfd_set_error (bfd_error_bad_value);
2888 return FALSE;
2890 s = s->output_section;
2892 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2894 else
2896 /* PR 290:
2897 The Intel C compiler generates SHT_IA_64_UNWIND with
2898 SHF_LINK_ORDER. But it doesn't set the sh_link or
2899 sh_info fields. Hence we could get the situation
2900 where s is NULL. */
2901 const struct elf_backend_data *bed
2902 = get_elf_backend_data (abfd);
2903 if (bed->link_order_error_handler)
2904 bed->link_order_error_handler
2905 (_("%B: warning: sh_link not set for section `%A'"),
2906 abfd, sec);
2910 switch (d->this_hdr.sh_type)
2912 case SHT_REL:
2913 case SHT_RELA:
2914 /* A reloc section which we are treating as a normal BFD
2915 section. sh_link is the section index of the symbol
2916 table. sh_info is the section index of the section to
2917 which the relocation entries apply. We assume that an
2918 allocated reloc section uses the dynamic symbol table.
2919 FIXME: How can we be sure? */
2920 s = bfd_get_section_by_name (abfd, ".dynsym");
2921 if (s != NULL)
2922 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2924 /* We look up the section the relocs apply to by name. */
2925 name = sec->name;
2926 if (d->this_hdr.sh_type == SHT_REL)
2927 name += 4;
2928 else
2929 name += 5;
2930 s = bfd_get_section_by_name (abfd, name);
2931 if (s != NULL)
2932 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2933 break;
2935 case SHT_STRTAB:
2936 /* We assume that a section named .stab*str is a stabs
2937 string section. We look for a section with the same name
2938 but without the trailing ``str'', and set its sh_link
2939 field to point to this section. */
2940 if (CONST_STRNEQ (sec->name, ".stab")
2941 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2943 size_t len;
2944 char *alc;
2946 len = strlen (sec->name);
2947 alc = bfd_malloc (len - 2);
2948 if (alc == NULL)
2949 return FALSE;
2950 memcpy (alc, sec->name, len - 3);
2951 alc[len - 3] = '\0';
2952 s = bfd_get_section_by_name (abfd, alc);
2953 free (alc);
2954 if (s != NULL)
2956 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2958 /* This is a .stab section. */
2959 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2960 elf_section_data (s)->this_hdr.sh_entsize
2961 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2964 break;
2966 case SHT_DYNAMIC:
2967 case SHT_DYNSYM:
2968 case SHT_GNU_verneed:
2969 case SHT_GNU_verdef:
2970 /* sh_link is the section header index of the string table
2971 used for the dynamic entries, or the symbol table, or the
2972 version strings. */
2973 s = bfd_get_section_by_name (abfd, ".dynstr");
2974 if (s != NULL)
2975 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2976 break;
2978 case SHT_GNU_LIBLIST:
2979 /* sh_link is the section header index of the prelink library
2980 list used for the dynamic entries, or the symbol table, or
2981 the version strings. */
2982 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
2983 ? ".dynstr" : ".gnu.libstr");
2984 if (s != NULL)
2985 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2986 break;
2988 case SHT_HASH:
2989 case SHT_GNU_HASH:
2990 case SHT_GNU_versym:
2991 /* sh_link is the section header index of the symbol table
2992 this hash table or version table is for. */
2993 s = bfd_get_section_by_name (abfd, ".dynsym");
2994 if (s != NULL)
2995 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2996 break;
2998 case SHT_GROUP:
2999 d->this_hdr.sh_link = t->symtab_section;
3003 for (secn = 1; secn < section_number; ++secn)
3004 if (i_shdrp[secn] == NULL)
3005 i_shdrp[secn] = i_shdrp[0];
3006 else
3007 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3008 i_shdrp[secn]->sh_name);
3009 return TRUE;
3012 /* Map symbol from it's internal number to the external number, moving
3013 all local symbols to be at the head of the list. */
3015 static bfd_boolean
3016 sym_is_global (bfd *abfd, asymbol *sym)
3018 /* If the backend has a special mapping, use it. */
3019 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3020 if (bed->elf_backend_sym_is_global)
3021 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3023 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3024 || bfd_is_und_section (bfd_get_section (sym))
3025 || bfd_is_com_section (bfd_get_section (sym)));
3028 /* Don't output section symbols for sections that are not going to be
3029 output. Also, don't output section symbols for reloc and other
3030 special sections. */
3032 static bfd_boolean
3033 ignore_section_sym (bfd *abfd, asymbol *sym)
3035 return ((sym->flags & BSF_SECTION_SYM) != 0
3036 && (sym->value != 0
3037 || (sym->section->owner != abfd
3038 && (sym->section->output_section->owner != abfd
3039 || sym->section->output_offset != 0))));
3042 static bfd_boolean
3043 elf_map_symbols (bfd *abfd)
3045 unsigned int symcount = bfd_get_symcount (abfd);
3046 asymbol **syms = bfd_get_outsymbols (abfd);
3047 asymbol **sect_syms;
3048 unsigned int num_locals = 0;
3049 unsigned int num_globals = 0;
3050 unsigned int num_locals2 = 0;
3051 unsigned int num_globals2 = 0;
3052 int max_index = 0;
3053 unsigned int idx;
3054 asection *asect;
3055 asymbol **new_syms;
3057 #ifdef DEBUG
3058 fprintf (stderr, "elf_map_symbols\n");
3059 fflush (stderr);
3060 #endif
3062 for (asect = abfd->sections; asect; asect = asect->next)
3064 if (max_index < asect->index)
3065 max_index = asect->index;
3068 max_index++;
3069 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3070 if (sect_syms == NULL)
3071 return FALSE;
3072 elf_section_syms (abfd) = sect_syms;
3073 elf_num_section_syms (abfd) = max_index;
3075 /* Init sect_syms entries for any section symbols we have already
3076 decided to output. */
3077 for (idx = 0; idx < symcount; idx++)
3079 asymbol *sym = syms[idx];
3081 if ((sym->flags & BSF_SECTION_SYM) != 0
3082 && !ignore_section_sym (abfd, sym))
3084 asection *sec = sym->section;
3086 if (sec->owner != abfd)
3087 sec = sec->output_section;
3089 sect_syms[sec->index] = syms[idx];
3093 /* Classify all of the symbols. */
3094 for (idx = 0; idx < symcount; idx++)
3096 if (ignore_section_sym (abfd, syms[idx]))
3097 continue;
3098 if (!sym_is_global (abfd, syms[idx]))
3099 num_locals++;
3100 else
3101 num_globals++;
3104 /* We will be adding a section symbol for each normal BFD section. Most
3105 sections will already have a section symbol in outsymbols, but
3106 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3107 at least in that case. */
3108 for (asect = abfd->sections; asect; asect = asect->next)
3110 if (sect_syms[asect->index] == NULL)
3112 if (!sym_is_global (abfd, asect->symbol))
3113 num_locals++;
3114 else
3115 num_globals++;
3119 /* Now sort the symbols so the local symbols are first. */
3120 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3122 if (new_syms == NULL)
3123 return FALSE;
3125 for (idx = 0; idx < symcount; idx++)
3127 asymbol *sym = syms[idx];
3128 unsigned int i;
3130 if (ignore_section_sym (abfd, sym))
3131 continue;
3132 if (!sym_is_global (abfd, sym))
3133 i = num_locals2++;
3134 else
3135 i = num_locals + num_globals2++;
3136 new_syms[i] = sym;
3137 sym->udata.i = i + 1;
3139 for (asect = abfd->sections; asect; asect = asect->next)
3141 if (sect_syms[asect->index] == NULL)
3143 asymbol *sym = asect->symbol;
3144 unsigned int i;
3146 sect_syms[asect->index] = sym;
3147 if (!sym_is_global (abfd, sym))
3148 i = num_locals2++;
3149 else
3150 i = num_locals + num_globals2++;
3151 new_syms[i] = sym;
3152 sym->udata.i = i + 1;
3156 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3158 elf_num_locals (abfd) = num_locals;
3159 elf_num_globals (abfd) = num_globals;
3160 return TRUE;
3163 /* Align to the maximum file alignment that could be required for any
3164 ELF data structure. */
3166 static inline file_ptr
3167 align_file_position (file_ptr off, int align)
3169 return (off + align - 1) & ~(align - 1);
3172 /* Assign a file position to a section, optionally aligning to the
3173 required section alignment. */
3175 file_ptr
3176 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3177 file_ptr offset,
3178 bfd_boolean align)
3180 if (align)
3182 unsigned int al;
3184 al = i_shdrp->sh_addralign;
3185 if (al > 1)
3186 offset = BFD_ALIGN (offset, al);
3188 i_shdrp->sh_offset = offset;
3189 if (i_shdrp->bfd_section != NULL)
3190 i_shdrp->bfd_section->filepos = offset;
3191 if (i_shdrp->sh_type != SHT_NOBITS)
3192 offset += i_shdrp->sh_size;
3193 return offset;
3196 /* Compute the file positions we are going to put the sections at, and
3197 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3198 is not NULL, this is being called by the ELF backend linker. */
3200 bfd_boolean
3201 _bfd_elf_compute_section_file_positions (bfd *abfd,
3202 struct bfd_link_info *link_info)
3204 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3205 bfd_boolean failed;
3206 struct bfd_strtab_hash *strtab = NULL;
3207 Elf_Internal_Shdr *shstrtab_hdr;
3209 if (abfd->output_has_begun)
3210 return TRUE;
3212 /* Do any elf backend specific processing first. */
3213 if (bed->elf_backend_begin_write_processing)
3214 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3216 if (! prep_headers (abfd))
3217 return FALSE;
3219 /* Post process the headers if necessary. */
3220 if (bed->elf_backend_post_process_headers)
3221 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3223 failed = FALSE;
3224 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3225 if (failed)
3226 return FALSE;
3228 if (!assign_section_numbers (abfd, link_info))
3229 return FALSE;
3231 /* The backend linker builds symbol table information itself. */
3232 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3234 /* Non-zero if doing a relocatable link. */
3235 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3237 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3238 return FALSE;
3241 if (link_info == NULL)
3243 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3244 if (failed)
3245 return FALSE;
3248 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3249 /* sh_name was set in prep_headers. */
3250 shstrtab_hdr->sh_type = SHT_STRTAB;
3251 shstrtab_hdr->sh_flags = 0;
3252 shstrtab_hdr->sh_addr = 0;
3253 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3254 shstrtab_hdr->sh_entsize = 0;
3255 shstrtab_hdr->sh_link = 0;
3256 shstrtab_hdr->sh_info = 0;
3257 /* sh_offset is set in assign_file_positions_except_relocs. */
3258 shstrtab_hdr->sh_addralign = 1;
3260 if (!assign_file_positions_except_relocs (abfd, link_info))
3261 return FALSE;
3263 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3265 file_ptr off;
3266 Elf_Internal_Shdr *hdr;
3268 off = elf_tdata (abfd)->next_file_pos;
3270 hdr = &elf_tdata (abfd)->symtab_hdr;
3271 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3273 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3274 if (hdr->sh_size != 0)
3275 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3277 hdr = &elf_tdata (abfd)->strtab_hdr;
3278 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3280 elf_tdata (abfd)->next_file_pos = off;
3282 /* Now that we know where the .strtab section goes, write it
3283 out. */
3284 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3285 || ! _bfd_stringtab_emit (abfd, strtab))
3286 return FALSE;
3287 _bfd_stringtab_free (strtab);
3290 abfd->output_has_begun = TRUE;
3292 return TRUE;
3295 /* Make an initial estimate of the size of the program header. If we
3296 get the number wrong here, we'll redo section placement. */
3298 static bfd_size_type
3299 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3301 size_t segs;
3302 asection *s;
3303 const struct elf_backend_data *bed;
3305 /* Assume we will need exactly two PT_LOAD segments: one for text
3306 and one for data. */
3307 segs = 2;
3309 s = bfd_get_section_by_name (abfd, ".interp");
3310 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3312 /* If we have a loadable interpreter section, we need a
3313 PT_INTERP segment. In this case, assume we also need a
3314 PT_PHDR segment, although that may not be true for all
3315 targets. */
3316 segs += 2;
3319 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3321 /* We need a PT_DYNAMIC segment. */
3322 ++segs;
3324 if (elf_tdata (abfd)->relro)
3326 /* We need a PT_GNU_RELRO segment only when there is a
3327 PT_DYNAMIC segment. */
3328 ++segs;
3332 if (elf_tdata (abfd)->eh_frame_hdr)
3334 /* We need a PT_GNU_EH_FRAME segment. */
3335 ++segs;
3338 if (elf_tdata (abfd)->stack_flags)
3340 /* We need a PT_GNU_STACK segment. */
3341 ++segs;
3344 for (s = abfd->sections; s != NULL; s = s->next)
3346 if ((s->flags & SEC_LOAD) != 0
3347 && CONST_STRNEQ (s->name, ".note"))
3349 /* We need a PT_NOTE segment. */
3350 ++segs;
3351 /* Try to create just one PT_NOTE segment
3352 for all adjacent loadable .note* sections.
3353 gABI requires that within a PT_NOTE segment
3354 (and also inside of each SHT_NOTE section)
3355 each note is padded to a multiple of 4 size,
3356 so we check whether the sections are correctly
3357 aligned. */
3358 if (s->alignment_power == 2)
3359 while (s->next != NULL
3360 && s->next->alignment_power == 2
3361 && (s->next->flags & SEC_LOAD) != 0
3362 && CONST_STRNEQ (s->next->name, ".note"))
3363 s = s->next;
3367 for (s = abfd->sections; s != NULL; s = s->next)
3369 if (s->flags & SEC_THREAD_LOCAL)
3371 /* We need a PT_TLS segment. */
3372 ++segs;
3373 break;
3377 /* Let the backend count up any program headers it might need. */
3378 bed = get_elf_backend_data (abfd);
3379 if (bed->elf_backend_additional_program_headers)
3381 int a;
3383 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3384 if (a == -1)
3385 abort ();
3386 segs += a;
3389 return segs * bed->s->sizeof_phdr;
3392 /* Create a mapping from a set of sections to a program segment. */
3394 static struct elf_segment_map *
3395 make_mapping (bfd *abfd,
3396 asection **sections,
3397 unsigned int from,
3398 unsigned int to,
3399 bfd_boolean phdr)
3401 struct elf_segment_map *m;
3402 unsigned int i;
3403 asection **hdrpp;
3404 bfd_size_type amt;
3406 amt = sizeof (struct elf_segment_map);
3407 amt += (to - from - 1) * sizeof (asection *);
3408 m = bfd_zalloc (abfd, amt);
3409 if (m == NULL)
3410 return NULL;
3411 m->next = NULL;
3412 m->p_type = PT_LOAD;
3413 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3414 m->sections[i - from] = *hdrpp;
3415 m->count = to - from;
3417 if (from == 0 && phdr)
3419 /* Include the headers in the first PT_LOAD segment. */
3420 m->includes_filehdr = 1;
3421 m->includes_phdrs = 1;
3424 return m;
3427 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3428 on failure. */
3430 struct elf_segment_map *
3431 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3433 struct elf_segment_map *m;
3435 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3436 if (m == NULL)
3437 return NULL;
3438 m->next = NULL;
3439 m->p_type = PT_DYNAMIC;
3440 m->count = 1;
3441 m->sections[0] = dynsec;
3443 return m;
3446 /* Possibly add or remove segments from the segment map. */
3448 static bfd_boolean
3449 elf_modify_segment_map (bfd *abfd,
3450 struct bfd_link_info *info,
3451 bfd_boolean remove_empty_load)
3453 struct elf_segment_map **m;
3454 const struct elf_backend_data *bed;
3456 /* The placement algorithm assumes that non allocated sections are
3457 not in PT_LOAD segments. We ensure this here by removing such
3458 sections from the segment map. We also remove excluded
3459 sections. Finally, any PT_LOAD segment without sections is
3460 removed. */
3461 m = &elf_tdata (abfd)->segment_map;
3462 while (*m)
3464 unsigned int i, new_count;
3466 for (new_count = 0, i = 0; i < (*m)->count; i++)
3468 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3469 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3470 || (*m)->p_type != PT_LOAD))
3472 (*m)->sections[new_count] = (*m)->sections[i];
3473 new_count++;
3476 (*m)->count = new_count;
3478 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3479 *m = (*m)->next;
3480 else
3481 m = &(*m)->next;
3484 bed = get_elf_backend_data (abfd);
3485 if (bed->elf_backend_modify_segment_map != NULL)
3487 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3488 return FALSE;
3491 return TRUE;
3494 /* Set up a mapping from BFD sections to program segments. */
3496 bfd_boolean
3497 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3499 unsigned int count;
3500 struct elf_segment_map *m;
3501 asection **sections = NULL;
3502 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3503 bfd_boolean no_user_phdrs;
3505 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3506 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3508 asection *s;
3509 unsigned int i;
3510 struct elf_segment_map *mfirst;
3511 struct elf_segment_map **pm;
3512 asection *last_hdr;
3513 bfd_vma last_size;
3514 unsigned int phdr_index;
3515 bfd_vma maxpagesize;
3516 asection **hdrpp;
3517 bfd_boolean phdr_in_segment = TRUE;
3518 bfd_boolean writable;
3519 int tls_count = 0;
3520 asection *first_tls = NULL;
3521 asection *dynsec, *eh_frame_hdr;
3522 bfd_size_type amt;
3524 /* Select the allocated sections, and sort them. */
3526 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3527 if (sections == NULL)
3528 goto error_return;
3530 i = 0;
3531 for (s = abfd->sections; s != NULL; s = s->next)
3533 if ((s->flags & SEC_ALLOC) != 0)
3535 sections[i] = s;
3536 ++i;
3539 BFD_ASSERT (i <= bfd_count_sections (abfd));
3540 count = i;
3542 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3544 /* Build the mapping. */
3546 mfirst = NULL;
3547 pm = &mfirst;
3549 /* If we have a .interp section, then create a PT_PHDR segment for
3550 the program headers and a PT_INTERP segment for the .interp
3551 section. */
3552 s = bfd_get_section_by_name (abfd, ".interp");
3553 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3555 amt = sizeof (struct elf_segment_map);
3556 m = bfd_zalloc (abfd, amt);
3557 if (m == NULL)
3558 goto error_return;
3559 m->next = NULL;
3560 m->p_type = PT_PHDR;
3561 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3562 m->p_flags = PF_R | PF_X;
3563 m->p_flags_valid = 1;
3564 m->includes_phdrs = 1;
3566 *pm = m;
3567 pm = &m->next;
3569 amt = sizeof (struct elf_segment_map);
3570 m = bfd_zalloc (abfd, amt);
3571 if (m == NULL)
3572 goto error_return;
3573 m->next = NULL;
3574 m->p_type = PT_INTERP;
3575 m->count = 1;
3576 m->sections[0] = s;
3578 *pm = m;
3579 pm = &m->next;
3582 /* Look through the sections. We put sections in the same program
3583 segment when the start of the second section can be placed within
3584 a few bytes of the end of the first section. */
3585 last_hdr = NULL;
3586 last_size = 0;
3587 phdr_index = 0;
3588 maxpagesize = bed->maxpagesize;
3589 writable = FALSE;
3590 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3591 if (dynsec != NULL
3592 && (dynsec->flags & SEC_LOAD) == 0)
3593 dynsec = NULL;
3595 /* Deal with -Ttext or something similar such that the first section
3596 is not adjacent to the program headers. This is an
3597 approximation, since at this point we don't know exactly how many
3598 program headers we will need. */
3599 if (count > 0)
3601 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3603 if (phdr_size == (bfd_size_type) -1)
3604 phdr_size = get_program_header_size (abfd, info);
3605 if ((abfd->flags & D_PAGED) == 0
3606 || sections[0]->lma < phdr_size
3607 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3608 phdr_in_segment = FALSE;
3611 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3613 asection *hdr;
3614 bfd_boolean new_segment;
3616 hdr = *hdrpp;
3618 /* See if this section and the last one will fit in the same
3619 segment. */
3621 if (last_hdr == NULL)
3623 /* If we don't have a segment yet, then we don't need a new
3624 one (we build the last one after this loop). */
3625 new_segment = FALSE;
3627 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3629 /* If this section has a different relation between the
3630 virtual address and the load address, then we need a new
3631 segment. */
3632 new_segment = TRUE;
3634 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3635 < BFD_ALIGN (hdr->lma, maxpagesize))
3637 /* If putting this section in this segment would force us to
3638 skip a page in the segment, then we need a new segment. */
3639 new_segment = TRUE;
3641 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3642 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3644 /* We don't want to put a loadable section after a
3645 nonloadable section in the same segment.
3646 Consider .tbss sections as loadable for this purpose. */
3647 new_segment = TRUE;
3649 else if ((abfd->flags & D_PAGED) == 0)
3651 /* If the file is not demand paged, which means that we
3652 don't require the sections to be correctly aligned in the
3653 file, then there is no other reason for a new segment. */
3654 new_segment = FALSE;
3656 else if (! writable
3657 && (hdr->flags & SEC_READONLY) == 0
3658 && (((last_hdr->lma + last_size - 1)
3659 & ~(maxpagesize - 1))
3660 != (hdr->lma & ~(maxpagesize - 1))))
3662 /* We don't want to put a writable section in a read only
3663 segment, unless they are on the same page in memory
3664 anyhow. We already know that the last section does not
3665 bring us past the current section on the page, so the
3666 only case in which the new section is not on the same
3667 page as the previous section is when the previous section
3668 ends precisely on a page boundary. */
3669 new_segment = TRUE;
3671 else
3673 /* Otherwise, we can use the same segment. */
3674 new_segment = FALSE;
3677 /* Allow interested parties a chance to override our decision. */
3678 if (last_hdr && info->callbacks->override_segment_assignment)
3679 new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment);
3681 if (! new_segment)
3683 if ((hdr->flags & SEC_READONLY) == 0)
3684 writable = TRUE;
3685 last_hdr = hdr;
3686 /* .tbss sections effectively have zero size. */
3687 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3688 != SEC_THREAD_LOCAL)
3689 last_size = hdr->size;
3690 else
3691 last_size = 0;
3692 continue;
3695 /* We need a new program segment. We must create a new program
3696 header holding all the sections from phdr_index until hdr. */
3698 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3699 if (m == NULL)
3700 goto error_return;
3702 *pm = m;
3703 pm = &m->next;
3705 if ((hdr->flags & SEC_READONLY) == 0)
3706 writable = TRUE;
3707 else
3708 writable = FALSE;
3710 last_hdr = hdr;
3711 /* .tbss sections effectively have zero size. */
3712 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3713 last_size = hdr->size;
3714 else
3715 last_size = 0;
3716 phdr_index = i;
3717 phdr_in_segment = FALSE;
3720 /* Create a final PT_LOAD program segment. */
3721 if (last_hdr != NULL)
3723 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3724 if (m == NULL)
3725 goto error_return;
3727 *pm = m;
3728 pm = &m->next;
3731 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3732 if (dynsec != NULL)
3734 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3735 if (m == NULL)
3736 goto error_return;
3737 *pm = m;
3738 pm = &m->next;
3741 /* For each batch of consecutive loadable .note sections,
3742 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3743 because if we link together nonloadable .note sections and
3744 loadable .note sections, we will generate two .note sections
3745 in the output file. FIXME: Using names for section types is
3746 bogus anyhow. */
3747 for (s = abfd->sections; s != NULL; s = s->next)
3749 if ((s->flags & SEC_LOAD) != 0
3750 && CONST_STRNEQ (s->name, ".note"))
3752 asection *s2;
3753 unsigned count = 1;
3754 amt = sizeof (struct elf_segment_map);
3755 if (s->alignment_power == 2)
3756 for (s2 = s; s2->next != NULL; s2 = s2->next)
3758 if (s2->next->alignment_power == 2
3759 && (s2->next->flags & SEC_LOAD) != 0
3760 && CONST_STRNEQ (s2->next->name, ".note")
3761 && align_power (s2->vma + s2->size, 2)
3762 == s2->next->vma)
3763 count++;
3764 else
3765 break;
3767 amt += (count - 1) * sizeof (asection *);
3768 m = bfd_zalloc (abfd, amt);
3769 if (m == NULL)
3770 goto error_return;
3771 m->next = NULL;
3772 m->p_type = PT_NOTE;
3773 m->count = count;
3774 while (count > 1)
3776 m->sections[m->count - count--] = s;
3777 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3778 s = s->next;
3780 m->sections[m->count - 1] = s;
3781 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3782 *pm = m;
3783 pm = &m->next;
3785 if (s->flags & SEC_THREAD_LOCAL)
3787 if (! tls_count)
3788 first_tls = s;
3789 tls_count++;
3793 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3794 if (tls_count > 0)
3796 int i;
3798 amt = sizeof (struct elf_segment_map);
3799 amt += (tls_count - 1) * sizeof (asection *);
3800 m = bfd_zalloc (abfd, amt);
3801 if (m == NULL)
3802 goto error_return;
3803 m->next = NULL;
3804 m->p_type = PT_TLS;
3805 m->count = tls_count;
3806 /* Mandated PF_R. */
3807 m->p_flags = PF_R;
3808 m->p_flags_valid = 1;
3809 for (i = 0; i < tls_count; ++i)
3811 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3812 m->sections[i] = first_tls;
3813 first_tls = first_tls->next;
3816 *pm = m;
3817 pm = &m->next;
3820 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3821 segment. */
3822 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3823 if (eh_frame_hdr != NULL
3824 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3826 amt = sizeof (struct elf_segment_map);
3827 m = bfd_zalloc (abfd, amt);
3828 if (m == NULL)
3829 goto error_return;
3830 m->next = NULL;
3831 m->p_type = PT_GNU_EH_FRAME;
3832 m->count = 1;
3833 m->sections[0] = eh_frame_hdr->output_section;
3835 *pm = m;
3836 pm = &m->next;
3839 if (elf_tdata (abfd)->stack_flags)
3841 amt = sizeof (struct elf_segment_map);
3842 m = bfd_zalloc (abfd, amt);
3843 if (m == NULL)
3844 goto error_return;
3845 m->next = NULL;
3846 m->p_type = PT_GNU_STACK;
3847 m->p_flags = elf_tdata (abfd)->stack_flags;
3848 m->p_flags_valid = 1;
3850 *pm = m;
3851 pm = &m->next;
3854 if (dynsec != NULL && elf_tdata (abfd)->relro)
3856 /* We make a PT_GNU_RELRO segment only when there is a
3857 PT_DYNAMIC segment. */
3858 amt = sizeof (struct elf_segment_map);
3859 m = bfd_zalloc (abfd, amt);
3860 if (m == NULL)
3861 goto error_return;
3862 m->next = NULL;
3863 m->p_type = PT_GNU_RELRO;
3864 m->p_flags = PF_R;
3865 m->p_flags_valid = 1;
3867 *pm = m;
3868 pm = &m->next;
3871 free (sections);
3872 elf_tdata (abfd)->segment_map = mfirst;
3875 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
3876 return FALSE;
3878 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3879 ++count;
3880 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
3882 return TRUE;
3884 error_return:
3885 if (sections != NULL)
3886 free (sections);
3887 return FALSE;
3890 /* Sort sections by address. */
3892 static int
3893 elf_sort_sections (const void *arg1, const void *arg2)
3895 const asection *sec1 = *(const asection **) arg1;
3896 const asection *sec2 = *(const asection **) arg2;
3897 bfd_size_type size1, size2;
3899 /* Sort by LMA first, since this is the address used to
3900 place the section into a segment. */
3901 if (sec1->lma < sec2->lma)
3902 return -1;
3903 else if (sec1->lma > sec2->lma)
3904 return 1;
3906 /* Then sort by VMA. Normally the LMA and the VMA will be
3907 the same, and this will do nothing. */
3908 if (sec1->vma < sec2->vma)
3909 return -1;
3910 else if (sec1->vma > sec2->vma)
3911 return 1;
3913 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3915 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3917 if (TOEND (sec1))
3919 if (TOEND (sec2))
3921 /* If the indicies are the same, do not return 0
3922 here, but continue to try the next comparison. */
3923 if (sec1->target_index - sec2->target_index != 0)
3924 return sec1->target_index - sec2->target_index;
3926 else
3927 return 1;
3929 else if (TOEND (sec2))
3930 return -1;
3932 #undef TOEND
3934 /* Sort by size, to put zero sized sections
3935 before others at the same address. */
3937 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
3938 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
3940 if (size1 < size2)
3941 return -1;
3942 if (size1 > size2)
3943 return 1;
3945 return sec1->target_index - sec2->target_index;
3948 /* Ian Lance Taylor writes:
3950 We shouldn't be using % with a negative signed number. That's just
3951 not good. We have to make sure either that the number is not
3952 negative, or that the number has an unsigned type. When the types
3953 are all the same size they wind up as unsigned. When file_ptr is a
3954 larger signed type, the arithmetic winds up as signed long long,
3955 which is wrong.
3957 What we're trying to say here is something like ``increase OFF by
3958 the least amount that will cause it to be equal to the VMA modulo
3959 the page size.'' */
3960 /* In other words, something like:
3962 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3963 off_offset = off % bed->maxpagesize;
3964 if (vma_offset < off_offset)
3965 adjustment = vma_offset + bed->maxpagesize - off_offset;
3966 else
3967 adjustment = vma_offset - off_offset;
3969 which can can be collapsed into the expression below. */
3971 static file_ptr
3972 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
3974 return ((vma - off) % maxpagesize);
3977 static void
3978 print_segment_map (const struct elf_segment_map *m)
3980 unsigned int j;
3981 const char *pt = get_segment_type (m->p_type);
3982 char buf[32];
3984 if (pt == NULL)
3986 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
3987 sprintf (buf, "LOPROC+%7.7x",
3988 (unsigned int) (m->p_type - PT_LOPROC));
3989 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
3990 sprintf (buf, "LOOS+%7.7x",
3991 (unsigned int) (m->p_type - PT_LOOS));
3992 else
3993 snprintf (buf, sizeof (buf), "%8.8x",
3994 (unsigned int) m->p_type);
3995 pt = buf;
3997 fprintf (stderr, "%s:", pt);
3998 for (j = 0; j < m->count; j++)
3999 fprintf (stderr, " %s", m->sections [j]->name);
4000 putc ('\n',stderr);
4003 /* Assign file positions to the sections based on the mapping from
4004 sections to segments. This function also sets up some fields in
4005 the file header. */
4007 static bfd_boolean
4008 assign_file_positions_for_load_sections (bfd *abfd,
4009 struct bfd_link_info *link_info)
4011 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4012 struct elf_segment_map *m;
4013 Elf_Internal_Phdr *phdrs;
4014 Elf_Internal_Phdr *p;
4015 file_ptr off;
4016 bfd_size_type maxpagesize;
4017 unsigned int alloc;
4018 unsigned int i, j;
4020 if (link_info == NULL
4021 && !elf_modify_segment_map (abfd, link_info, FALSE))
4022 return FALSE;
4024 alloc = 0;
4025 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4026 ++alloc;
4028 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4029 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4030 elf_elfheader (abfd)->e_phnum = alloc;
4032 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4033 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4034 else
4035 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4036 >= alloc * bed->s->sizeof_phdr);
4038 if (alloc == 0)
4040 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4041 return TRUE;
4044 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
4045 elf_tdata (abfd)->phdr = phdrs;
4046 if (phdrs == NULL)
4047 return FALSE;
4049 maxpagesize = 1;
4050 if ((abfd->flags & D_PAGED) != 0)
4051 maxpagesize = bed->maxpagesize;
4053 off = bed->s->sizeof_ehdr;
4054 off += alloc * bed->s->sizeof_phdr;
4056 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4057 m != NULL;
4058 m = m->next, p++, j++)
4060 asection **secpp;
4061 bfd_vma off_adjust;
4062 bfd_boolean no_contents;
4064 /* If elf_segment_map is not from map_sections_to_segments, the
4065 sections may not be correctly ordered. NOTE: sorting should
4066 not be done to the PT_NOTE section of a corefile, which may
4067 contain several pseudo-sections artificially created by bfd.
4068 Sorting these pseudo-sections breaks things badly. */
4069 if (m->count > 1
4070 && !(elf_elfheader (abfd)->e_type == ET_CORE
4071 && m->p_type == PT_NOTE))
4072 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4073 elf_sort_sections);
4075 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4076 number of sections with contents contributing to both p_filesz
4077 and p_memsz, followed by a number of sections with no contents
4078 that just contribute to p_memsz. In this loop, OFF tracks next
4079 available file offset for PT_LOAD and PT_NOTE segments. */
4080 p->p_type = m->p_type;
4081 p->p_flags = m->p_flags;
4083 if (m->count == 0)
4084 p->p_vaddr = 0;
4085 else
4086 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4088 if (m->p_paddr_valid)
4089 p->p_paddr = m->p_paddr;
4090 else if (m->count == 0)
4091 p->p_paddr = 0;
4092 else
4093 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4095 if (p->p_type == PT_LOAD
4096 && (abfd->flags & D_PAGED) != 0)
4098 /* p_align in demand paged PT_LOAD segments effectively stores
4099 the maximum page size. When copying an executable with
4100 objcopy, we set m->p_align from the input file. Use this
4101 value for maxpagesize rather than bed->maxpagesize, which
4102 may be different. Note that we use maxpagesize for PT_TLS
4103 segment alignment later in this function, so we are relying
4104 on at least one PT_LOAD segment appearing before a PT_TLS
4105 segment. */
4106 if (m->p_align_valid)
4107 maxpagesize = m->p_align;
4109 p->p_align = maxpagesize;
4111 else if (m->count == 0)
4112 p->p_align = 1 << bed->s->log_file_align;
4113 else if (m->p_align_valid)
4114 p->p_align = m->p_align;
4115 else
4116 p->p_align = 0;
4118 no_contents = FALSE;
4119 off_adjust = 0;
4120 if (p->p_type == PT_LOAD
4121 && m->count > 0)
4123 bfd_size_type align;
4124 unsigned int align_power = 0;
4126 if (m->p_align_valid)
4127 align = p->p_align;
4128 else
4130 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4132 unsigned int secalign;
4134 secalign = bfd_get_section_alignment (abfd, *secpp);
4135 if (secalign > align_power)
4136 align_power = secalign;
4138 align = (bfd_size_type) 1 << align_power;
4139 if (align < maxpagesize)
4140 align = maxpagesize;
4143 for (i = 0; i < m->count; i++)
4144 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4145 /* If we aren't making room for this section, then
4146 it must be SHT_NOBITS regardless of what we've
4147 set via struct bfd_elf_special_section. */
4148 elf_section_type (m->sections[i]) = SHT_NOBITS;
4150 /* Find out whether this segment contains any loadable
4151 sections. If the first section isn't loadable, the same
4152 holds for any other sections. */
4153 i = 0;
4154 while (elf_section_type (m->sections[i]) == SHT_NOBITS)
4156 /* If a segment starts with .tbss, we need to look
4157 at the next section to decide whether the segment
4158 has any loadable sections. */
4159 if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
4160 || ++i >= m->count)
4162 no_contents = TRUE;
4163 break;
4167 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4168 off += off_adjust;
4169 if (no_contents)
4171 /* We shouldn't need to align the segment on disk since
4172 the segment doesn't need file space, but the gABI
4173 arguably requires the alignment and glibc ld.so
4174 checks it. So to comply with the alignment
4175 requirement but not waste file space, we adjust
4176 p_offset for just this segment. (OFF_ADJUST is
4177 subtracted from OFF later.) This may put p_offset
4178 past the end of file, but that shouldn't matter. */
4180 else
4181 off_adjust = 0;
4183 /* Make sure the .dynamic section is the first section in the
4184 PT_DYNAMIC segment. */
4185 else if (p->p_type == PT_DYNAMIC
4186 && m->count > 1
4187 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4189 _bfd_error_handler
4190 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4191 abfd);
4192 bfd_set_error (bfd_error_bad_value);
4193 return FALSE;
4196 p->p_offset = 0;
4197 p->p_filesz = 0;
4198 p->p_memsz = 0;
4200 if (m->includes_filehdr)
4202 if (!m->p_flags_valid)
4203 p->p_flags |= PF_R;
4204 p->p_filesz = bed->s->sizeof_ehdr;
4205 p->p_memsz = bed->s->sizeof_ehdr;
4206 if (m->count > 0)
4208 BFD_ASSERT (p->p_type == PT_LOAD);
4210 if (p->p_vaddr < (bfd_vma) off)
4212 (*_bfd_error_handler)
4213 (_("%B: Not enough room for program headers, try linking with -N"),
4214 abfd);
4215 bfd_set_error (bfd_error_bad_value);
4216 return FALSE;
4219 p->p_vaddr -= off;
4220 if (!m->p_paddr_valid)
4221 p->p_paddr -= off;
4225 if (m->includes_phdrs)
4227 if (!m->p_flags_valid)
4228 p->p_flags |= PF_R;
4230 if (!m->includes_filehdr)
4232 p->p_offset = bed->s->sizeof_ehdr;
4234 if (m->count > 0)
4236 BFD_ASSERT (p->p_type == PT_LOAD);
4237 p->p_vaddr -= off - p->p_offset;
4238 if (!m->p_paddr_valid)
4239 p->p_paddr -= off - p->p_offset;
4243 p->p_filesz += alloc * bed->s->sizeof_phdr;
4244 p->p_memsz += alloc * bed->s->sizeof_phdr;
4247 if (p->p_type == PT_LOAD
4248 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4250 if (!m->includes_filehdr && !m->includes_phdrs)
4251 p->p_offset = off;
4252 else
4254 file_ptr adjust;
4256 adjust = off - (p->p_offset + p->p_filesz);
4257 if (!no_contents)
4258 p->p_filesz += adjust;
4259 p->p_memsz += adjust;
4263 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4264 maps. Set filepos for sections in PT_LOAD segments, and in
4265 core files, for sections in PT_NOTE segments.
4266 assign_file_positions_for_non_load_sections will set filepos
4267 for other sections and update p_filesz for other segments. */
4268 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4270 asection *sec;
4271 bfd_size_type align;
4272 Elf_Internal_Shdr *this_hdr;
4274 sec = *secpp;
4275 this_hdr = &elf_section_data (sec)->this_hdr;
4276 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4278 if (p->p_type == PT_LOAD
4279 || p->p_type == PT_TLS)
4281 bfd_signed_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
4283 if (this_hdr->sh_type != SHT_NOBITS
4284 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4285 && ((this_hdr->sh_flags & SHF_TLS) == 0
4286 || p->p_type == PT_TLS)))
4288 if (adjust < 0)
4290 (*_bfd_error_handler)
4291 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4292 abfd, sec, (unsigned long) sec->lma);
4293 adjust = 0;
4295 p->p_memsz += adjust;
4297 if (this_hdr->sh_type != SHT_NOBITS)
4299 off += adjust;
4300 p->p_filesz += adjust;
4305 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4307 /* The section at i == 0 is the one that actually contains
4308 everything. */
4309 if (i == 0)
4311 this_hdr->sh_offset = sec->filepos = off;
4312 off += this_hdr->sh_size;
4313 p->p_filesz = this_hdr->sh_size;
4314 p->p_memsz = 0;
4315 p->p_align = 1;
4317 else
4319 /* The rest are fake sections that shouldn't be written. */
4320 sec->filepos = 0;
4321 sec->size = 0;
4322 sec->flags = 0;
4323 continue;
4326 else
4328 if (p->p_type == PT_LOAD)
4330 this_hdr->sh_offset = sec->filepos = off;
4331 if (this_hdr->sh_type != SHT_NOBITS)
4332 off += this_hdr->sh_size;
4335 if (this_hdr->sh_type != SHT_NOBITS)
4337 p->p_filesz += this_hdr->sh_size;
4338 /* A load section without SHF_ALLOC is something like
4339 a note section in a PT_NOTE segment. These take
4340 file space but are not loaded into memory. */
4341 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4342 p->p_memsz += this_hdr->sh_size;
4344 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4346 if (p->p_type == PT_TLS)
4347 p->p_memsz += this_hdr->sh_size;
4349 /* .tbss is special. It doesn't contribute to p_memsz of
4350 normal segments. */
4351 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4352 p->p_memsz += this_hdr->sh_size;
4355 if (p->p_type == PT_GNU_RELRO)
4356 p->p_align = 1;
4357 else if (align > p->p_align
4358 && !m->p_align_valid
4359 && (p->p_type != PT_LOAD
4360 || (abfd->flags & D_PAGED) == 0))
4361 p->p_align = align;
4364 if (!m->p_flags_valid)
4366 p->p_flags |= PF_R;
4367 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4368 p->p_flags |= PF_X;
4369 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4370 p->p_flags |= PF_W;
4373 off -= off_adjust;
4375 /* Check that all sections are in a PT_LOAD segment.
4376 Don't check funky gdb generated core files. */
4377 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4378 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4380 Elf_Internal_Shdr *this_hdr;
4381 asection *sec;
4383 sec = *secpp;
4384 this_hdr = &(elf_section_data(sec)->this_hdr);
4385 if (this_hdr->sh_size != 0
4386 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4388 (*_bfd_error_handler)
4389 (_("%B: section `%A' can't be allocated in segment %d"),
4390 abfd, sec, j);
4391 print_segment_map (m);
4392 bfd_set_error (bfd_error_bad_value);
4393 return FALSE;
4398 elf_tdata (abfd)->next_file_pos = off;
4399 return TRUE;
4402 /* Assign file positions for the other sections. */
4404 static bfd_boolean
4405 assign_file_positions_for_non_load_sections (bfd *abfd,
4406 struct bfd_link_info *link_info)
4408 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4409 Elf_Internal_Shdr **i_shdrpp;
4410 Elf_Internal_Shdr **hdrpp;
4411 Elf_Internal_Phdr *phdrs;
4412 Elf_Internal_Phdr *p;
4413 struct elf_segment_map *m;
4414 bfd_vma filehdr_vaddr, filehdr_paddr;
4415 bfd_vma phdrs_vaddr, phdrs_paddr;
4416 file_ptr off;
4417 unsigned int num_sec;
4418 unsigned int i;
4419 unsigned int count;
4421 i_shdrpp = elf_elfsections (abfd);
4422 num_sec = elf_numsections (abfd);
4423 off = elf_tdata (abfd)->next_file_pos;
4424 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4426 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4427 Elf_Internal_Shdr *hdr;
4429 hdr = *hdrpp;
4430 if (hdr->bfd_section != NULL
4431 && (hdr->bfd_section->filepos != 0
4432 || (hdr->sh_type == SHT_NOBITS
4433 && hdr->contents == NULL)))
4434 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4435 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4437 if (hdr->sh_size != 0)
4438 ((*_bfd_error_handler)
4439 (_("%B: warning: allocated section `%s' not in segment"),
4440 abfd,
4441 (hdr->bfd_section == NULL
4442 ? "*unknown*"
4443 : hdr->bfd_section->name)));
4444 /* We don't need to page align empty sections. */
4445 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4446 off += vma_page_aligned_bias (hdr->sh_addr, off,
4447 bed->maxpagesize);
4448 else
4449 off += vma_page_aligned_bias (hdr->sh_addr, off,
4450 hdr->sh_addralign);
4451 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4452 FALSE);
4454 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4455 && hdr->bfd_section == NULL)
4456 || hdr == i_shdrpp[tdata->symtab_section]
4457 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4458 || hdr == i_shdrpp[tdata->strtab_section])
4459 hdr->sh_offset = -1;
4460 else
4461 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4463 if (i == SHN_LORESERVE - 1)
4465 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4466 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4470 /* Now that we have set the section file positions, we can set up
4471 the file positions for the non PT_LOAD segments. */
4472 count = 0;
4473 filehdr_vaddr = 0;
4474 filehdr_paddr = 0;
4475 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4476 phdrs_paddr = 0;
4477 phdrs = elf_tdata (abfd)->phdr;
4478 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4479 m != NULL;
4480 m = m->next, p++)
4482 ++count;
4483 if (p->p_type != PT_LOAD)
4484 continue;
4486 if (m->includes_filehdr)
4488 filehdr_vaddr = p->p_vaddr;
4489 filehdr_paddr = p->p_paddr;
4491 if (m->includes_phdrs)
4493 phdrs_vaddr = p->p_vaddr;
4494 phdrs_paddr = p->p_paddr;
4495 if (m->includes_filehdr)
4497 phdrs_vaddr += bed->s->sizeof_ehdr;
4498 phdrs_paddr += bed->s->sizeof_ehdr;
4503 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4504 m != NULL;
4505 m = m->next, p++)
4507 if (m->count != 0)
4509 if (p->p_type != PT_LOAD
4510 && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core))
4512 Elf_Internal_Shdr *hdr;
4513 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4515 hdr = &elf_section_data (m->sections[m->count - 1])->this_hdr;
4516 p->p_filesz = (m->sections[m->count - 1]->filepos
4517 - m->sections[0]->filepos);
4518 if (hdr->sh_type != SHT_NOBITS)
4519 p->p_filesz += hdr->sh_size;
4521 p->p_offset = m->sections[0]->filepos;
4524 else
4526 if (m->includes_filehdr)
4528 p->p_vaddr = filehdr_vaddr;
4529 if (! m->p_paddr_valid)
4530 p->p_paddr = filehdr_paddr;
4532 else if (m->includes_phdrs)
4534 p->p_vaddr = phdrs_vaddr;
4535 if (! m->p_paddr_valid)
4536 p->p_paddr = phdrs_paddr;
4538 else if (p->p_type == PT_GNU_RELRO)
4540 Elf_Internal_Phdr *lp;
4542 for (lp = phdrs; lp < phdrs + count; ++lp)
4544 if (lp->p_type == PT_LOAD
4545 && lp->p_vaddr <= link_info->relro_end
4546 && lp->p_vaddr >= link_info->relro_start
4547 && (lp->p_vaddr + lp->p_filesz
4548 >= link_info->relro_end))
4549 break;
4552 if (lp < phdrs + count
4553 && link_info->relro_end > lp->p_vaddr)
4555 p->p_vaddr = lp->p_vaddr;
4556 p->p_paddr = lp->p_paddr;
4557 p->p_offset = lp->p_offset;
4558 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4559 p->p_memsz = p->p_filesz;
4560 p->p_align = 1;
4561 p->p_flags = (lp->p_flags & ~PF_W);
4563 else
4565 memset (p, 0, sizeof *p);
4566 p->p_type = PT_NULL;
4572 elf_tdata (abfd)->next_file_pos = off;
4574 return TRUE;
4577 /* Work out the file positions of all the sections. This is called by
4578 _bfd_elf_compute_section_file_positions. All the section sizes and
4579 VMAs must be known before this is called.
4581 Reloc sections come in two flavours: Those processed specially as
4582 "side-channel" data attached to a section to which they apply, and
4583 those that bfd doesn't process as relocations. The latter sort are
4584 stored in a normal bfd section by bfd_section_from_shdr. We don't
4585 consider the former sort here, unless they form part of the loadable
4586 image. Reloc sections not assigned here will be handled later by
4587 assign_file_positions_for_relocs.
4589 We also don't set the positions of the .symtab and .strtab here. */
4591 static bfd_boolean
4592 assign_file_positions_except_relocs (bfd *abfd,
4593 struct bfd_link_info *link_info)
4595 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4596 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4597 file_ptr off;
4598 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4600 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4601 && bfd_get_format (abfd) != bfd_core)
4603 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4604 unsigned int num_sec = elf_numsections (abfd);
4605 Elf_Internal_Shdr **hdrpp;
4606 unsigned int i;
4608 /* Start after the ELF header. */
4609 off = i_ehdrp->e_ehsize;
4611 /* We are not creating an executable, which means that we are
4612 not creating a program header, and that the actual order of
4613 the sections in the file is unimportant. */
4614 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4616 Elf_Internal_Shdr *hdr;
4618 hdr = *hdrpp;
4619 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4620 && hdr->bfd_section == NULL)
4621 || i == tdata->symtab_section
4622 || i == tdata->symtab_shndx_section
4623 || i == tdata->strtab_section)
4625 hdr->sh_offset = -1;
4627 else
4628 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4630 if (i == SHN_LORESERVE - 1)
4632 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4633 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4637 else
4639 unsigned int alloc;
4641 /* Assign file positions for the loaded sections based on the
4642 assignment of sections to segments. */
4643 if (!assign_file_positions_for_load_sections (abfd, link_info))
4644 return FALSE;
4646 /* And for non-load sections. */
4647 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4648 return FALSE;
4650 if (bed->elf_backend_modify_program_headers != NULL)
4652 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4653 return FALSE;
4656 /* Write out the program headers. */
4657 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4658 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4659 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4660 return FALSE;
4662 off = tdata->next_file_pos;
4665 /* Place the section headers. */
4666 off = align_file_position (off, 1 << bed->s->log_file_align);
4667 i_ehdrp->e_shoff = off;
4668 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4670 tdata->next_file_pos = off;
4672 return TRUE;
4675 static bfd_boolean
4676 prep_headers (bfd *abfd)
4678 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4679 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4680 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4681 struct elf_strtab_hash *shstrtab;
4682 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4684 i_ehdrp = elf_elfheader (abfd);
4685 i_shdrp = elf_elfsections (abfd);
4687 shstrtab = _bfd_elf_strtab_init ();
4688 if (shstrtab == NULL)
4689 return FALSE;
4691 elf_shstrtab (abfd) = shstrtab;
4693 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4694 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4695 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4696 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4698 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4699 i_ehdrp->e_ident[EI_DATA] =
4700 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4701 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4703 if ((abfd->flags & DYNAMIC) != 0)
4704 i_ehdrp->e_type = ET_DYN;
4705 else if ((abfd->flags & EXEC_P) != 0)
4706 i_ehdrp->e_type = ET_EXEC;
4707 else if (bfd_get_format (abfd) == bfd_core)
4708 i_ehdrp->e_type = ET_CORE;
4709 else
4710 i_ehdrp->e_type = ET_REL;
4712 switch (bfd_get_arch (abfd))
4714 case bfd_arch_unknown:
4715 i_ehdrp->e_machine = EM_NONE;
4716 break;
4718 /* There used to be a long list of cases here, each one setting
4719 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4720 in the corresponding bfd definition. To avoid duplication,
4721 the switch was removed. Machines that need special handling
4722 can generally do it in elf_backend_final_write_processing(),
4723 unless they need the information earlier than the final write.
4724 Such need can generally be supplied by replacing the tests for
4725 e_machine with the conditions used to determine it. */
4726 default:
4727 i_ehdrp->e_machine = bed->elf_machine_code;
4730 i_ehdrp->e_version = bed->s->ev_current;
4731 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4733 /* No program header, for now. */
4734 i_ehdrp->e_phoff = 0;
4735 i_ehdrp->e_phentsize = 0;
4736 i_ehdrp->e_phnum = 0;
4738 /* Each bfd section is section header entry. */
4739 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4740 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4742 /* If we're building an executable, we'll need a program header table. */
4743 if (abfd->flags & EXEC_P)
4744 /* It all happens later. */
4746 else
4748 i_ehdrp->e_phentsize = 0;
4749 i_phdrp = 0;
4750 i_ehdrp->e_phoff = 0;
4753 elf_tdata (abfd)->symtab_hdr.sh_name =
4754 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4755 elf_tdata (abfd)->strtab_hdr.sh_name =
4756 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4757 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4758 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4759 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4760 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4761 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4762 return FALSE;
4764 return TRUE;
4767 /* Assign file positions for all the reloc sections which are not part
4768 of the loadable file image. */
4770 void
4771 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4773 file_ptr off;
4774 unsigned int i, num_sec;
4775 Elf_Internal_Shdr **shdrpp;
4777 off = elf_tdata (abfd)->next_file_pos;
4779 num_sec = elf_numsections (abfd);
4780 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4782 Elf_Internal_Shdr *shdrp;
4784 shdrp = *shdrpp;
4785 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4786 && shdrp->sh_offset == -1)
4787 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4790 elf_tdata (abfd)->next_file_pos = off;
4793 bfd_boolean
4794 _bfd_elf_write_object_contents (bfd *abfd)
4796 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4797 Elf_Internal_Ehdr *i_ehdrp;
4798 Elf_Internal_Shdr **i_shdrp;
4799 bfd_boolean failed;
4800 unsigned int count, num_sec;
4802 if (! abfd->output_has_begun
4803 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4804 return FALSE;
4806 i_shdrp = elf_elfsections (abfd);
4807 i_ehdrp = elf_elfheader (abfd);
4809 failed = FALSE;
4810 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4811 if (failed)
4812 return FALSE;
4814 _bfd_elf_assign_file_positions_for_relocs (abfd);
4816 /* After writing the headers, we need to write the sections too... */
4817 num_sec = elf_numsections (abfd);
4818 for (count = 1; count < num_sec; count++)
4820 if (bed->elf_backend_section_processing)
4821 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4822 if (i_shdrp[count]->contents)
4824 bfd_size_type amt = i_shdrp[count]->sh_size;
4826 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4827 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4828 return FALSE;
4830 if (count == SHN_LORESERVE - 1)
4831 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4834 /* Write out the section header names. */
4835 if (elf_shstrtab (abfd) != NULL
4836 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4837 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4838 return FALSE;
4840 if (bed->elf_backend_final_write_processing)
4841 (*bed->elf_backend_final_write_processing) (abfd,
4842 elf_tdata (abfd)->linker);
4844 if (!bed->s->write_shdrs_and_ehdr (abfd))
4845 return FALSE;
4847 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4848 if (elf_tdata (abfd)->after_write_object_contents)
4849 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
4851 return TRUE;
4854 bfd_boolean
4855 _bfd_elf_write_corefile_contents (bfd *abfd)
4857 /* Hopefully this can be done just like an object file. */
4858 return _bfd_elf_write_object_contents (abfd);
4861 /* Given a section, search the header to find them. */
4864 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4866 const struct elf_backend_data *bed;
4867 int index;
4869 if (elf_section_data (asect) != NULL
4870 && elf_section_data (asect)->this_idx != 0)
4871 return elf_section_data (asect)->this_idx;
4873 if (bfd_is_abs_section (asect))
4874 index = SHN_ABS;
4875 else if (bfd_is_com_section (asect))
4876 index = SHN_COMMON;
4877 else if (bfd_is_und_section (asect))
4878 index = SHN_UNDEF;
4879 else
4880 index = -1;
4882 bed = get_elf_backend_data (abfd);
4883 if (bed->elf_backend_section_from_bfd_section)
4885 int retval = index;
4887 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4888 return retval;
4891 if (index == -1)
4892 bfd_set_error (bfd_error_nonrepresentable_section);
4894 return index;
4897 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4898 on error. */
4901 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4903 asymbol *asym_ptr = *asym_ptr_ptr;
4904 int idx;
4905 flagword flags = asym_ptr->flags;
4907 /* When gas creates relocations against local labels, it creates its
4908 own symbol for the section, but does put the symbol into the
4909 symbol chain, so udata is 0. When the linker is generating
4910 relocatable output, this section symbol may be for one of the
4911 input sections rather than the output section. */
4912 if (asym_ptr->udata.i == 0
4913 && (flags & BSF_SECTION_SYM)
4914 && asym_ptr->section)
4916 asection *sec;
4917 int indx;
4919 sec = asym_ptr->section;
4920 if (sec->owner != abfd && sec->output_section != NULL)
4921 sec = sec->output_section;
4922 if (sec->owner == abfd
4923 && (indx = sec->index) < elf_num_section_syms (abfd)
4924 && elf_section_syms (abfd)[indx] != NULL)
4925 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4928 idx = asym_ptr->udata.i;
4930 if (idx == 0)
4932 /* This case can occur when using --strip-symbol on a symbol
4933 which is used in a relocation entry. */
4934 (*_bfd_error_handler)
4935 (_("%B: symbol `%s' required but not present"),
4936 abfd, bfd_asymbol_name (asym_ptr));
4937 bfd_set_error (bfd_error_no_symbols);
4938 return -1;
4941 #if DEBUG & 4
4943 fprintf (stderr,
4944 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4945 (long) asym_ptr, asym_ptr->name, idx, flags,
4946 elf_symbol_flags (flags));
4947 fflush (stderr);
4949 #endif
4951 return idx;
4954 /* Rewrite program header information. */
4956 static bfd_boolean
4957 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
4959 Elf_Internal_Ehdr *iehdr;
4960 struct elf_segment_map *map;
4961 struct elf_segment_map *map_first;
4962 struct elf_segment_map **pointer_to_map;
4963 Elf_Internal_Phdr *segment;
4964 asection *section;
4965 unsigned int i;
4966 unsigned int num_segments;
4967 bfd_boolean phdr_included = FALSE;
4968 bfd_vma maxpagesize;
4969 struct elf_segment_map *phdr_adjust_seg = NULL;
4970 unsigned int phdr_adjust_num = 0;
4971 const struct elf_backend_data *bed;
4973 bed = get_elf_backend_data (ibfd);
4974 iehdr = elf_elfheader (ibfd);
4976 map_first = NULL;
4977 pointer_to_map = &map_first;
4979 num_segments = elf_elfheader (ibfd)->e_phnum;
4980 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4982 /* Returns the end address of the segment + 1. */
4983 #define SEGMENT_END(segment, start) \
4984 (start + (segment->p_memsz > segment->p_filesz \
4985 ? segment->p_memsz : segment->p_filesz))
4987 #define SECTION_SIZE(section, segment) \
4988 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4989 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4990 ? section->size : 0)
4992 /* Returns TRUE if the given section is contained within
4993 the given segment. VMA addresses are compared. */
4994 #define IS_CONTAINED_BY_VMA(section, segment) \
4995 (section->vma >= segment->p_vaddr \
4996 && (section->vma + SECTION_SIZE (section, segment) \
4997 <= (SEGMENT_END (segment, segment->p_vaddr))))
4999 /* Returns TRUE if the given section is contained within
5000 the given segment. LMA addresses are compared. */
5001 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5002 (section->lma >= base \
5003 && (section->lma + SECTION_SIZE (section, segment) \
5004 <= SEGMENT_END (segment, base)))
5006 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
5007 #define IS_COREFILE_NOTE(p, s) \
5008 (p->p_type == PT_NOTE \
5009 && bfd_get_format (ibfd) == bfd_core \
5010 && s->vma == 0 && s->lma == 0 \
5011 && (bfd_vma) s->filepos >= p->p_offset \
5012 && ((bfd_vma) s->filepos + s->size \
5013 <= p->p_offset + p->p_filesz))
5015 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5016 linker, which generates a PT_INTERP section with p_vaddr and
5017 p_memsz set to 0. */
5018 #define IS_SOLARIS_PT_INTERP(p, s) \
5019 (p->p_vaddr == 0 \
5020 && p->p_paddr == 0 \
5021 && p->p_memsz == 0 \
5022 && p->p_filesz > 0 \
5023 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5024 && s->size > 0 \
5025 && (bfd_vma) s->filepos >= p->p_offset \
5026 && ((bfd_vma) s->filepos + s->size \
5027 <= p->p_offset + p->p_filesz))
5029 /* Decide if the given section should be included in the given segment.
5030 A section will be included if:
5031 1. It is within the address space of the segment -- we use the LMA
5032 if that is set for the segment and the VMA otherwise,
5033 2. It is an allocated segment,
5034 3. There is an output section associated with it,
5035 4. The section has not already been allocated to a previous segment.
5036 5. PT_GNU_STACK segments do not include any sections.
5037 6. PT_TLS segment includes only SHF_TLS sections.
5038 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5039 8. PT_DYNAMIC should not contain empty sections at the beginning
5040 (with the possible exception of .dynamic). */
5041 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5042 ((((segment->p_paddr \
5043 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5044 : IS_CONTAINED_BY_VMA (section, segment)) \
5045 && (section->flags & SEC_ALLOC) != 0) \
5046 || IS_COREFILE_NOTE (segment, section)) \
5047 && segment->p_type != PT_GNU_STACK \
5048 && (segment->p_type != PT_TLS \
5049 || (section->flags & SEC_THREAD_LOCAL)) \
5050 && (segment->p_type == PT_LOAD \
5051 || segment->p_type == PT_TLS \
5052 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5053 && (segment->p_type != PT_DYNAMIC \
5054 || SECTION_SIZE (section, segment) > 0 \
5055 || (segment->p_paddr \
5056 ? segment->p_paddr != section->lma \
5057 : segment->p_vaddr != section->vma) \
5058 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5059 == 0)) \
5060 && ! section->segment_mark)
5062 /* If the output section of a section in the input segment is NULL,
5063 it is removed from the corresponding output segment. */
5064 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5065 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5066 && section->output_section != NULL)
5068 /* Returns TRUE iff seg1 starts after the end of seg2. */
5069 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5070 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5072 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5073 their VMA address ranges and their LMA address ranges overlap.
5074 It is possible to have overlapping VMA ranges without overlapping LMA
5075 ranges. RedBoot images for example can have both .data and .bss mapped
5076 to the same VMA range, but with the .data section mapped to a different
5077 LMA. */
5078 #define SEGMENT_OVERLAPS(seg1, seg2) \
5079 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5080 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5081 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5082 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5084 /* Initialise the segment mark field. */
5085 for (section = ibfd->sections; section != NULL; section = section->next)
5086 section->segment_mark = FALSE;
5088 /* Scan through the segments specified in the program header
5089 of the input BFD. For this first scan we look for overlaps
5090 in the loadable segments. These can be created by weird
5091 parameters to objcopy. Also, fix some solaris weirdness. */
5092 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5093 i < num_segments;
5094 i++, segment++)
5096 unsigned int j;
5097 Elf_Internal_Phdr *segment2;
5099 if (segment->p_type == PT_INTERP)
5100 for (section = ibfd->sections; section; section = section->next)
5101 if (IS_SOLARIS_PT_INTERP (segment, section))
5103 /* Mininal change so that the normal section to segment
5104 assignment code will work. */
5105 segment->p_vaddr = section->vma;
5106 break;
5109 if (segment->p_type != PT_LOAD)
5110 continue;
5112 /* Determine if this segment overlaps any previous segments. */
5113 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
5115 bfd_signed_vma extra_length;
5117 if (segment2->p_type != PT_LOAD
5118 || ! SEGMENT_OVERLAPS (segment, segment2))
5119 continue;
5121 /* Merge the two segments together. */
5122 if (segment2->p_vaddr < segment->p_vaddr)
5124 /* Extend SEGMENT2 to include SEGMENT and then delete
5125 SEGMENT. */
5126 extra_length =
5127 SEGMENT_END (segment, segment->p_vaddr)
5128 - SEGMENT_END (segment2, segment2->p_vaddr);
5130 if (extra_length > 0)
5132 segment2->p_memsz += extra_length;
5133 segment2->p_filesz += extra_length;
5136 segment->p_type = PT_NULL;
5138 /* Since we have deleted P we must restart the outer loop. */
5139 i = 0;
5140 segment = elf_tdata (ibfd)->phdr;
5141 break;
5143 else
5145 /* Extend SEGMENT to include SEGMENT2 and then delete
5146 SEGMENT2. */
5147 extra_length =
5148 SEGMENT_END (segment2, segment2->p_vaddr)
5149 - SEGMENT_END (segment, segment->p_vaddr);
5151 if (extra_length > 0)
5153 segment->p_memsz += extra_length;
5154 segment->p_filesz += extra_length;
5157 segment2->p_type = PT_NULL;
5162 /* The second scan attempts to assign sections to segments. */
5163 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5164 i < num_segments;
5165 i ++, segment ++)
5167 unsigned int section_count;
5168 asection ** sections;
5169 asection * output_section;
5170 unsigned int isec;
5171 bfd_vma matching_lma;
5172 bfd_vma suggested_lma;
5173 unsigned int j;
5174 bfd_size_type amt;
5175 asection * first_section;
5177 if (segment->p_type == PT_NULL)
5178 continue;
5180 first_section = NULL;
5181 /* Compute how many sections might be placed into this segment. */
5182 for (section = ibfd->sections, section_count = 0;
5183 section != NULL;
5184 section = section->next)
5186 /* Find the first section in the input segment, which may be
5187 removed from the corresponding output segment. */
5188 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5190 if (first_section == NULL)
5191 first_section = section;
5192 if (section->output_section != NULL)
5193 ++section_count;
5197 /* Allocate a segment map big enough to contain
5198 all of the sections we have selected. */
5199 amt = sizeof (struct elf_segment_map);
5200 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5201 map = bfd_zalloc (obfd, amt);
5202 if (map == NULL)
5203 return FALSE;
5205 /* Initialise the fields of the segment map. Default to
5206 using the physical address of the segment in the input BFD. */
5207 map->next = NULL;
5208 map->p_type = segment->p_type;
5209 map->p_flags = segment->p_flags;
5210 map->p_flags_valid = 1;
5212 /* If the first section in the input segment is removed, there is
5213 no need to preserve segment physical address in the corresponding
5214 output segment. */
5215 if (!first_section || first_section->output_section != NULL)
5217 map->p_paddr = segment->p_paddr;
5218 map->p_paddr_valid = 1;
5221 /* Determine if this segment contains the ELF file header
5222 and if it contains the program headers themselves. */
5223 map->includes_filehdr = (segment->p_offset == 0
5224 && segment->p_filesz >= iehdr->e_ehsize);
5226 map->includes_phdrs = 0;
5228 if (! phdr_included || segment->p_type != PT_LOAD)
5230 map->includes_phdrs =
5231 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5232 && (segment->p_offset + segment->p_filesz
5233 >= ((bfd_vma) iehdr->e_phoff
5234 + iehdr->e_phnum * iehdr->e_phentsize)));
5236 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5237 phdr_included = TRUE;
5240 if (section_count == 0)
5242 /* Special segments, such as the PT_PHDR segment, may contain
5243 no sections, but ordinary, loadable segments should contain
5244 something. They are allowed by the ELF spec however, so only
5245 a warning is produced. */
5246 if (segment->p_type == PT_LOAD)
5247 (*_bfd_error_handler)
5248 (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"),
5249 ibfd);
5251 map->count = 0;
5252 *pointer_to_map = map;
5253 pointer_to_map = &map->next;
5255 continue;
5258 /* Now scan the sections in the input BFD again and attempt
5259 to add their corresponding output sections to the segment map.
5260 The problem here is how to handle an output section which has
5261 been moved (ie had its LMA changed). There are four possibilities:
5263 1. None of the sections have been moved.
5264 In this case we can continue to use the segment LMA from the
5265 input BFD.
5267 2. All of the sections have been moved by the same amount.
5268 In this case we can change the segment's LMA to match the LMA
5269 of the first section.
5271 3. Some of the sections have been moved, others have not.
5272 In this case those sections which have not been moved can be
5273 placed in the current segment which will have to have its size,
5274 and possibly its LMA changed, and a new segment or segments will
5275 have to be created to contain the other sections.
5277 4. The sections have been moved, but not by the same amount.
5278 In this case we can change the segment's LMA to match the LMA
5279 of the first section and we will have to create a new segment
5280 or segments to contain the other sections.
5282 In order to save time, we allocate an array to hold the section
5283 pointers that we are interested in. As these sections get assigned
5284 to a segment, they are removed from this array. */
5286 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5287 to work around this long long bug. */
5288 sections = bfd_malloc2 (section_count, sizeof (asection *));
5289 if (sections == NULL)
5290 return FALSE;
5292 /* Step One: Scan for segment vs section LMA conflicts.
5293 Also add the sections to the section array allocated above.
5294 Also add the sections to the current segment. In the common
5295 case, where the sections have not been moved, this means that
5296 we have completely filled the segment, and there is nothing
5297 more to do. */
5298 isec = 0;
5299 matching_lma = 0;
5300 suggested_lma = 0;
5302 for (j = 0, section = ibfd->sections;
5303 section != NULL;
5304 section = section->next)
5306 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5308 output_section = section->output_section;
5310 sections[j ++] = section;
5312 /* The Solaris native linker always sets p_paddr to 0.
5313 We try to catch that case here, and set it to the
5314 correct value. Note - some backends require that
5315 p_paddr be left as zero. */
5316 if (segment->p_paddr == 0
5317 && segment->p_vaddr != 0
5318 && (! bed->want_p_paddr_set_to_zero)
5319 && isec == 0
5320 && output_section->lma != 0
5321 && (output_section->vma == (segment->p_vaddr
5322 + (map->includes_filehdr
5323 ? iehdr->e_ehsize
5324 : 0)
5325 + (map->includes_phdrs
5326 ? (iehdr->e_phnum
5327 * iehdr->e_phentsize)
5328 : 0))))
5329 map->p_paddr = segment->p_vaddr;
5331 /* Match up the physical address of the segment with the
5332 LMA address of the output section. */
5333 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5334 || IS_COREFILE_NOTE (segment, section)
5335 || (bed->want_p_paddr_set_to_zero &&
5336 IS_CONTAINED_BY_VMA (output_section, segment)))
5338 if (matching_lma == 0 || output_section->lma < matching_lma)
5339 matching_lma = output_section->lma;
5341 /* We assume that if the section fits within the segment
5342 then it does not overlap any other section within that
5343 segment. */
5344 map->sections[isec ++] = output_section;
5346 else if (suggested_lma == 0)
5347 suggested_lma = output_section->lma;
5351 BFD_ASSERT (j == section_count);
5353 /* Step Two: Adjust the physical address of the current segment,
5354 if necessary. */
5355 if (isec == section_count)
5357 /* All of the sections fitted within the segment as currently
5358 specified. This is the default case. Add the segment to
5359 the list of built segments and carry on to process the next
5360 program header in the input BFD. */
5361 map->count = section_count;
5362 *pointer_to_map = map;
5363 pointer_to_map = &map->next;
5365 if (matching_lma != map->p_paddr
5366 && !map->includes_filehdr && !map->includes_phdrs)
5367 /* There is some padding before the first section in the
5368 segment. So, we must account for that in the output
5369 segment's vma. */
5370 map->p_vaddr_offset = matching_lma - map->p_paddr;
5372 free (sections);
5373 continue;
5375 else
5377 if (matching_lma != 0)
5379 /* At least one section fits inside the current segment.
5380 Keep it, but modify its physical address to match the
5381 LMA of the first section that fitted. */
5382 map->p_paddr = matching_lma;
5384 else
5386 /* None of the sections fitted inside the current segment.
5387 Change the current segment's physical address to match
5388 the LMA of the first section. */
5389 map->p_paddr = suggested_lma;
5392 /* Offset the segment physical address from the lma
5393 to allow for space taken up by elf headers. */
5394 if (map->includes_filehdr)
5395 map->p_paddr -= iehdr->e_ehsize;
5397 if (map->includes_phdrs)
5399 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5401 /* iehdr->e_phnum is just an estimate of the number
5402 of program headers that we will need. Make a note
5403 here of the number we used and the segment we chose
5404 to hold these headers, so that we can adjust the
5405 offset when we know the correct value. */
5406 phdr_adjust_num = iehdr->e_phnum;
5407 phdr_adjust_seg = map;
5411 /* Step Three: Loop over the sections again, this time assigning
5412 those that fit to the current segment and removing them from the
5413 sections array; but making sure not to leave large gaps. Once all
5414 possible sections have been assigned to the current segment it is
5415 added to the list of built segments and if sections still remain
5416 to be assigned, a new segment is constructed before repeating
5417 the loop. */
5418 isec = 0;
5421 map->count = 0;
5422 suggested_lma = 0;
5424 /* Fill the current segment with sections that fit. */
5425 for (j = 0; j < section_count; j++)
5427 section = sections[j];
5429 if (section == NULL)
5430 continue;
5432 output_section = section->output_section;
5434 BFD_ASSERT (output_section != NULL);
5436 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5437 || IS_COREFILE_NOTE (segment, section))
5439 if (map->count == 0)
5441 /* If the first section in a segment does not start at
5442 the beginning of the segment, then something is
5443 wrong. */
5444 if (output_section->lma !=
5445 (map->p_paddr
5446 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5447 + (map->includes_phdrs
5448 ? iehdr->e_phnum * iehdr->e_phentsize
5449 : 0)))
5450 abort ();
5452 else
5454 asection * prev_sec;
5456 prev_sec = map->sections[map->count - 1];
5458 /* If the gap between the end of the previous section
5459 and the start of this section is more than
5460 maxpagesize then we need to start a new segment. */
5461 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5462 maxpagesize)
5463 < BFD_ALIGN (output_section->lma, maxpagesize))
5464 || ((prev_sec->lma + prev_sec->size)
5465 > output_section->lma))
5467 if (suggested_lma == 0)
5468 suggested_lma = output_section->lma;
5470 continue;
5474 map->sections[map->count++] = output_section;
5475 ++isec;
5476 sections[j] = NULL;
5477 section->segment_mark = TRUE;
5479 else if (suggested_lma == 0)
5480 suggested_lma = output_section->lma;
5483 BFD_ASSERT (map->count > 0);
5485 /* Add the current segment to the list of built segments. */
5486 *pointer_to_map = map;
5487 pointer_to_map = &map->next;
5489 if (isec < section_count)
5491 /* We still have not allocated all of the sections to
5492 segments. Create a new segment here, initialise it
5493 and carry on looping. */
5494 amt = sizeof (struct elf_segment_map);
5495 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5496 map = bfd_alloc (obfd, amt);
5497 if (map == NULL)
5499 free (sections);
5500 return FALSE;
5503 /* Initialise the fields of the segment map. Set the physical
5504 physical address to the LMA of the first section that has
5505 not yet been assigned. */
5506 map->next = NULL;
5507 map->p_type = segment->p_type;
5508 map->p_flags = segment->p_flags;
5509 map->p_flags_valid = 1;
5510 map->p_paddr = suggested_lma;
5511 map->p_paddr_valid = 1;
5512 map->includes_filehdr = 0;
5513 map->includes_phdrs = 0;
5516 while (isec < section_count);
5518 free (sections);
5521 /* The Solaris linker creates program headers in which all the
5522 p_paddr fields are zero. When we try to objcopy or strip such a
5523 file, we get confused. Check for this case, and if we find it
5524 reset the p_paddr_valid fields. */
5525 for (map = map_first; map != NULL; map = map->next)
5526 if (map->p_paddr != 0)
5527 break;
5528 if (map == NULL)
5529 for (map = map_first; map != NULL; map = map->next)
5530 map->p_paddr_valid = 0;
5532 elf_tdata (obfd)->segment_map = map_first;
5534 /* If we had to estimate the number of program headers that were
5535 going to be needed, then check our estimate now and adjust
5536 the offset if necessary. */
5537 if (phdr_adjust_seg != NULL)
5539 unsigned int count;
5541 for (count = 0, map = map_first; map != NULL; map = map->next)
5542 count++;
5544 if (count > phdr_adjust_num)
5545 phdr_adjust_seg->p_paddr
5546 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5549 #undef SEGMENT_END
5550 #undef SECTION_SIZE
5551 #undef IS_CONTAINED_BY_VMA
5552 #undef IS_CONTAINED_BY_LMA
5553 #undef IS_COREFILE_NOTE
5554 #undef IS_SOLARIS_PT_INTERP
5555 #undef IS_SECTION_IN_INPUT_SEGMENT
5556 #undef INCLUDE_SECTION_IN_SEGMENT
5557 #undef SEGMENT_AFTER_SEGMENT
5558 #undef SEGMENT_OVERLAPS
5559 return TRUE;
5562 /* Copy ELF program header information. */
5564 static bfd_boolean
5565 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5567 Elf_Internal_Ehdr *iehdr;
5568 struct elf_segment_map *map;
5569 struct elf_segment_map *map_first;
5570 struct elf_segment_map **pointer_to_map;
5571 Elf_Internal_Phdr *segment;
5572 unsigned int i;
5573 unsigned int num_segments;
5574 bfd_boolean phdr_included = FALSE;
5576 iehdr = elf_elfheader (ibfd);
5578 map_first = NULL;
5579 pointer_to_map = &map_first;
5581 num_segments = elf_elfheader (ibfd)->e_phnum;
5582 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5583 i < num_segments;
5584 i++, segment++)
5586 asection *section;
5587 unsigned int section_count;
5588 bfd_size_type amt;
5589 Elf_Internal_Shdr *this_hdr;
5590 asection *first_section = NULL;
5591 asection *lowest_section = NULL;
5593 /* FIXME: Do we need to copy PT_NULL segment? */
5594 if (segment->p_type == PT_NULL)
5595 continue;
5597 /* Compute how many sections are in this segment. */
5598 for (section = ibfd->sections, section_count = 0;
5599 section != NULL;
5600 section = section->next)
5602 this_hdr = &(elf_section_data(section)->this_hdr);
5603 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5605 if (!first_section)
5606 first_section = lowest_section = section;
5607 if (section->lma < lowest_section->lma)
5608 lowest_section = section;
5609 section_count++;
5613 /* Allocate a segment map big enough to contain
5614 all of the sections we have selected. */
5615 amt = sizeof (struct elf_segment_map);
5616 if (section_count != 0)
5617 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5618 map = bfd_zalloc (obfd, amt);
5619 if (map == NULL)
5620 return FALSE;
5622 /* Initialize the fields of the output segment map with the
5623 input segment. */
5624 map->next = NULL;
5625 map->p_type = segment->p_type;
5626 map->p_flags = segment->p_flags;
5627 map->p_flags_valid = 1;
5628 map->p_paddr = segment->p_paddr;
5629 map->p_paddr_valid = 1;
5630 map->p_align = segment->p_align;
5631 map->p_align_valid = 1;
5632 map->p_vaddr_offset = 0;
5634 /* Determine if this segment contains the ELF file header
5635 and if it contains the program headers themselves. */
5636 map->includes_filehdr = (segment->p_offset == 0
5637 && segment->p_filesz >= iehdr->e_ehsize);
5639 map->includes_phdrs = 0;
5640 if (! phdr_included || segment->p_type != PT_LOAD)
5642 map->includes_phdrs =
5643 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5644 && (segment->p_offset + segment->p_filesz
5645 >= ((bfd_vma) iehdr->e_phoff
5646 + iehdr->e_phnum * iehdr->e_phentsize)));
5648 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5649 phdr_included = TRUE;
5652 if (!map->includes_phdrs && !map->includes_filehdr)
5653 /* There is some other padding before the first section. */
5654 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5655 - segment->p_paddr);
5657 if (section_count != 0)
5659 unsigned int isec = 0;
5661 for (section = first_section;
5662 section != NULL;
5663 section = section->next)
5665 this_hdr = &(elf_section_data(section)->this_hdr);
5666 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5668 map->sections[isec++] = section->output_section;
5669 if (isec == section_count)
5670 break;
5675 map->count = section_count;
5676 *pointer_to_map = map;
5677 pointer_to_map = &map->next;
5680 elf_tdata (obfd)->segment_map = map_first;
5681 return TRUE;
5684 /* Copy private BFD data. This copies or rewrites ELF program header
5685 information. */
5687 static bfd_boolean
5688 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5690 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5691 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5692 return TRUE;
5694 if (elf_tdata (ibfd)->phdr == NULL)
5695 return TRUE;
5697 if (ibfd->xvec == obfd->xvec)
5699 /* Check to see if any sections in the input BFD
5700 covered by ELF program header have changed. */
5701 Elf_Internal_Phdr *segment;
5702 asection *section, *osec;
5703 unsigned int i, num_segments;
5704 Elf_Internal_Shdr *this_hdr;
5706 /* Initialize the segment mark field. */
5707 for (section = obfd->sections; section != NULL;
5708 section = section->next)
5709 section->segment_mark = FALSE;
5711 num_segments = elf_elfheader (ibfd)->e_phnum;
5712 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5713 i < num_segments;
5714 i++, segment++)
5716 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5717 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5718 which severly confuses things, so always regenerate the segment
5719 map in this case. */
5720 if (segment->p_paddr == 0
5721 && segment->p_memsz == 0
5722 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5723 goto rewrite;
5725 for (section = ibfd->sections;
5726 section != NULL; section = section->next)
5728 /* We mark the output section so that we know it comes
5729 from the input BFD. */
5730 osec = section->output_section;
5731 if (osec)
5732 osec->segment_mark = TRUE;
5734 /* Check if this section is covered by the segment. */
5735 this_hdr = &(elf_section_data(section)->this_hdr);
5736 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5738 /* FIXME: Check if its output section is changed or
5739 removed. What else do we need to check? */
5740 if (osec == NULL
5741 || section->flags != osec->flags
5742 || section->lma != osec->lma
5743 || section->vma != osec->vma
5744 || section->size != osec->size
5745 || section->rawsize != osec->rawsize
5746 || section->alignment_power != osec->alignment_power)
5747 goto rewrite;
5752 /* Check to see if any output section do not come from the
5753 input BFD. */
5754 for (section = obfd->sections; section != NULL;
5755 section = section->next)
5757 if (section->segment_mark == FALSE)
5758 goto rewrite;
5759 else
5760 section->segment_mark = FALSE;
5763 return copy_elf_program_header (ibfd, obfd);
5766 rewrite:
5767 return rewrite_elf_program_header (ibfd, obfd);
5770 /* Initialize private output section information from input section. */
5772 bfd_boolean
5773 _bfd_elf_init_private_section_data (bfd *ibfd,
5774 asection *isec,
5775 bfd *obfd,
5776 asection *osec,
5777 struct bfd_link_info *link_info)
5780 Elf_Internal_Shdr *ihdr, *ohdr;
5781 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
5783 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5784 || obfd->xvec->flavour != bfd_target_elf_flavour)
5785 return TRUE;
5787 /* Don't copy the output ELF section type from input if the
5788 output BFD section flags have been set to something different.
5789 elf_fake_sections will set ELF section type based on BFD
5790 section flags. */
5791 if (elf_section_type (osec) == SHT_NULL
5792 && (osec->flags == isec->flags || !osec->flags))
5793 elf_section_type (osec) = elf_section_type (isec);
5795 /* FIXME: Is this correct for all OS/PROC specific flags? */
5796 elf_section_flags (osec) |= (elf_section_flags (isec)
5797 & (SHF_MASKOS | SHF_MASKPROC));
5799 /* Set things up for objcopy and relocatable link. The output
5800 SHT_GROUP section will have its elf_next_in_group pointing back
5801 to the input group members. Ignore linker created group section.
5802 See elfNN_ia64_object_p in elfxx-ia64.c. */
5803 if (need_group)
5805 if (elf_sec_group (isec) == NULL
5806 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
5808 if (elf_section_flags (isec) & SHF_GROUP)
5809 elf_section_flags (osec) |= SHF_GROUP;
5810 elf_next_in_group (osec) = elf_next_in_group (isec);
5811 elf_group_name (osec) = elf_group_name (isec);
5815 ihdr = &elf_section_data (isec)->this_hdr;
5817 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
5818 don't use the output section of the linked-to section since it
5819 may be NULL at this point. */
5820 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
5822 ohdr = &elf_section_data (osec)->this_hdr;
5823 ohdr->sh_flags |= SHF_LINK_ORDER;
5824 elf_linked_to_section (osec) = elf_linked_to_section (isec);
5827 osec->use_rela_p = isec->use_rela_p;
5829 return TRUE;
5832 /* Copy private section information. This copies over the entsize
5833 field, and sometimes the info field. */
5835 bfd_boolean
5836 _bfd_elf_copy_private_section_data (bfd *ibfd,
5837 asection *isec,
5838 bfd *obfd,
5839 asection *osec)
5841 Elf_Internal_Shdr *ihdr, *ohdr;
5843 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5844 || obfd->xvec->flavour != bfd_target_elf_flavour)
5845 return TRUE;
5847 ihdr = &elf_section_data (isec)->this_hdr;
5848 ohdr = &elf_section_data (osec)->this_hdr;
5850 ohdr->sh_entsize = ihdr->sh_entsize;
5852 if (ihdr->sh_type == SHT_SYMTAB
5853 || ihdr->sh_type == SHT_DYNSYM
5854 || ihdr->sh_type == SHT_GNU_verneed
5855 || ihdr->sh_type == SHT_GNU_verdef)
5856 ohdr->sh_info = ihdr->sh_info;
5858 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
5859 NULL);
5862 /* Copy private header information. */
5864 bfd_boolean
5865 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
5867 asection *isec;
5869 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5870 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5871 return TRUE;
5873 /* Copy over private BFD data if it has not already been copied.
5874 This must be done here, rather than in the copy_private_bfd_data
5875 entry point, because the latter is called after the section
5876 contents have been set, which means that the program headers have
5877 already been worked out. */
5878 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5880 if (! copy_private_bfd_data (ibfd, obfd))
5881 return FALSE;
5884 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
5885 but this might be wrong if we deleted the group section. */
5886 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
5887 if (elf_section_type (isec) == SHT_GROUP
5888 && isec->output_section == NULL)
5890 asection *first = elf_next_in_group (isec);
5891 asection *s = first;
5892 while (s != NULL)
5894 if (s->output_section != NULL)
5896 elf_section_flags (s->output_section) &= ~SHF_GROUP;
5897 elf_group_name (s->output_section) = NULL;
5899 s = elf_next_in_group (s);
5900 if (s == first)
5901 break;
5905 return TRUE;
5908 /* Copy private symbol information. If this symbol is in a section
5909 which we did not map into a BFD section, try to map the section
5910 index correctly. We use special macro definitions for the mapped
5911 section indices; these definitions are interpreted by the
5912 swap_out_syms function. */
5914 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5915 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5916 #define MAP_STRTAB (SHN_HIOS + 3)
5917 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5918 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5920 bfd_boolean
5921 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5922 asymbol *isymarg,
5923 bfd *obfd,
5924 asymbol *osymarg)
5926 elf_symbol_type *isym, *osym;
5928 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5929 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5930 return TRUE;
5932 isym = elf_symbol_from (ibfd, isymarg);
5933 osym = elf_symbol_from (obfd, osymarg);
5935 if (isym != NULL
5936 && osym != NULL
5937 && bfd_is_abs_section (isym->symbol.section))
5939 unsigned int shndx;
5941 shndx = isym->internal_elf_sym.st_shndx;
5942 if (shndx == elf_onesymtab (ibfd))
5943 shndx = MAP_ONESYMTAB;
5944 else if (shndx == elf_dynsymtab (ibfd))
5945 shndx = MAP_DYNSYMTAB;
5946 else if (shndx == elf_tdata (ibfd)->strtab_section)
5947 shndx = MAP_STRTAB;
5948 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5949 shndx = MAP_SHSTRTAB;
5950 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5951 shndx = MAP_SYM_SHNDX;
5952 osym->internal_elf_sym.st_shndx = shndx;
5955 return TRUE;
5958 /* Swap out the symbols. */
5960 static bfd_boolean
5961 swap_out_syms (bfd *abfd,
5962 struct bfd_strtab_hash **sttp,
5963 int relocatable_p)
5965 const struct elf_backend_data *bed;
5966 int symcount;
5967 asymbol **syms;
5968 struct bfd_strtab_hash *stt;
5969 Elf_Internal_Shdr *symtab_hdr;
5970 Elf_Internal_Shdr *symtab_shndx_hdr;
5971 Elf_Internal_Shdr *symstrtab_hdr;
5972 bfd_byte *outbound_syms;
5973 bfd_byte *outbound_shndx;
5974 int idx;
5975 bfd_size_type amt;
5976 bfd_boolean name_local_sections;
5978 if (!elf_map_symbols (abfd))
5979 return FALSE;
5981 /* Dump out the symtabs. */
5982 stt = _bfd_elf_stringtab_init ();
5983 if (stt == NULL)
5984 return FALSE;
5986 bed = get_elf_backend_data (abfd);
5987 symcount = bfd_get_symcount (abfd);
5988 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5989 symtab_hdr->sh_type = SHT_SYMTAB;
5990 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5991 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5992 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5993 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5995 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5996 symstrtab_hdr->sh_type = SHT_STRTAB;
5998 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
5999 if (outbound_syms == NULL)
6001 _bfd_stringtab_free (stt);
6002 return FALSE;
6004 symtab_hdr->contents = outbound_syms;
6006 outbound_shndx = NULL;
6007 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6008 if (symtab_shndx_hdr->sh_name != 0)
6010 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6011 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6012 sizeof (Elf_External_Sym_Shndx));
6013 if (outbound_shndx == NULL)
6015 _bfd_stringtab_free (stt);
6016 return FALSE;
6019 symtab_shndx_hdr->contents = outbound_shndx;
6020 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6021 symtab_shndx_hdr->sh_size = amt;
6022 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6023 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6026 /* Now generate the data (for "contents"). */
6028 /* Fill in zeroth symbol and swap it out. */
6029 Elf_Internal_Sym sym;
6030 sym.st_name = 0;
6031 sym.st_value = 0;
6032 sym.st_size = 0;
6033 sym.st_info = 0;
6034 sym.st_other = 0;
6035 sym.st_shndx = SHN_UNDEF;
6036 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6037 outbound_syms += bed->s->sizeof_sym;
6038 if (outbound_shndx != NULL)
6039 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6042 name_local_sections
6043 = (bed->elf_backend_name_local_section_symbols
6044 && bed->elf_backend_name_local_section_symbols (abfd));
6046 syms = bfd_get_outsymbols (abfd);
6047 for (idx = 0; idx < symcount; idx++)
6049 Elf_Internal_Sym sym;
6050 bfd_vma value = syms[idx]->value;
6051 elf_symbol_type *type_ptr;
6052 flagword flags = syms[idx]->flags;
6053 int type;
6055 if (!name_local_sections
6056 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6058 /* Local section symbols have no name. */
6059 sym.st_name = 0;
6061 else
6063 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6064 syms[idx]->name,
6065 TRUE, FALSE);
6066 if (sym.st_name == (unsigned long) -1)
6068 _bfd_stringtab_free (stt);
6069 return FALSE;
6073 type_ptr = elf_symbol_from (abfd, syms[idx]);
6075 if ((flags & BSF_SECTION_SYM) == 0
6076 && bfd_is_com_section (syms[idx]->section))
6078 /* ELF common symbols put the alignment into the `value' field,
6079 and the size into the `size' field. This is backwards from
6080 how BFD handles it, so reverse it here. */
6081 sym.st_size = value;
6082 if (type_ptr == NULL
6083 || type_ptr->internal_elf_sym.st_value == 0)
6084 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6085 else
6086 sym.st_value = type_ptr->internal_elf_sym.st_value;
6087 sym.st_shndx = _bfd_elf_section_from_bfd_section
6088 (abfd, syms[idx]->section);
6090 else
6092 asection *sec = syms[idx]->section;
6093 int shndx;
6095 if (sec->output_section)
6097 value += sec->output_offset;
6098 sec = sec->output_section;
6101 /* Don't add in the section vma for relocatable output. */
6102 if (! relocatable_p)
6103 value += sec->vma;
6104 sym.st_value = value;
6105 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6107 if (bfd_is_abs_section (sec)
6108 && type_ptr != NULL
6109 && type_ptr->internal_elf_sym.st_shndx != 0)
6111 /* This symbol is in a real ELF section which we did
6112 not create as a BFD section. Undo the mapping done
6113 by copy_private_symbol_data. */
6114 shndx = type_ptr->internal_elf_sym.st_shndx;
6115 switch (shndx)
6117 case MAP_ONESYMTAB:
6118 shndx = elf_onesymtab (abfd);
6119 break;
6120 case MAP_DYNSYMTAB:
6121 shndx = elf_dynsymtab (abfd);
6122 break;
6123 case MAP_STRTAB:
6124 shndx = elf_tdata (abfd)->strtab_section;
6125 break;
6126 case MAP_SHSTRTAB:
6127 shndx = elf_tdata (abfd)->shstrtab_section;
6128 break;
6129 case MAP_SYM_SHNDX:
6130 shndx = elf_tdata (abfd)->symtab_shndx_section;
6131 break;
6132 default:
6133 break;
6136 else
6138 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6140 if (shndx == -1)
6142 asection *sec2;
6144 /* Writing this would be a hell of a lot easier if
6145 we had some decent documentation on bfd, and
6146 knew what to expect of the library, and what to
6147 demand of applications. For example, it
6148 appears that `objcopy' might not set the
6149 section of a symbol to be a section that is
6150 actually in the output file. */
6151 sec2 = bfd_get_section_by_name (abfd, sec->name);
6152 if (sec2 == NULL)
6154 _bfd_error_handler (_("\
6155 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6156 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6157 sec->name);
6158 bfd_set_error (bfd_error_invalid_operation);
6159 _bfd_stringtab_free (stt);
6160 return FALSE;
6163 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6164 BFD_ASSERT (shndx != -1);
6168 sym.st_shndx = shndx;
6171 if ((flags & BSF_THREAD_LOCAL) != 0)
6172 type = STT_TLS;
6173 else if ((flags & BSF_FUNCTION) != 0)
6174 type = STT_FUNC;
6175 else if ((flags & BSF_OBJECT) != 0)
6176 type = STT_OBJECT;
6177 else if ((flags & BSF_RELC) != 0)
6178 type = STT_RELC;
6179 else if ((flags & BSF_SRELC) != 0)
6180 type = STT_SRELC;
6181 else
6182 type = STT_NOTYPE;
6184 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6185 type = STT_TLS;
6187 /* Processor-specific types. */
6188 if (type_ptr != NULL
6189 && bed->elf_backend_get_symbol_type)
6190 type = ((*bed->elf_backend_get_symbol_type)
6191 (&type_ptr->internal_elf_sym, type));
6193 if (flags & BSF_SECTION_SYM)
6195 if (flags & BSF_GLOBAL)
6196 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6197 else
6198 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6200 else if (bfd_is_com_section (syms[idx]->section))
6201 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6202 else if (bfd_is_und_section (syms[idx]->section))
6203 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6204 ? STB_WEAK
6205 : STB_GLOBAL),
6206 type);
6207 else if (flags & BSF_FILE)
6208 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6209 else
6211 int bind = STB_LOCAL;
6213 if (flags & BSF_LOCAL)
6214 bind = STB_LOCAL;
6215 else if (flags & BSF_WEAK)
6216 bind = STB_WEAK;
6217 else if (flags & BSF_GLOBAL)
6218 bind = STB_GLOBAL;
6220 sym.st_info = ELF_ST_INFO (bind, type);
6223 if (type_ptr != NULL)
6224 sym.st_other = type_ptr->internal_elf_sym.st_other;
6225 else
6226 sym.st_other = 0;
6228 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6229 outbound_syms += bed->s->sizeof_sym;
6230 if (outbound_shndx != NULL)
6231 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6234 *sttp = stt;
6235 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6236 symstrtab_hdr->sh_type = SHT_STRTAB;
6238 symstrtab_hdr->sh_flags = 0;
6239 symstrtab_hdr->sh_addr = 0;
6240 symstrtab_hdr->sh_entsize = 0;
6241 symstrtab_hdr->sh_link = 0;
6242 symstrtab_hdr->sh_info = 0;
6243 symstrtab_hdr->sh_addralign = 1;
6245 return TRUE;
6248 /* Return the number of bytes required to hold the symtab vector.
6250 Note that we base it on the count plus 1, since we will null terminate
6251 the vector allocated based on this size. However, the ELF symbol table
6252 always has a dummy entry as symbol #0, so it ends up even. */
6254 long
6255 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6257 long symcount;
6258 long symtab_size;
6259 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6261 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6262 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6263 if (symcount > 0)
6264 symtab_size -= sizeof (asymbol *);
6266 return symtab_size;
6269 long
6270 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6272 long symcount;
6273 long symtab_size;
6274 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6276 if (elf_dynsymtab (abfd) == 0)
6278 bfd_set_error (bfd_error_invalid_operation);
6279 return -1;
6282 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6283 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6284 if (symcount > 0)
6285 symtab_size -= sizeof (asymbol *);
6287 return symtab_size;
6290 long
6291 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6292 sec_ptr asect)
6294 return (asect->reloc_count + 1) * sizeof (arelent *);
6297 /* Canonicalize the relocs. */
6299 long
6300 _bfd_elf_canonicalize_reloc (bfd *abfd,
6301 sec_ptr section,
6302 arelent **relptr,
6303 asymbol **symbols)
6305 arelent *tblptr;
6306 unsigned int i;
6307 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6309 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6310 return -1;
6312 tblptr = section->relocation;
6313 for (i = 0; i < section->reloc_count; i++)
6314 *relptr++ = tblptr++;
6316 *relptr = NULL;
6318 return section->reloc_count;
6321 long
6322 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6324 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6325 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6327 if (symcount >= 0)
6328 bfd_get_symcount (abfd) = symcount;
6329 return symcount;
6332 long
6333 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6334 asymbol **allocation)
6336 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6337 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6339 if (symcount >= 0)
6340 bfd_get_dynamic_symcount (abfd) = symcount;
6341 return symcount;
6344 /* Return the size required for the dynamic reloc entries. Any loadable
6345 section that was actually installed in the BFD, and has type SHT_REL
6346 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6347 dynamic reloc section. */
6349 long
6350 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6352 long ret;
6353 asection *s;
6355 if (elf_dynsymtab (abfd) == 0)
6357 bfd_set_error (bfd_error_invalid_operation);
6358 return -1;
6361 ret = sizeof (arelent *);
6362 for (s = abfd->sections; s != NULL; s = s->next)
6363 if ((s->flags & SEC_LOAD) != 0
6364 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6365 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6366 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6367 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6368 * sizeof (arelent *));
6370 return ret;
6373 /* Canonicalize the dynamic relocation entries. Note that we return the
6374 dynamic relocations as a single block, although they are actually
6375 associated with particular sections; the interface, which was
6376 designed for SunOS style shared libraries, expects that there is only
6377 one set of dynamic relocs. Any loadable section that was actually
6378 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6379 dynamic symbol table, is considered to be a dynamic reloc section. */
6381 long
6382 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6383 arelent **storage,
6384 asymbol **syms)
6386 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6387 asection *s;
6388 long ret;
6390 if (elf_dynsymtab (abfd) == 0)
6392 bfd_set_error (bfd_error_invalid_operation);
6393 return -1;
6396 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6397 ret = 0;
6398 for (s = abfd->sections; s != NULL; s = s->next)
6400 if ((s->flags & SEC_LOAD) != 0
6401 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6402 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6403 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6405 arelent *p;
6406 long count, i;
6408 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6409 return -1;
6410 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6411 p = s->relocation;
6412 for (i = 0; i < count; i++)
6413 *storage++ = p++;
6414 ret += count;
6418 *storage = NULL;
6420 return ret;
6423 /* Read in the version information. */
6425 bfd_boolean
6426 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6428 bfd_byte *contents = NULL;
6429 unsigned int freeidx = 0;
6431 if (elf_dynverref (abfd) != 0)
6433 Elf_Internal_Shdr *hdr;
6434 Elf_External_Verneed *everneed;
6435 Elf_Internal_Verneed *iverneed;
6436 unsigned int i;
6437 bfd_byte *contents_end;
6439 hdr = &elf_tdata (abfd)->dynverref_hdr;
6441 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6442 sizeof (Elf_Internal_Verneed));
6443 if (elf_tdata (abfd)->verref == NULL)
6444 goto error_return;
6446 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6448 contents = bfd_malloc (hdr->sh_size);
6449 if (contents == NULL)
6451 error_return_verref:
6452 elf_tdata (abfd)->verref = NULL;
6453 elf_tdata (abfd)->cverrefs = 0;
6454 goto error_return;
6456 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6457 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6458 goto error_return_verref;
6460 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6461 goto error_return_verref;
6463 BFD_ASSERT (sizeof (Elf_External_Verneed)
6464 == sizeof (Elf_External_Vernaux));
6465 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6466 everneed = (Elf_External_Verneed *) contents;
6467 iverneed = elf_tdata (abfd)->verref;
6468 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6470 Elf_External_Vernaux *evernaux;
6471 Elf_Internal_Vernaux *ivernaux;
6472 unsigned int j;
6474 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6476 iverneed->vn_bfd = abfd;
6478 iverneed->vn_filename =
6479 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6480 iverneed->vn_file);
6481 if (iverneed->vn_filename == NULL)
6482 goto error_return_verref;
6484 if (iverneed->vn_cnt == 0)
6485 iverneed->vn_auxptr = NULL;
6486 else
6488 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6489 sizeof (Elf_Internal_Vernaux));
6490 if (iverneed->vn_auxptr == NULL)
6491 goto error_return_verref;
6494 if (iverneed->vn_aux
6495 > (size_t) (contents_end - (bfd_byte *) everneed))
6496 goto error_return_verref;
6498 evernaux = ((Elf_External_Vernaux *)
6499 ((bfd_byte *) everneed + iverneed->vn_aux));
6500 ivernaux = iverneed->vn_auxptr;
6501 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6503 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6505 ivernaux->vna_nodename =
6506 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6507 ivernaux->vna_name);
6508 if (ivernaux->vna_nodename == NULL)
6509 goto error_return_verref;
6511 if (j + 1 < iverneed->vn_cnt)
6512 ivernaux->vna_nextptr = ivernaux + 1;
6513 else
6514 ivernaux->vna_nextptr = NULL;
6516 if (ivernaux->vna_next
6517 > (size_t) (contents_end - (bfd_byte *) evernaux))
6518 goto error_return_verref;
6520 evernaux = ((Elf_External_Vernaux *)
6521 ((bfd_byte *) evernaux + ivernaux->vna_next));
6523 if (ivernaux->vna_other > freeidx)
6524 freeidx = ivernaux->vna_other;
6527 if (i + 1 < hdr->sh_info)
6528 iverneed->vn_nextref = iverneed + 1;
6529 else
6530 iverneed->vn_nextref = NULL;
6532 if (iverneed->vn_next
6533 > (size_t) (contents_end - (bfd_byte *) everneed))
6534 goto error_return_verref;
6536 everneed = ((Elf_External_Verneed *)
6537 ((bfd_byte *) everneed + iverneed->vn_next));
6540 free (contents);
6541 contents = NULL;
6544 if (elf_dynverdef (abfd) != 0)
6546 Elf_Internal_Shdr *hdr;
6547 Elf_External_Verdef *everdef;
6548 Elf_Internal_Verdef *iverdef;
6549 Elf_Internal_Verdef *iverdefarr;
6550 Elf_Internal_Verdef iverdefmem;
6551 unsigned int i;
6552 unsigned int maxidx;
6553 bfd_byte *contents_end_def, *contents_end_aux;
6555 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6557 contents = bfd_malloc (hdr->sh_size);
6558 if (contents == NULL)
6559 goto error_return;
6560 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6561 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6562 goto error_return;
6564 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6565 goto error_return;
6567 BFD_ASSERT (sizeof (Elf_External_Verdef)
6568 >= sizeof (Elf_External_Verdaux));
6569 contents_end_def = contents + hdr->sh_size
6570 - sizeof (Elf_External_Verdef);
6571 contents_end_aux = contents + hdr->sh_size
6572 - sizeof (Elf_External_Verdaux);
6574 /* We know the number of entries in the section but not the maximum
6575 index. Therefore we have to run through all entries and find
6576 the maximum. */
6577 everdef = (Elf_External_Verdef *) contents;
6578 maxidx = 0;
6579 for (i = 0; i < hdr->sh_info; ++i)
6581 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6583 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6584 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6586 if (iverdefmem.vd_next
6587 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6588 goto error_return;
6590 everdef = ((Elf_External_Verdef *)
6591 ((bfd_byte *) everdef + iverdefmem.vd_next));
6594 if (default_imported_symver)
6596 if (freeidx > maxidx)
6597 maxidx = ++freeidx;
6598 else
6599 freeidx = ++maxidx;
6601 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6602 sizeof (Elf_Internal_Verdef));
6603 if (elf_tdata (abfd)->verdef == NULL)
6604 goto error_return;
6606 elf_tdata (abfd)->cverdefs = maxidx;
6608 everdef = (Elf_External_Verdef *) contents;
6609 iverdefarr = elf_tdata (abfd)->verdef;
6610 for (i = 0; i < hdr->sh_info; i++)
6612 Elf_External_Verdaux *everdaux;
6613 Elf_Internal_Verdaux *iverdaux;
6614 unsigned int j;
6616 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6618 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6620 error_return_verdef:
6621 elf_tdata (abfd)->verdef = NULL;
6622 elf_tdata (abfd)->cverdefs = 0;
6623 goto error_return;
6626 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6627 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6629 iverdef->vd_bfd = abfd;
6631 if (iverdef->vd_cnt == 0)
6632 iverdef->vd_auxptr = NULL;
6633 else
6635 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6636 sizeof (Elf_Internal_Verdaux));
6637 if (iverdef->vd_auxptr == NULL)
6638 goto error_return_verdef;
6641 if (iverdef->vd_aux
6642 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6643 goto error_return_verdef;
6645 everdaux = ((Elf_External_Verdaux *)
6646 ((bfd_byte *) everdef + iverdef->vd_aux));
6647 iverdaux = iverdef->vd_auxptr;
6648 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6650 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6652 iverdaux->vda_nodename =
6653 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6654 iverdaux->vda_name);
6655 if (iverdaux->vda_nodename == NULL)
6656 goto error_return_verdef;
6658 if (j + 1 < iverdef->vd_cnt)
6659 iverdaux->vda_nextptr = iverdaux + 1;
6660 else
6661 iverdaux->vda_nextptr = NULL;
6663 if (iverdaux->vda_next
6664 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6665 goto error_return_verdef;
6667 everdaux = ((Elf_External_Verdaux *)
6668 ((bfd_byte *) everdaux + iverdaux->vda_next));
6671 if (iverdef->vd_cnt)
6672 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6674 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6675 iverdef->vd_nextdef = iverdef + 1;
6676 else
6677 iverdef->vd_nextdef = NULL;
6679 everdef = ((Elf_External_Verdef *)
6680 ((bfd_byte *) everdef + iverdef->vd_next));
6683 free (contents);
6684 contents = NULL;
6686 else if (default_imported_symver)
6688 if (freeidx < 3)
6689 freeidx = 3;
6690 else
6691 freeidx++;
6693 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6694 sizeof (Elf_Internal_Verdef));
6695 if (elf_tdata (abfd)->verdef == NULL)
6696 goto error_return;
6698 elf_tdata (abfd)->cverdefs = freeidx;
6701 /* Create a default version based on the soname. */
6702 if (default_imported_symver)
6704 Elf_Internal_Verdef *iverdef;
6705 Elf_Internal_Verdaux *iverdaux;
6707 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6709 iverdef->vd_version = VER_DEF_CURRENT;
6710 iverdef->vd_flags = 0;
6711 iverdef->vd_ndx = freeidx;
6712 iverdef->vd_cnt = 1;
6714 iverdef->vd_bfd = abfd;
6716 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6717 if (iverdef->vd_nodename == NULL)
6718 goto error_return_verdef;
6719 iverdef->vd_nextdef = NULL;
6720 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6721 if (iverdef->vd_auxptr == NULL)
6722 goto error_return_verdef;
6724 iverdaux = iverdef->vd_auxptr;
6725 iverdaux->vda_nodename = iverdef->vd_nodename;
6726 iverdaux->vda_nextptr = NULL;
6729 return TRUE;
6731 error_return:
6732 if (contents != NULL)
6733 free (contents);
6734 return FALSE;
6737 asymbol *
6738 _bfd_elf_make_empty_symbol (bfd *abfd)
6740 elf_symbol_type *newsym;
6741 bfd_size_type amt = sizeof (elf_symbol_type);
6743 newsym = bfd_zalloc (abfd, amt);
6744 if (!newsym)
6745 return NULL;
6746 else
6748 newsym->symbol.the_bfd = abfd;
6749 return &newsym->symbol;
6753 void
6754 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6755 asymbol *symbol,
6756 symbol_info *ret)
6758 bfd_symbol_info (symbol, ret);
6761 /* Return whether a symbol name implies a local symbol. Most targets
6762 use this function for the is_local_label_name entry point, but some
6763 override it. */
6765 bfd_boolean
6766 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6767 const char *name)
6769 /* Normal local symbols start with ``.L''. */
6770 if (name[0] == '.' && name[1] == 'L')
6771 return TRUE;
6773 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6774 DWARF debugging symbols starting with ``..''. */
6775 if (name[0] == '.' && name[1] == '.')
6776 return TRUE;
6778 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6779 emitting DWARF debugging output. I suspect this is actually a
6780 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6781 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6782 underscore to be emitted on some ELF targets). For ease of use,
6783 we treat such symbols as local. */
6784 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6785 return TRUE;
6787 return FALSE;
6790 alent *
6791 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6792 asymbol *symbol ATTRIBUTE_UNUSED)
6794 abort ();
6795 return NULL;
6798 bfd_boolean
6799 _bfd_elf_set_arch_mach (bfd *abfd,
6800 enum bfd_architecture arch,
6801 unsigned long machine)
6803 /* If this isn't the right architecture for this backend, and this
6804 isn't the generic backend, fail. */
6805 if (arch != get_elf_backend_data (abfd)->arch
6806 && arch != bfd_arch_unknown
6807 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6808 return FALSE;
6810 return bfd_default_set_arch_mach (abfd, arch, machine);
6813 /* Find the function to a particular section and offset,
6814 for error reporting. */
6816 static bfd_boolean
6817 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6818 asection *section,
6819 asymbol **symbols,
6820 bfd_vma offset,
6821 const char **filename_ptr,
6822 const char **functionname_ptr)
6824 const char *filename;
6825 asymbol *func, *file;
6826 bfd_vma low_func;
6827 asymbol **p;
6828 /* ??? Given multiple file symbols, it is impossible to reliably
6829 choose the right file name for global symbols. File symbols are
6830 local symbols, and thus all file symbols must sort before any
6831 global symbols. The ELF spec may be interpreted to say that a
6832 file symbol must sort before other local symbols, but currently
6833 ld -r doesn't do this. So, for ld -r output, it is possible to
6834 make a better choice of file name for local symbols by ignoring
6835 file symbols appearing after a given local symbol. */
6836 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6838 filename = NULL;
6839 func = NULL;
6840 file = NULL;
6841 low_func = 0;
6842 state = nothing_seen;
6844 for (p = symbols; *p != NULL; p++)
6846 elf_symbol_type *q;
6848 q = (elf_symbol_type *) *p;
6850 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6852 default:
6853 break;
6854 case STT_FILE:
6855 file = &q->symbol;
6856 if (state == symbol_seen)
6857 state = file_after_symbol_seen;
6858 continue;
6859 case STT_NOTYPE:
6860 case STT_FUNC:
6861 if (bfd_get_section (&q->symbol) == section
6862 && q->symbol.value >= low_func
6863 && q->symbol.value <= offset)
6865 func = (asymbol *) q;
6866 low_func = q->symbol.value;
6867 filename = NULL;
6868 if (file != NULL
6869 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
6870 || state != file_after_symbol_seen))
6871 filename = bfd_asymbol_name (file);
6873 break;
6875 if (state == nothing_seen)
6876 state = symbol_seen;
6879 if (func == NULL)
6880 return FALSE;
6882 if (filename_ptr)
6883 *filename_ptr = filename;
6884 if (functionname_ptr)
6885 *functionname_ptr = bfd_asymbol_name (func);
6887 return TRUE;
6890 /* Find the nearest line to a particular section and offset,
6891 for error reporting. */
6893 bfd_boolean
6894 _bfd_elf_find_nearest_line (bfd *abfd,
6895 asection *section,
6896 asymbol **symbols,
6897 bfd_vma offset,
6898 const char **filename_ptr,
6899 const char **functionname_ptr,
6900 unsigned int *line_ptr)
6902 bfd_boolean found;
6904 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6905 filename_ptr, functionname_ptr,
6906 line_ptr))
6908 if (!*functionname_ptr)
6909 elf_find_function (abfd, section, symbols, offset,
6910 *filename_ptr ? NULL : filename_ptr,
6911 functionname_ptr);
6913 return TRUE;
6916 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6917 filename_ptr, functionname_ptr,
6918 line_ptr, 0,
6919 &elf_tdata (abfd)->dwarf2_find_line_info))
6921 if (!*functionname_ptr)
6922 elf_find_function (abfd, section, symbols, offset,
6923 *filename_ptr ? NULL : filename_ptr,
6924 functionname_ptr);
6926 return TRUE;
6929 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6930 &found, filename_ptr,
6931 functionname_ptr, line_ptr,
6932 &elf_tdata (abfd)->line_info))
6933 return FALSE;
6934 if (found && (*functionname_ptr || *line_ptr))
6935 return TRUE;
6937 if (symbols == NULL)
6938 return FALSE;
6940 if (! elf_find_function (abfd, section, symbols, offset,
6941 filename_ptr, functionname_ptr))
6942 return FALSE;
6944 *line_ptr = 0;
6945 return TRUE;
6948 /* Find the line for a symbol. */
6950 bfd_boolean
6951 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
6952 const char **filename_ptr, unsigned int *line_ptr)
6954 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
6955 filename_ptr, line_ptr, 0,
6956 &elf_tdata (abfd)->dwarf2_find_line_info);
6959 /* After a call to bfd_find_nearest_line, successive calls to
6960 bfd_find_inliner_info can be used to get source information about
6961 each level of function inlining that terminated at the address
6962 passed to bfd_find_nearest_line. Currently this is only supported
6963 for DWARF2 with appropriate DWARF3 extensions. */
6965 bfd_boolean
6966 _bfd_elf_find_inliner_info (bfd *abfd,
6967 const char **filename_ptr,
6968 const char **functionname_ptr,
6969 unsigned int *line_ptr)
6971 bfd_boolean found;
6972 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
6973 functionname_ptr, line_ptr,
6974 & elf_tdata (abfd)->dwarf2_find_line_info);
6975 return found;
6979 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
6981 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6982 int ret = bed->s->sizeof_ehdr;
6984 if (!info->relocatable)
6986 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
6988 if (phdr_size == (bfd_size_type) -1)
6990 struct elf_segment_map *m;
6992 phdr_size = 0;
6993 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
6994 phdr_size += bed->s->sizeof_phdr;
6996 if (phdr_size == 0)
6997 phdr_size = get_program_header_size (abfd, info);
7000 elf_tdata (abfd)->program_header_size = phdr_size;
7001 ret += phdr_size;
7004 return ret;
7007 bfd_boolean
7008 _bfd_elf_set_section_contents (bfd *abfd,
7009 sec_ptr section,
7010 const void *location,
7011 file_ptr offset,
7012 bfd_size_type count)
7014 Elf_Internal_Shdr *hdr;
7015 bfd_signed_vma pos;
7017 if (! abfd->output_has_begun
7018 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7019 return FALSE;
7021 hdr = &elf_section_data (section)->this_hdr;
7022 pos = hdr->sh_offset + offset;
7023 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7024 || bfd_bwrite (location, count, abfd) != count)
7025 return FALSE;
7027 return TRUE;
7030 void
7031 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7032 arelent *cache_ptr ATTRIBUTE_UNUSED,
7033 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7035 abort ();
7038 /* Try to convert a non-ELF reloc into an ELF one. */
7040 bfd_boolean
7041 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7043 /* Check whether we really have an ELF howto. */
7045 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7047 bfd_reloc_code_real_type code;
7048 reloc_howto_type *howto;
7050 /* Alien reloc: Try to determine its type to replace it with an
7051 equivalent ELF reloc. */
7053 if (areloc->howto->pc_relative)
7055 switch (areloc->howto->bitsize)
7057 case 8:
7058 code = BFD_RELOC_8_PCREL;
7059 break;
7060 case 12:
7061 code = BFD_RELOC_12_PCREL;
7062 break;
7063 case 16:
7064 code = BFD_RELOC_16_PCREL;
7065 break;
7066 case 24:
7067 code = BFD_RELOC_24_PCREL;
7068 break;
7069 case 32:
7070 code = BFD_RELOC_32_PCREL;
7071 break;
7072 case 64:
7073 code = BFD_RELOC_64_PCREL;
7074 break;
7075 default:
7076 goto fail;
7079 howto = bfd_reloc_type_lookup (abfd, code);
7081 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7083 if (howto->pcrel_offset)
7084 areloc->addend += areloc->address;
7085 else
7086 areloc->addend -= areloc->address; /* addend is unsigned!! */
7089 else
7091 switch (areloc->howto->bitsize)
7093 case 8:
7094 code = BFD_RELOC_8;
7095 break;
7096 case 14:
7097 code = BFD_RELOC_14;
7098 break;
7099 case 16:
7100 code = BFD_RELOC_16;
7101 break;
7102 case 26:
7103 code = BFD_RELOC_26;
7104 break;
7105 case 32:
7106 code = BFD_RELOC_32;
7107 break;
7108 case 64:
7109 code = BFD_RELOC_64;
7110 break;
7111 default:
7112 goto fail;
7115 howto = bfd_reloc_type_lookup (abfd, code);
7118 if (howto)
7119 areloc->howto = howto;
7120 else
7121 goto fail;
7124 return TRUE;
7126 fail:
7127 (*_bfd_error_handler)
7128 (_("%B: unsupported relocation type %s"),
7129 abfd, areloc->howto->name);
7130 bfd_set_error (bfd_error_bad_value);
7131 return FALSE;
7134 bfd_boolean
7135 _bfd_elf_close_and_cleanup (bfd *abfd)
7137 if (bfd_get_format (abfd) == bfd_object)
7139 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7140 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7141 _bfd_dwarf2_cleanup_debug_info (abfd);
7144 return _bfd_generic_close_and_cleanup (abfd);
7147 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7148 in the relocation's offset. Thus we cannot allow any sort of sanity
7149 range-checking to interfere. There is nothing else to do in processing
7150 this reloc. */
7152 bfd_reloc_status_type
7153 _bfd_elf_rel_vtable_reloc_fn
7154 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7155 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7156 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7157 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7159 return bfd_reloc_ok;
7162 /* Elf core file support. Much of this only works on native
7163 toolchains, since we rely on knowing the
7164 machine-dependent procfs structure in order to pick
7165 out details about the corefile. */
7167 #ifdef HAVE_SYS_PROCFS_H
7168 # include <sys/procfs.h>
7169 #endif
7171 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7173 static int
7174 elfcore_make_pid (bfd *abfd)
7176 return ((elf_tdata (abfd)->core_lwpid << 16)
7177 + (elf_tdata (abfd)->core_pid));
7180 /* If there isn't a section called NAME, make one, using
7181 data from SECT. Note, this function will generate a
7182 reference to NAME, so you shouldn't deallocate or
7183 overwrite it. */
7185 static bfd_boolean
7186 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7188 asection *sect2;
7190 if (bfd_get_section_by_name (abfd, name) != NULL)
7191 return TRUE;
7193 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7194 if (sect2 == NULL)
7195 return FALSE;
7197 sect2->size = sect->size;
7198 sect2->filepos = sect->filepos;
7199 sect2->alignment_power = sect->alignment_power;
7200 return TRUE;
7203 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7204 actually creates up to two pseudosections:
7205 - For the single-threaded case, a section named NAME, unless
7206 such a section already exists.
7207 - For the multi-threaded case, a section named "NAME/PID", where
7208 PID is elfcore_make_pid (abfd).
7209 Both pseudosections have identical contents. */
7210 bfd_boolean
7211 _bfd_elfcore_make_pseudosection (bfd *abfd,
7212 char *name,
7213 size_t size,
7214 ufile_ptr filepos)
7216 char buf[100];
7217 char *threaded_name;
7218 size_t len;
7219 asection *sect;
7221 /* Build the section name. */
7223 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7224 len = strlen (buf) + 1;
7225 threaded_name = bfd_alloc (abfd, len);
7226 if (threaded_name == NULL)
7227 return FALSE;
7228 memcpy (threaded_name, buf, len);
7230 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7231 SEC_HAS_CONTENTS);
7232 if (sect == NULL)
7233 return FALSE;
7234 sect->size = size;
7235 sect->filepos = filepos;
7236 sect->alignment_power = 2;
7238 return elfcore_maybe_make_sect (abfd, name, sect);
7241 /* prstatus_t exists on:
7242 solaris 2.5+
7243 linux 2.[01] + glibc
7244 unixware 4.2
7247 #if defined (HAVE_PRSTATUS_T)
7249 static bfd_boolean
7250 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7252 size_t size;
7253 int offset;
7255 if (note->descsz == sizeof (prstatus_t))
7257 prstatus_t prstat;
7259 size = sizeof (prstat.pr_reg);
7260 offset = offsetof (prstatus_t, pr_reg);
7261 memcpy (&prstat, note->descdata, sizeof (prstat));
7263 /* Do not overwrite the core signal if it
7264 has already been set by another thread. */
7265 if (elf_tdata (abfd)->core_signal == 0)
7266 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7267 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7269 /* pr_who exists on:
7270 solaris 2.5+
7271 unixware 4.2
7272 pr_who doesn't exist on:
7273 linux 2.[01]
7275 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7276 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7277 #endif
7279 #if defined (HAVE_PRSTATUS32_T)
7280 else if (note->descsz == sizeof (prstatus32_t))
7282 /* 64-bit host, 32-bit corefile */
7283 prstatus32_t prstat;
7285 size = sizeof (prstat.pr_reg);
7286 offset = offsetof (prstatus32_t, pr_reg);
7287 memcpy (&prstat, note->descdata, sizeof (prstat));
7289 /* Do not overwrite the core signal if it
7290 has already been set by another thread. */
7291 if (elf_tdata (abfd)->core_signal == 0)
7292 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7293 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7295 /* pr_who exists on:
7296 solaris 2.5+
7297 unixware 4.2
7298 pr_who doesn't exist on:
7299 linux 2.[01]
7301 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7302 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7303 #endif
7305 #endif /* HAVE_PRSTATUS32_T */
7306 else
7308 /* Fail - we don't know how to handle any other
7309 note size (ie. data object type). */
7310 return TRUE;
7313 /* Make a ".reg/999" section and a ".reg" section. */
7314 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7315 size, note->descpos + offset);
7317 #endif /* defined (HAVE_PRSTATUS_T) */
7319 /* Create a pseudosection containing the exact contents of NOTE. */
7320 static bfd_boolean
7321 elfcore_make_note_pseudosection (bfd *abfd,
7322 char *name,
7323 Elf_Internal_Note *note)
7325 return _bfd_elfcore_make_pseudosection (abfd, name,
7326 note->descsz, note->descpos);
7329 /* There isn't a consistent prfpregset_t across platforms,
7330 but it doesn't matter, because we don't have to pick this
7331 data structure apart. */
7333 static bfd_boolean
7334 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7336 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7339 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7340 type of 5 (NT_PRXFPREG). Just include the whole note's contents
7341 literally. */
7343 static bfd_boolean
7344 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7346 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7349 #if defined (HAVE_PRPSINFO_T)
7350 typedef prpsinfo_t elfcore_psinfo_t;
7351 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7352 typedef prpsinfo32_t elfcore_psinfo32_t;
7353 #endif
7354 #endif
7356 #if defined (HAVE_PSINFO_T)
7357 typedef psinfo_t elfcore_psinfo_t;
7358 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7359 typedef psinfo32_t elfcore_psinfo32_t;
7360 #endif
7361 #endif
7363 /* return a malloc'ed copy of a string at START which is at
7364 most MAX bytes long, possibly without a terminating '\0'.
7365 the copy will always have a terminating '\0'. */
7367 char *
7368 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7370 char *dups;
7371 char *end = memchr (start, '\0', max);
7372 size_t len;
7374 if (end == NULL)
7375 len = max;
7376 else
7377 len = end - start;
7379 dups = bfd_alloc (abfd, len + 1);
7380 if (dups == NULL)
7381 return NULL;
7383 memcpy (dups, start, len);
7384 dups[len] = '\0';
7386 return dups;
7389 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7390 static bfd_boolean
7391 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7393 if (note->descsz == sizeof (elfcore_psinfo_t))
7395 elfcore_psinfo_t psinfo;
7397 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7399 elf_tdata (abfd)->core_program
7400 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7401 sizeof (psinfo.pr_fname));
7403 elf_tdata (abfd)->core_command
7404 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7405 sizeof (psinfo.pr_psargs));
7407 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7408 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7410 /* 64-bit host, 32-bit corefile */
7411 elfcore_psinfo32_t psinfo;
7413 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7415 elf_tdata (abfd)->core_program
7416 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7417 sizeof (psinfo.pr_fname));
7419 elf_tdata (abfd)->core_command
7420 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7421 sizeof (psinfo.pr_psargs));
7423 #endif
7425 else
7427 /* Fail - we don't know how to handle any other
7428 note size (ie. data object type). */
7429 return TRUE;
7432 /* Note that for some reason, a spurious space is tacked
7433 onto the end of the args in some (at least one anyway)
7434 implementations, so strip it off if it exists. */
7437 char *command = elf_tdata (abfd)->core_command;
7438 int n = strlen (command);
7440 if (0 < n && command[n - 1] == ' ')
7441 command[n - 1] = '\0';
7444 return TRUE;
7446 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7448 #if defined (HAVE_PSTATUS_T)
7449 static bfd_boolean
7450 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7452 if (note->descsz == sizeof (pstatus_t)
7453 #if defined (HAVE_PXSTATUS_T)
7454 || note->descsz == sizeof (pxstatus_t)
7455 #endif
7458 pstatus_t pstat;
7460 memcpy (&pstat, note->descdata, sizeof (pstat));
7462 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7464 #if defined (HAVE_PSTATUS32_T)
7465 else if (note->descsz == sizeof (pstatus32_t))
7467 /* 64-bit host, 32-bit corefile */
7468 pstatus32_t pstat;
7470 memcpy (&pstat, note->descdata, sizeof (pstat));
7472 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7474 #endif
7475 /* Could grab some more details from the "representative"
7476 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7477 NT_LWPSTATUS note, presumably. */
7479 return TRUE;
7481 #endif /* defined (HAVE_PSTATUS_T) */
7483 #if defined (HAVE_LWPSTATUS_T)
7484 static bfd_boolean
7485 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7487 lwpstatus_t lwpstat;
7488 char buf[100];
7489 char *name;
7490 size_t len;
7491 asection *sect;
7493 if (note->descsz != sizeof (lwpstat)
7494 #if defined (HAVE_LWPXSTATUS_T)
7495 && note->descsz != sizeof (lwpxstatus_t)
7496 #endif
7498 return TRUE;
7500 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7502 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7503 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7505 /* Make a ".reg/999" section. */
7507 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7508 len = strlen (buf) + 1;
7509 name = bfd_alloc (abfd, len);
7510 if (name == NULL)
7511 return FALSE;
7512 memcpy (name, buf, len);
7514 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7515 if (sect == NULL)
7516 return FALSE;
7518 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7519 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7520 sect->filepos = note->descpos
7521 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7522 #endif
7524 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7525 sect->size = sizeof (lwpstat.pr_reg);
7526 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7527 #endif
7529 sect->alignment_power = 2;
7531 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7532 return FALSE;
7534 /* Make a ".reg2/999" section */
7536 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7537 len = strlen (buf) + 1;
7538 name = bfd_alloc (abfd, len);
7539 if (name == NULL)
7540 return FALSE;
7541 memcpy (name, buf, len);
7543 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7544 if (sect == NULL)
7545 return FALSE;
7547 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7548 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7549 sect->filepos = note->descpos
7550 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7551 #endif
7553 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7554 sect->size = sizeof (lwpstat.pr_fpreg);
7555 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7556 #endif
7558 sect->alignment_power = 2;
7560 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7562 #endif /* defined (HAVE_LWPSTATUS_T) */
7564 #if defined (HAVE_WIN32_PSTATUS_T)
7565 static bfd_boolean
7566 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7568 char buf[30];
7569 char *name;
7570 size_t len;
7571 asection *sect;
7572 win32_pstatus_t pstatus;
7574 if (note->descsz < sizeof (pstatus))
7575 return TRUE;
7577 memcpy (&pstatus, note->descdata, sizeof (pstatus));
7579 switch (pstatus.data_type)
7581 case NOTE_INFO_PROCESS:
7582 /* FIXME: need to add ->core_command. */
7583 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
7584 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
7585 break;
7587 case NOTE_INFO_THREAD:
7588 /* Make a ".reg/999" section. */
7589 sprintf (buf, ".reg/%ld", (long) pstatus.data.thread_info.tid);
7591 len = strlen (buf) + 1;
7592 name = bfd_alloc (abfd, len);
7593 if (name == NULL)
7594 return FALSE;
7596 memcpy (name, buf, len);
7598 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7599 if (sect == NULL)
7600 return FALSE;
7602 sect->size = sizeof (pstatus.data.thread_info.thread_context);
7603 sect->filepos = (note->descpos
7604 + offsetof (struct win32_pstatus,
7605 data.thread_info.thread_context));
7606 sect->alignment_power = 2;
7608 if (pstatus.data.thread_info.is_active_thread)
7609 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7610 return FALSE;
7611 break;
7613 case NOTE_INFO_MODULE:
7614 /* Make a ".module/xxxxxxxx" section. */
7615 sprintf (buf, ".module/%08lx",
7616 (long) pstatus.data.module_info.base_address);
7618 len = strlen (buf) + 1;
7619 name = bfd_alloc (abfd, len);
7620 if (name == NULL)
7621 return FALSE;
7623 memcpy (name, buf, len);
7625 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7627 if (sect == NULL)
7628 return FALSE;
7630 sect->size = note->descsz;
7631 sect->filepos = note->descpos;
7632 sect->alignment_power = 2;
7633 break;
7635 default:
7636 return TRUE;
7639 return TRUE;
7641 #endif /* HAVE_WIN32_PSTATUS_T */
7643 static bfd_boolean
7644 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7646 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7648 switch (note->type)
7650 default:
7651 return TRUE;
7653 case NT_PRSTATUS:
7654 if (bed->elf_backend_grok_prstatus)
7655 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7656 return TRUE;
7657 #if defined (HAVE_PRSTATUS_T)
7658 return elfcore_grok_prstatus (abfd, note);
7659 #else
7660 return TRUE;
7661 #endif
7663 #if defined (HAVE_PSTATUS_T)
7664 case NT_PSTATUS:
7665 return elfcore_grok_pstatus (abfd, note);
7666 #endif
7668 #if defined (HAVE_LWPSTATUS_T)
7669 case NT_LWPSTATUS:
7670 return elfcore_grok_lwpstatus (abfd, note);
7671 #endif
7673 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7674 return elfcore_grok_prfpreg (abfd, note);
7676 #if defined (HAVE_WIN32_PSTATUS_T)
7677 case NT_WIN32PSTATUS:
7678 return elfcore_grok_win32pstatus (abfd, note);
7679 #endif
7681 case NT_PRXFPREG: /* Linux SSE extension */
7682 if (note->namesz == 6
7683 && strcmp (note->namedata, "LINUX") == 0)
7684 return elfcore_grok_prxfpreg (abfd, note);
7685 else
7686 return TRUE;
7688 case NT_PRPSINFO:
7689 case NT_PSINFO:
7690 if (bed->elf_backend_grok_psinfo)
7691 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7692 return TRUE;
7693 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7694 return elfcore_grok_psinfo (abfd, note);
7695 #else
7696 return TRUE;
7697 #endif
7699 case NT_AUXV:
7701 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7702 SEC_HAS_CONTENTS);
7704 if (sect == NULL)
7705 return FALSE;
7706 sect->size = note->descsz;
7707 sect->filepos = note->descpos;
7708 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7710 return TRUE;
7715 static bfd_boolean
7716 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7718 char *cp;
7720 cp = strchr (note->namedata, '@');
7721 if (cp != NULL)
7723 *lwpidp = atoi(cp + 1);
7724 return TRUE;
7726 return FALSE;
7729 static bfd_boolean
7730 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
7732 /* Signal number at offset 0x08. */
7733 elf_tdata (abfd)->core_signal
7734 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7736 /* Process ID at offset 0x50. */
7737 elf_tdata (abfd)->core_pid
7738 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7740 /* Command name at 0x7c (max 32 bytes, including nul). */
7741 elf_tdata (abfd)->core_command
7742 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7744 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7745 note);
7748 static bfd_boolean
7749 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
7751 int lwp;
7753 if (elfcore_netbsd_get_lwpid (note, &lwp))
7754 elf_tdata (abfd)->core_lwpid = lwp;
7756 if (note->type == NT_NETBSDCORE_PROCINFO)
7758 /* NetBSD-specific core "procinfo". Note that we expect to
7759 find this note before any of the others, which is fine,
7760 since the kernel writes this note out first when it
7761 creates a core file. */
7763 return elfcore_grok_netbsd_procinfo (abfd, note);
7766 /* As of Jan 2002 there are no other machine-independent notes
7767 defined for NetBSD core files. If the note type is less
7768 than the start of the machine-dependent note types, we don't
7769 understand it. */
7771 if (note->type < NT_NETBSDCORE_FIRSTMACH)
7772 return TRUE;
7775 switch (bfd_get_arch (abfd))
7777 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7778 PT_GETFPREGS == mach+2. */
7780 case bfd_arch_alpha:
7781 case bfd_arch_sparc:
7782 switch (note->type)
7784 case NT_NETBSDCORE_FIRSTMACH+0:
7785 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7787 case NT_NETBSDCORE_FIRSTMACH+2:
7788 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7790 default:
7791 return TRUE;
7794 /* On all other arch's, PT_GETREGS == mach+1 and
7795 PT_GETFPREGS == mach+3. */
7797 default:
7798 switch (note->type)
7800 case NT_NETBSDCORE_FIRSTMACH+1:
7801 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7803 case NT_NETBSDCORE_FIRSTMACH+3:
7804 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7806 default:
7807 return TRUE;
7810 /* NOTREACHED */
7813 static bfd_boolean
7814 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
7816 void *ddata = note->descdata;
7817 char buf[100];
7818 char *name;
7819 asection *sect;
7820 short sig;
7821 unsigned flags;
7823 /* nto_procfs_status 'pid' field is at offset 0. */
7824 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7826 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7827 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7829 /* nto_procfs_status 'flags' field is at offset 8. */
7830 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
7832 /* nto_procfs_status 'what' field is at offset 14. */
7833 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7835 elf_tdata (abfd)->core_signal = sig;
7836 elf_tdata (abfd)->core_lwpid = *tid;
7839 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7840 do not come from signals so we make sure we set the current
7841 thread just in case. */
7842 if (flags & 0x00000080)
7843 elf_tdata (abfd)->core_lwpid = *tid;
7845 /* Make a ".qnx_core_status/%d" section. */
7846 sprintf (buf, ".qnx_core_status/%ld", *tid);
7848 name = bfd_alloc (abfd, strlen (buf) + 1);
7849 if (name == NULL)
7850 return FALSE;
7851 strcpy (name, buf);
7853 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7854 if (sect == NULL)
7855 return FALSE;
7857 sect->size = note->descsz;
7858 sect->filepos = note->descpos;
7859 sect->alignment_power = 2;
7861 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7864 static bfd_boolean
7865 elfcore_grok_nto_regs (bfd *abfd,
7866 Elf_Internal_Note *note,
7867 long tid,
7868 char *base)
7870 char buf[100];
7871 char *name;
7872 asection *sect;
7874 /* Make a "(base)/%d" section. */
7875 sprintf (buf, "%s/%ld", base, tid);
7877 name = bfd_alloc (abfd, strlen (buf) + 1);
7878 if (name == NULL)
7879 return FALSE;
7880 strcpy (name, buf);
7882 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7883 if (sect == NULL)
7884 return FALSE;
7886 sect->size = note->descsz;
7887 sect->filepos = note->descpos;
7888 sect->alignment_power = 2;
7890 /* This is the current thread. */
7891 if (elf_tdata (abfd)->core_lwpid == tid)
7892 return elfcore_maybe_make_sect (abfd, base, sect);
7894 return TRUE;
7897 #define BFD_QNT_CORE_INFO 7
7898 #define BFD_QNT_CORE_STATUS 8
7899 #define BFD_QNT_CORE_GREG 9
7900 #define BFD_QNT_CORE_FPREG 10
7902 static bfd_boolean
7903 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7905 /* Every GREG section has a STATUS section before it. Store the
7906 tid from the previous call to pass down to the next gregs
7907 function. */
7908 static long tid = 1;
7910 switch (note->type)
7912 case BFD_QNT_CORE_INFO:
7913 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7914 case BFD_QNT_CORE_STATUS:
7915 return elfcore_grok_nto_status (abfd, note, &tid);
7916 case BFD_QNT_CORE_GREG:
7917 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
7918 case BFD_QNT_CORE_FPREG:
7919 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
7920 default:
7921 return TRUE;
7925 /* Function: elfcore_write_note
7927 Inputs:
7928 buffer to hold note, and current size of buffer
7929 name of note
7930 type of note
7931 data for note
7932 size of data for note
7934 Writes note to end of buffer. ELF64 notes are written exactly as
7935 for ELF32, despite the current (as of 2006) ELF gabi specifying
7936 that they ought to have 8-byte namesz and descsz field, and have
7937 8-byte alignment. Other writers, eg. Linux kernel, do the same.
7939 Return:
7940 Pointer to realloc'd buffer, *BUFSIZ updated. */
7942 char *
7943 elfcore_write_note (bfd *abfd,
7944 char *buf,
7945 int *bufsiz,
7946 const char *name,
7947 int type,
7948 const void *input,
7949 int size)
7951 Elf_External_Note *xnp;
7952 size_t namesz;
7953 size_t newspace;
7954 char *dest;
7956 namesz = 0;
7957 if (name != NULL)
7958 namesz = strlen (name) + 1;
7960 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
7962 buf = realloc (buf, *bufsiz + newspace);
7963 dest = buf + *bufsiz;
7964 *bufsiz += newspace;
7965 xnp = (Elf_External_Note *) dest;
7966 H_PUT_32 (abfd, namesz, xnp->namesz);
7967 H_PUT_32 (abfd, size, xnp->descsz);
7968 H_PUT_32 (abfd, type, xnp->type);
7969 dest = xnp->name;
7970 if (name != NULL)
7972 memcpy (dest, name, namesz);
7973 dest += namesz;
7974 while (namesz & 3)
7976 *dest++ = '\0';
7977 ++namesz;
7980 memcpy (dest, input, size);
7981 dest += size;
7982 while (size & 3)
7984 *dest++ = '\0';
7985 ++size;
7987 return buf;
7990 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7991 char *
7992 elfcore_write_prpsinfo (bfd *abfd,
7993 char *buf,
7994 int *bufsiz,
7995 const char *fname,
7996 const char *psargs)
7998 const char *note_name = "CORE";
7999 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8001 if (bed->elf_backend_write_core_note != NULL)
8003 char *ret;
8004 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8005 NT_PRPSINFO, fname, psargs);
8006 if (ret != NULL)
8007 return ret;
8010 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8011 if (bed->s->elfclass == ELFCLASS32)
8013 #if defined (HAVE_PSINFO32_T)
8014 psinfo32_t data;
8015 int note_type = NT_PSINFO;
8016 #else
8017 prpsinfo32_t data;
8018 int note_type = NT_PRPSINFO;
8019 #endif
8021 memset (&data, 0, sizeof (data));
8022 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8023 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8024 return elfcore_write_note (abfd, buf, bufsiz,
8025 note_name, note_type, &data, sizeof (data));
8027 else
8028 #endif
8030 #if defined (HAVE_PSINFO_T)
8031 psinfo_t data;
8032 int note_type = NT_PSINFO;
8033 #else
8034 prpsinfo_t data;
8035 int note_type = NT_PRPSINFO;
8036 #endif
8038 memset (&data, 0, sizeof (data));
8039 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8040 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8041 return elfcore_write_note (abfd, buf, bufsiz,
8042 note_name, note_type, &data, sizeof (data));
8045 #endif /* PSINFO_T or PRPSINFO_T */
8047 #if defined (HAVE_PRSTATUS_T)
8048 char *
8049 elfcore_write_prstatus (bfd *abfd,
8050 char *buf,
8051 int *bufsiz,
8052 long pid,
8053 int cursig,
8054 const void *gregs)
8056 const char *note_name = "CORE";
8057 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8059 if (bed->elf_backend_write_core_note != NULL)
8061 char *ret;
8062 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8063 NT_PRSTATUS,
8064 pid, cursig, gregs);
8065 if (ret != NULL)
8066 return ret;
8069 #if defined (HAVE_PRSTATUS32_T)
8070 if (bed->s->elfclass == ELFCLASS32)
8072 prstatus32_t prstat;
8074 memset (&prstat, 0, sizeof (prstat));
8075 prstat.pr_pid = pid;
8076 prstat.pr_cursig = cursig;
8077 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8078 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8079 NT_PRSTATUS, &prstat, sizeof (prstat));
8081 else
8082 #endif
8084 prstatus_t prstat;
8086 memset (&prstat, 0, sizeof (prstat));
8087 prstat.pr_pid = pid;
8088 prstat.pr_cursig = cursig;
8089 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8090 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8091 NT_PRSTATUS, &prstat, sizeof (prstat));
8094 #endif /* HAVE_PRSTATUS_T */
8096 #if defined (HAVE_LWPSTATUS_T)
8097 char *
8098 elfcore_write_lwpstatus (bfd *abfd,
8099 char *buf,
8100 int *bufsiz,
8101 long pid,
8102 int cursig,
8103 const void *gregs)
8105 lwpstatus_t lwpstat;
8106 const char *note_name = "CORE";
8108 memset (&lwpstat, 0, sizeof (lwpstat));
8109 lwpstat.pr_lwpid = pid >> 16;
8110 lwpstat.pr_cursig = cursig;
8111 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8112 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8113 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8114 #if !defined(gregs)
8115 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8116 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8117 #else
8118 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8119 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8120 #endif
8121 #endif
8122 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8123 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8125 #endif /* HAVE_LWPSTATUS_T */
8127 #if defined (HAVE_PSTATUS_T)
8128 char *
8129 elfcore_write_pstatus (bfd *abfd,
8130 char *buf,
8131 int *bufsiz,
8132 long pid,
8133 int cursig ATTRIBUTE_UNUSED,
8134 const void *gregs ATTRIBUTE_UNUSED)
8136 const char *note_name = "CORE";
8137 #if defined (HAVE_PSTATUS32_T)
8138 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8140 if (bed->s->elfclass == ELFCLASS32)
8142 pstatus32_t pstat;
8144 memset (&pstat, 0, sizeof (pstat));
8145 pstat.pr_pid = pid & 0xffff;
8146 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8147 NT_PSTATUS, &pstat, sizeof (pstat));
8148 return buf;
8150 else
8151 #endif
8153 pstatus_t pstat;
8155 memset (&pstat, 0, sizeof (pstat));
8156 pstat.pr_pid = pid & 0xffff;
8157 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8158 NT_PSTATUS, &pstat, sizeof (pstat));
8159 return buf;
8162 #endif /* HAVE_PSTATUS_T */
8164 char *
8165 elfcore_write_prfpreg (bfd *abfd,
8166 char *buf,
8167 int *bufsiz,
8168 const void *fpregs,
8169 int size)
8171 const char *note_name = "CORE";
8172 return elfcore_write_note (abfd, buf, bufsiz,
8173 note_name, NT_FPREGSET, fpregs, size);
8176 char *
8177 elfcore_write_prxfpreg (bfd *abfd,
8178 char *buf,
8179 int *bufsiz,
8180 const void *xfpregs,
8181 int size)
8183 char *note_name = "LINUX";
8184 return elfcore_write_note (abfd, buf, bufsiz,
8185 note_name, NT_PRXFPREG, xfpregs, size);
8188 static bfd_boolean
8189 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8191 char *buf;
8192 char *p;
8194 if (size <= 0)
8195 return TRUE;
8197 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8198 return FALSE;
8200 buf = bfd_malloc (size);
8201 if (buf == NULL)
8202 return FALSE;
8204 if (bfd_bread (buf, size, abfd) != size)
8206 error:
8207 free (buf);
8208 return FALSE;
8211 p = buf;
8212 while (p < buf + size)
8214 /* FIXME: bad alignment assumption. */
8215 Elf_External_Note *xnp = (Elf_External_Note *) p;
8216 Elf_Internal_Note in;
8218 in.type = H_GET_32 (abfd, xnp->type);
8220 in.namesz = H_GET_32 (abfd, xnp->namesz);
8221 in.namedata = xnp->name;
8223 in.descsz = H_GET_32 (abfd, xnp->descsz);
8224 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8225 in.descpos = offset + (in.descdata - buf);
8227 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8229 if (! elfcore_grok_netbsd_note (abfd, &in))
8230 goto error;
8232 else if (CONST_STRNEQ (in.namedata, "QNX"))
8234 if (! elfcore_grok_nto_note (abfd, &in))
8235 goto error;
8237 else
8239 if (! elfcore_grok_note (abfd, &in))
8240 goto error;
8243 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8246 free (buf);
8247 return TRUE;
8250 /* Providing external access to the ELF program header table. */
8252 /* Return an upper bound on the number of bytes required to store a
8253 copy of ABFD's program header table entries. Return -1 if an error
8254 occurs; bfd_get_error will return an appropriate code. */
8256 long
8257 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8259 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8261 bfd_set_error (bfd_error_wrong_format);
8262 return -1;
8265 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8268 /* Copy ABFD's program header table entries to *PHDRS. The entries
8269 will be stored as an array of Elf_Internal_Phdr structures, as
8270 defined in include/elf/internal.h. To find out how large the
8271 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8273 Return the number of program header table entries read, or -1 if an
8274 error occurs; bfd_get_error will return an appropriate code. */
8277 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8279 int num_phdrs;
8281 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8283 bfd_set_error (bfd_error_wrong_format);
8284 return -1;
8287 num_phdrs = elf_elfheader (abfd)->e_phnum;
8288 memcpy (phdrs, elf_tdata (abfd)->phdr,
8289 num_phdrs * sizeof (Elf_Internal_Phdr));
8291 return num_phdrs;
8294 void
8295 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
8297 #ifdef BFD64
8298 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
8300 i_ehdrp = elf_elfheader (abfd);
8301 if (i_ehdrp == NULL)
8302 sprintf_vma (buf, value);
8303 else
8305 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
8307 #if BFD_HOST_64BIT_LONG
8308 sprintf (buf, "%016lx", value);
8309 #else
8310 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
8311 _bfd_int64_low (value));
8312 #endif
8314 else
8315 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
8317 #else
8318 sprintf_vma (buf, value);
8319 #endif
8322 void
8323 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
8325 #ifdef BFD64
8326 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
8328 i_ehdrp = elf_elfheader (abfd);
8329 if (i_ehdrp == NULL)
8330 fprintf_vma ((FILE *) stream, value);
8331 else
8333 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
8335 #if BFD_HOST_64BIT_LONG
8336 fprintf ((FILE *) stream, "%016lx", value);
8337 #else
8338 fprintf ((FILE *) stream, "%08lx%08lx",
8339 _bfd_int64_high (value), _bfd_int64_low (value));
8340 #endif
8342 else
8343 fprintf ((FILE *) stream, "%08lx",
8344 (unsigned long) (value & 0xffffffff));
8346 #else
8347 fprintf_vma ((FILE *) stream, value);
8348 #endif
8351 enum elf_reloc_type_class
8352 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8354 return reloc_class_normal;
8357 /* For RELA architectures, return the relocation value for a
8358 relocation against a local symbol. */
8360 bfd_vma
8361 _bfd_elf_rela_local_sym (bfd *abfd,
8362 Elf_Internal_Sym *sym,
8363 asection **psec,
8364 Elf_Internal_Rela *rel)
8366 asection *sec = *psec;
8367 bfd_vma relocation;
8369 relocation = (sec->output_section->vma
8370 + sec->output_offset
8371 + sym->st_value);
8372 if ((sec->flags & SEC_MERGE)
8373 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8374 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8376 rel->r_addend =
8377 _bfd_merged_section_offset (abfd, psec,
8378 elf_section_data (sec)->sec_info,
8379 sym->st_value + rel->r_addend);
8380 if (sec != *psec)
8382 /* If we have changed the section, and our original section is
8383 marked with SEC_EXCLUDE, it means that the original
8384 SEC_MERGE section has been completely subsumed in some
8385 other SEC_MERGE section. In this case, we need to leave
8386 some info around for --emit-relocs. */
8387 if ((sec->flags & SEC_EXCLUDE) != 0)
8388 sec->kept_section = *psec;
8389 sec = *psec;
8391 rel->r_addend -= relocation;
8392 rel->r_addend += sec->output_section->vma + sec->output_offset;
8394 return relocation;
8397 bfd_vma
8398 _bfd_elf_rel_local_sym (bfd *abfd,
8399 Elf_Internal_Sym *sym,
8400 asection **psec,
8401 bfd_vma addend)
8403 asection *sec = *psec;
8405 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8406 return sym->st_value + addend;
8408 return _bfd_merged_section_offset (abfd, psec,
8409 elf_section_data (sec)->sec_info,
8410 sym->st_value + addend);
8413 bfd_vma
8414 _bfd_elf_section_offset (bfd *abfd,
8415 struct bfd_link_info *info,
8416 asection *sec,
8417 bfd_vma offset)
8419 switch (sec->sec_info_type)
8421 case ELF_INFO_TYPE_STABS:
8422 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8423 offset);
8424 case ELF_INFO_TYPE_EH_FRAME:
8425 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8426 default:
8427 return offset;
8431 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8432 reconstruct an ELF file by reading the segments out of remote memory
8433 based on the ELF file header at EHDR_VMA and the ELF program headers it
8434 points to. If not null, *LOADBASEP is filled in with the difference
8435 between the VMAs from which the segments were read, and the VMAs the
8436 file headers (and hence BFD's idea of each section's VMA) put them at.
8438 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8439 remote memory at target address VMA into the local buffer at MYADDR; it
8440 should return zero on success or an `errno' code on failure. TEMPL must
8441 be a BFD for an ELF target with the word size and byte order found in
8442 the remote memory. */
8444 bfd *
8445 bfd_elf_bfd_from_remote_memory
8446 (bfd *templ,
8447 bfd_vma ehdr_vma,
8448 bfd_vma *loadbasep,
8449 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8451 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8452 (templ, ehdr_vma, loadbasep, target_read_memory);
8455 long
8456 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8457 long symcount ATTRIBUTE_UNUSED,
8458 asymbol **syms ATTRIBUTE_UNUSED,
8459 long dynsymcount,
8460 asymbol **dynsyms,
8461 asymbol **ret)
8463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8464 asection *relplt;
8465 asymbol *s;
8466 const char *relplt_name;
8467 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8468 arelent *p;
8469 long count, i, n;
8470 size_t size;
8471 Elf_Internal_Shdr *hdr;
8472 char *names;
8473 asection *plt;
8475 *ret = NULL;
8477 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8478 return 0;
8480 if (dynsymcount <= 0)
8481 return 0;
8483 if (!bed->plt_sym_val)
8484 return 0;
8486 relplt_name = bed->relplt_name;
8487 if (relplt_name == NULL)
8488 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
8489 relplt = bfd_get_section_by_name (abfd, relplt_name);
8490 if (relplt == NULL)
8491 return 0;
8493 hdr = &elf_section_data (relplt)->this_hdr;
8494 if (hdr->sh_link != elf_dynsymtab (abfd)
8495 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8496 return 0;
8498 plt = bfd_get_section_by_name (abfd, ".plt");
8499 if (plt == NULL)
8500 return 0;
8502 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8503 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8504 return -1;
8506 count = relplt->size / hdr->sh_entsize;
8507 size = count * sizeof (asymbol);
8508 p = relplt->relocation;
8509 for (i = 0; i < count; i++, p++)
8510 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8512 s = *ret = bfd_malloc (size);
8513 if (s == NULL)
8514 return -1;
8516 names = (char *) (s + count);
8517 p = relplt->relocation;
8518 n = 0;
8519 for (i = 0; i < count; i++, s++, p++)
8521 size_t len;
8522 bfd_vma addr;
8524 addr = bed->plt_sym_val (i, plt, p);
8525 if (addr == (bfd_vma) -1)
8526 continue;
8528 *s = **p->sym_ptr_ptr;
8529 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8530 we are defining a symbol, ensure one of them is set. */
8531 if ((s->flags & BSF_LOCAL) == 0)
8532 s->flags |= BSF_GLOBAL;
8533 s->section = plt;
8534 s->value = addr - plt->vma;
8535 s->name = names;
8536 len = strlen ((*p->sym_ptr_ptr)->name);
8537 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8538 names += len;
8539 memcpy (names, "@plt", sizeof ("@plt"));
8540 names += sizeof ("@plt");
8541 ++n;
8544 return n;
8547 /* It is only used by x86-64 so far. */
8548 asection _bfd_elf_large_com_section
8549 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
8550 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
8552 void
8553 _bfd_elf_set_osabi (bfd * abfd,
8554 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8556 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8558 i_ehdrp = elf_elfheader (abfd);
8560 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8564 /* Return TRUE for ELF symbol types that represent functions.
8565 This is the default version of this function, which is sufficient for
8566 most targets. It returns true if TYPE is STT_FUNC. */
8568 bfd_boolean
8569 _bfd_elf_is_function_type (unsigned int type)
8571 return (type == STT_FUNC);