2009-06-10 Philip Blundell <philb@gnu.org>
[binutils.git] / bfd / elf.c
blobc7151be4dd5efb4f0e6a5492631bcd4a9253c1e1
1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
26 SECTION
27 ELF backends
29 BFD support for ELF formats is being worked on.
30 Currently, the best supported back ends are for sparc and i386
31 (running svr4 or Solaris 2).
33 Documentation of the internals of the support code still needs
34 to be written. The code is changing quickly enough that we
35 haven't bothered yet. */
37 /* For sparc64-cross-sparc32. */
38 #define _SYSCALL32
39 #include "sysdep.h"
40 #include "bfd.h"
41 #include "bfdlink.h"
42 #include "libbfd.h"
43 #define ARCH_SIZE 0
44 #include "elf-bfd.h"
45 #include "libiberty.h"
46 #include "safe-ctype.h"
48 static int elf_sort_sections (const void *, const void *);
49 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
50 static bfd_boolean prep_headers (bfd *);
51 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
52 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
53 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
54 file_ptr offset);
56 /* Swap version information in and out. The version information is
57 currently size independent. If that ever changes, this code will
58 need to move into elfcode.h. */
60 /* Swap in a Verdef structure. */
62 void
63 _bfd_elf_swap_verdef_in (bfd *abfd,
64 const Elf_External_Verdef *src,
65 Elf_Internal_Verdef *dst)
67 dst->vd_version = H_GET_16 (abfd, src->vd_version);
68 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
69 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
70 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
71 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
72 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
73 dst->vd_next = H_GET_32 (abfd, src->vd_next);
76 /* Swap out a Verdef structure. */
78 void
79 _bfd_elf_swap_verdef_out (bfd *abfd,
80 const Elf_Internal_Verdef *src,
81 Elf_External_Verdef *dst)
83 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
84 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
85 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
86 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
87 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
88 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
89 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
92 /* Swap in a Verdaux structure. */
94 void
95 _bfd_elf_swap_verdaux_in (bfd *abfd,
96 const Elf_External_Verdaux *src,
97 Elf_Internal_Verdaux *dst)
99 dst->vda_name = H_GET_32 (abfd, src->vda_name);
100 dst->vda_next = H_GET_32 (abfd, src->vda_next);
103 /* Swap out a Verdaux structure. */
105 void
106 _bfd_elf_swap_verdaux_out (bfd *abfd,
107 const Elf_Internal_Verdaux *src,
108 Elf_External_Verdaux *dst)
110 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
111 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
114 /* Swap in a Verneed structure. */
116 void
117 _bfd_elf_swap_verneed_in (bfd *abfd,
118 const Elf_External_Verneed *src,
119 Elf_Internal_Verneed *dst)
121 dst->vn_version = H_GET_16 (abfd, src->vn_version);
122 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
123 dst->vn_file = H_GET_32 (abfd, src->vn_file);
124 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
125 dst->vn_next = H_GET_32 (abfd, src->vn_next);
128 /* Swap out a Verneed structure. */
130 void
131 _bfd_elf_swap_verneed_out (bfd *abfd,
132 const Elf_Internal_Verneed *src,
133 Elf_External_Verneed *dst)
135 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
136 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
137 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
138 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
139 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
142 /* Swap in a Vernaux structure. */
144 void
145 _bfd_elf_swap_vernaux_in (bfd *abfd,
146 const Elf_External_Vernaux *src,
147 Elf_Internal_Vernaux *dst)
149 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
150 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
151 dst->vna_other = H_GET_16 (abfd, src->vna_other);
152 dst->vna_name = H_GET_32 (abfd, src->vna_name);
153 dst->vna_next = H_GET_32 (abfd, src->vna_next);
156 /* Swap out a Vernaux structure. */
158 void
159 _bfd_elf_swap_vernaux_out (bfd *abfd,
160 const Elf_Internal_Vernaux *src,
161 Elf_External_Vernaux *dst)
163 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
164 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
165 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
166 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
167 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
170 /* Swap in a Versym structure. */
172 void
173 _bfd_elf_swap_versym_in (bfd *abfd,
174 const Elf_External_Versym *src,
175 Elf_Internal_Versym *dst)
177 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
180 /* Swap out a Versym structure. */
182 void
183 _bfd_elf_swap_versym_out (bfd *abfd,
184 const Elf_Internal_Versym *src,
185 Elf_External_Versym *dst)
187 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
190 /* Standard ELF hash function. Do not change this function; you will
191 cause invalid hash tables to be generated. */
193 unsigned long
194 bfd_elf_hash (const char *namearg)
196 const unsigned char *name = (const unsigned char *) namearg;
197 unsigned long h = 0;
198 unsigned long g;
199 int ch;
201 while ((ch = *name++) != '\0')
203 h = (h << 4) + ch;
204 if ((g = (h & 0xf0000000)) != 0)
206 h ^= g >> 24;
207 /* The ELF ABI says `h &= ~g', but this is equivalent in
208 this case and on some machines one insn instead of two. */
209 h ^= g;
212 return h & 0xffffffff;
215 /* DT_GNU_HASH hash function. Do not change this function; you will
216 cause invalid hash tables to be generated. */
218 unsigned long
219 bfd_elf_gnu_hash (const char *namearg)
221 const unsigned char *name = (const unsigned char *) namearg;
222 unsigned long h = 5381;
223 unsigned char ch;
225 while ((ch = *name++) != '\0')
226 h = (h << 5) + h + ch;
227 return h & 0xffffffff;
230 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
231 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
232 bfd_boolean
233 bfd_elf_allocate_object (bfd *abfd,
234 size_t object_size,
235 enum elf_object_id object_id)
237 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
238 abfd->tdata.any = bfd_zalloc (abfd, object_size);
239 if (abfd->tdata.any == NULL)
240 return FALSE;
242 elf_object_id (abfd) = object_id;
243 elf_program_header_size (abfd) = (bfd_size_type) -1;
244 return TRUE;
248 bfd_boolean
249 bfd_elf_make_generic_object (bfd *abfd)
251 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
252 GENERIC_ELF_TDATA);
255 bfd_boolean
256 bfd_elf_mkcorefile (bfd *abfd)
258 /* I think this can be done just like an object file. */
259 return bfd_elf_make_generic_object (abfd);
262 static char *
263 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
265 Elf_Internal_Shdr **i_shdrp;
266 bfd_byte *shstrtab = NULL;
267 file_ptr offset;
268 bfd_size_type shstrtabsize;
270 i_shdrp = elf_elfsections (abfd);
271 if (i_shdrp == 0
272 || shindex >= elf_numsections (abfd)
273 || i_shdrp[shindex] == 0)
274 return NULL;
276 shstrtab = i_shdrp[shindex]->contents;
277 if (shstrtab == NULL)
279 /* No cached one, attempt to read, and cache what we read. */
280 offset = i_shdrp[shindex]->sh_offset;
281 shstrtabsize = i_shdrp[shindex]->sh_size;
283 /* Allocate and clear an extra byte at the end, to prevent crashes
284 in case the string table is not terminated. */
285 if (shstrtabsize + 1 <= 1
286 || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL
287 || bfd_seek (abfd, offset, SEEK_SET) != 0)
288 shstrtab = NULL;
289 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
291 if (bfd_get_error () != bfd_error_system_call)
292 bfd_set_error (bfd_error_file_truncated);
293 shstrtab = NULL;
294 /* Once we've failed to read it, make sure we don't keep
295 trying. Otherwise, we'll keep allocating space for
296 the string table over and over. */
297 i_shdrp[shindex]->sh_size = 0;
299 else
300 shstrtab[shstrtabsize] = '\0';
301 i_shdrp[shindex]->contents = shstrtab;
303 return (char *) shstrtab;
306 char *
307 bfd_elf_string_from_elf_section (bfd *abfd,
308 unsigned int shindex,
309 unsigned int strindex)
311 Elf_Internal_Shdr *hdr;
313 if (strindex == 0)
314 return "";
316 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
317 return NULL;
319 hdr = elf_elfsections (abfd)[shindex];
321 if (hdr->contents == NULL
322 && bfd_elf_get_str_section (abfd, shindex) == NULL)
323 return NULL;
325 if (strindex >= hdr->sh_size)
327 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
328 (*_bfd_error_handler)
329 (_("%B: invalid string offset %u >= %lu for section `%s'"),
330 abfd, strindex, (unsigned long) hdr->sh_size,
331 (shindex == shstrndx && strindex == hdr->sh_name
332 ? ".shstrtab"
333 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
334 return "";
337 return ((char *) hdr->contents) + strindex;
340 /* Read and convert symbols to internal format.
341 SYMCOUNT specifies the number of symbols to read, starting from
342 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
343 are non-NULL, they are used to store the internal symbols, external
344 symbols, and symbol section index extensions, respectively.
345 Returns a pointer to the internal symbol buffer (malloced if necessary)
346 or NULL if there were no symbols or some kind of problem. */
348 Elf_Internal_Sym *
349 bfd_elf_get_elf_syms (bfd *ibfd,
350 Elf_Internal_Shdr *symtab_hdr,
351 size_t symcount,
352 size_t symoffset,
353 Elf_Internal_Sym *intsym_buf,
354 void *extsym_buf,
355 Elf_External_Sym_Shndx *extshndx_buf)
357 Elf_Internal_Shdr *shndx_hdr;
358 void *alloc_ext;
359 const bfd_byte *esym;
360 Elf_External_Sym_Shndx *alloc_extshndx;
361 Elf_External_Sym_Shndx *shndx;
362 Elf_Internal_Sym *alloc_intsym;
363 Elf_Internal_Sym *isym;
364 Elf_Internal_Sym *isymend;
365 const struct elf_backend_data *bed;
366 size_t extsym_size;
367 bfd_size_type amt;
368 file_ptr pos;
370 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
371 abort ();
373 if (symcount == 0)
374 return intsym_buf;
376 /* Normal syms might have section extension entries. */
377 shndx_hdr = NULL;
378 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
379 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
381 /* Read the symbols. */
382 alloc_ext = NULL;
383 alloc_extshndx = NULL;
384 alloc_intsym = NULL;
385 bed = get_elf_backend_data (ibfd);
386 extsym_size = bed->s->sizeof_sym;
387 amt = symcount * extsym_size;
388 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
389 if (extsym_buf == NULL)
391 alloc_ext = bfd_malloc2 (symcount, extsym_size);
392 extsym_buf = alloc_ext;
394 if (extsym_buf == NULL
395 || bfd_seek (ibfd, pos, SEEK_SET) != 0
396 || bfd_bread (extsym_buf, amt, ibfd) != amt)
398 intsym_buf = NULL;
399 goto out;
402 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
403 extshndx_buf = NULL;
404 else
406 amt = symcount * sizeof (Elf_External_Sym_Shndx);
407 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
408 if (extshndx_buf == NULL)
410 alloc_extshndx = bfd_malloc2 (symcount,
411 sizeof (Elf_External_Sym_Shndx));
412 extshndx_buf = alloc_extshndx;
414 if (extshndx_buf == NULL
415 || bfd_seek (ibfd, pos, SEEK_SET) != 0
416 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
418 intsym_buf = NULL;
419 goto out;
423 if (intsym_buf == NULL)
425 alloc_intsym = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
426 intsym_buf = alloc_intsym;
427 if (intsym_buf == NULL)
428 goto out;
431 /* Convert the symbols to internal form. */
432 isymend = intsym_buf + symcount;
433 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
434 isym < isymend;
435 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
436 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
438 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
439 (*_bfd_error_handler) (_("%B symbol number %lu references "
440 "nonexistent SHT_SYMTAB_SHNDX section"),
441 ibfd, (unsigned long) symoffset);
442 if (alloc_intsym != NULL)
443 free (alloc_intsym);
444 intsym_buf = NULL;
445 goto out;
448 out:
449 if (alloc_ext != NULL)
450 free (alloc_ext);
451 if (alloc_extshndx != NULL)
452 free (alloc_extshndx);
454 return intsym_buf;
457 /* Look up a symbol name. */
458 const char *
459 bfd_elf_sym_name (bfd *abfd,
460 Elf_Internal_Shdr *symtab_hdr,
461 Elf_Internal_Sym *isym,
462 asection *sym_sec)
464 const char *name;
465 unsigned int iname = isym->st_name;
466 unsigned int shindex = symtab_hdr->sh_link;
468 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
469 /* Check for a bogus st_shndx to avoid crashing. */
470 && isym->st_shndx < elf_numsections (abfd))
472 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
473 shindex = elf_elfheader (abfd)->e_shstrndx;
476 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
477 if (name == NULL)
478 name = "(null)";
479 else if (sym_sec && *name == '\0')
480 name = bfd_section_name (abfd, sym_sec);
482 return name;
485 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
486 sections. The first element is the flags, the rest are section
487 pointers. */
489 typedef union elf_internal_group {
490 Elf_Internal_Shdr *shdr;
491 unsigned int flags;
492 } Elf_Internal_Group;
494 /* Return the name of the group signature symbol. Why isn't the
495 signature just a string? */
497 static const char *
498 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
500 Elf_Internal_Shdr *hdr;
501 unsigned char esym[sizeof (Elf64_External_Sym)];
502 Elf_External_Sym_Shndx eshndx;
503 Elf_Internal_Sym isym;
505 /* First we need to ensure the symbol table is available. Make sure
506 that it is a symbol table section. */
507 if (ghdr->sh_link >= elf_numsections (abfd))
508 return NULL;
509 hdr = elf_elfsections (abfd) [ghdr->sh_link];
510 if (hdr->sh_type != SHT_SYMTAB
511 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
512 return NULL;
514 /* Go read the symbol. */
515 hdr = &elf_tdata (abfd)->symtab_hdr;
516 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
517 &isym, esym, &eshndx) == NULL)
518 return NULL;
520 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
523 /* Set next_in_group list pointer, and group name for NEWSECT. */
525 static bfd_boolean
526 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
528 unsigned int num_group = elf_tdata (abfd)->num_group;
530 /* If num_group is zero, read in all SHT_GROUP sections. The count
531 is set to -1 if there are no SHT_GROUP sections. */
532 if (num_group == 0)
534 unsigned int i, shnum;
536 /* First count the number of groups. If we have a SHT_GROUP
537 section with just a flag word (ie. sh_size is 4), ignore it. */
538 shnum = elf_numsections (abfd);
539 num_group = 0;
541 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
542 ( (shdr)->sh_type == SHT_GROUP \
543 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
544 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
545 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
547 for (i = 0; i < shnum; i++)
549 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
551 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
552 num_group += 1;
555 if (num_group == 0)
557 num_group = (unsigned) -1;
558 elf_tdata (abfd)->num_group = num_group;
560 else
562 /* We keep a list of elf section headers for group sections,
563 so we can find them quickly. */
564 bfd_size_type amt;
566 elf_tdata (abfd)->num_group = num_group;
567 elf_tdata (abfd)->group_sect_ptr
568 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
569 if (elf_tdata (abfd)->group_sect_ptr == NULL)
570 return FALSE;
572 num_group = 0;
573 for (i = 0; i < shnum; i++)
575 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
577 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
579 unsigned char *src;
580 Elf_Internal_Group *dest;
582 /* Add to list of sections. */
583 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
584 num_group += 1;
586 /* Read the raw contents. */
587 BFD_ASSERT (sizeof (*dest) >= 4);
588 amt = shdr->sh_size * sizeof (*dest) / 4;
589 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
590 sizeof (*dest) / 4);
591 /* PR binutils/4110: Handle corrupt group headers. */
592 if (shdr->contents == NULL)
594 _bfd_error_handler
595 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
596 bfd_set_error (bfd_error_bad_value);
597 return FALSE;
600 memset (shdr->contents, 0, amt);
602 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
603 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
604 != shdr->sh_size))
605 return FALSE;
607 /* Translate raw contents, a flag word followed by an
608 array of elf section indices all in target byte order,
609 to the flag word followed by an array of elf section
610 pointers. */
611 src = shdr->contents + shdr->sh_size;
612 dest = (Elf_Internal_Group *) (shdr->contents + amt);
613 while (1)
615 unsigned int idx;
617 src -= 4;
618 --dest;
619 idx = H_GET_32 (abfd, src);
620 if (src == shdr->contents)
622 dest->flags = idx;
623 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
624 shdr->bfd_section->flags
625 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
626 break;
628 if (idx >= shnum)
630 ((*_bfd_error_handler)
631 (_("%B: invalid SHT_GROUP entry"), abfd));
632 idx = 0;
634 dest->shdr = elf_elfsections (abfd)[idx];
641 if (num_group != (unsigned) -1)
643 unsigned int i;
645 for (i = 0; i < num_group; i++)
647 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
648 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
649 unsigned int n_elt = shdr->sh_size / 4;
651 /* Look through this group's sections to see if current
652 section is a member. */
653 while (--n_elt != 0)
654 if ((++idx)->shdr == hdr)
656 asection *s = NULL;
658 /* We are a member of this group. Go looking through
659 other members to see if any others are linked via
660 next_in_group. */
661 idx = (Elf_Internal_Group *) shdr->contents;
662 n_elt = shdr->sh_size / 4;
663 while (--n_elt != 0)
664 if ((s = (++idx)->shdr->bfd_section) != NULL
665 && elf_next_in_group (s) != NULL)
666 break;
667 if (n_elt != 0)
669 /* Snarf the group name from other member, and
670 insert current section in circular list. */
671 elf_group_name (newsect) = elf_group_name (s);
672 elf_next_in_group (newsect) = elf_next_in_group (s);
673 elf_next_in_group (s) = newsect;
675 else
677 const char *gname;
679 gname = group_signature (abfd, shdr);
680 if (gname == NULL)
681 return FALSE;
682 elf_group_name (newsect) = gname;
684 /* Start a circular list with one element. */
685 elf_next_in_group (newsect) = newsect;
688 /* If the group section has been created, point to the
689 new member. */
690 if (shdr->bfd_section != NULL)
691 elf_next_in_group (shdr->bfd_section) = newsect;
693 i = num_group - 1;
694 break;
699 if (elf_group_name (newsect) == NULL)
701 (*_bfd_error_handler) (_("%B: no group info for section %A"),
702 abfd, newsect);
704 return TRUE;
707 bfd_boolean
708 _bfd_elf_setup_sections (bfd *abfd)
710 unsigned int i;
711 unsigned int num_group = elf_tdata (abfd)->num_group;
712 bfd_boolean result = TRUE;
713 asection *s;
715 /* Process SHF_LINK_ORDER. */
716 for (s = abfd->sections; s != NULL; s = s->next)
718 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
719 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
721 unsigned int elfsec = this_hdr->sh_link;
722 /* FIXME: The old Intel compiler and old strip/objcopy may
723 not set the sh_link or sh_info fields. Hence we could
724 get the situation where elfsec is 0. */
725 if (elfsec == 0)
727 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
728 if (bed->link_order_error_handler)
729 bed->link_order_error_handler
730 (_("%B: warning: sh_link not set for section `%A'"),
731 abfd, s);
733 else
735 asection *link = NULL;
737 if (elfsec < elf_numsections (abfd))
739 this_hdr = elf_elfsections (abfd)[elfsec];
740 link = this_hdr->bfd_section;
743 /* PR 1991, 2008:
744 Some strip/objcopy may leave an incorrect value in
745 sh_link. We don't want to proceed. */
746 if (link == NULL)
748 (*_bfd_error_handler)
749 (_("%B: sh_link [%d] in section `%A' is incorrect"),
750 s->owner, s, elfsec);
751 result = FALSE;
754 elf_linked_to_section (s) = link;
759 /* Process section groups. */
760 if (num_group == (unsigned) -1)
761 return result;
763 for (i = 0; i < num_group; i++)
765 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
766 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
767 unsigned int n_elt = shdr->sh_size / 4;
769 while (--n_elt != 0)
770 if ((++idx)->shdr->bfd_section)
771 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
772 else if (idx->shdr->sh_type == SHT_RELA
773 || idx->shdr->sh_type == SHT_REL)
774 /* We won't include relocation sections in section groups in
775 output object files. We adjust the group section size here
776 so that relocatable link will work correctly when
777 relocation sections are in section group in input object
778 files. */
779 shdr->bfd_section->size -= 4;
780 else
782 /* There are some unknown sections in the group. */
783 (*_bfd_error_handler)
784 (_("%B: unknown [%d] section `%s' in group [%s]"),
785 abfd,
786 (unsigned int) idx->shdr->sh_type,
787 bfd_elf_string_from_elf_section (abfd,
788 (elf_elfheader (abfd)
789 ->e_shstrndx),
790 idx->shdr->sh_name),
791 shdr->bfd_section->name);
792 result = FALSE;
795 return result;
798 bfd_boolean
799 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
801 return elf_next_in_group (sec) != NULL;
804 /* Make a BFD section from an ELF section. We store a pointer to the
805 BFD section in the bfd_section field of the header. */
807 bfd_boolean
808 _bfd_elf_make_section_from_shdr (bfd *abfd,
809 Elf_Internal_Shdr *hdr,
810 const char *name,
811 int shindex)
813 asection *newsect;
814 flagword flags;
815 const struct elf_backend_data *bed;
817 if (hdr->bfd_section != NULL)
819 BFD_ASSERT (strcmp (name,
820 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
821 return TRUE;
824 newsect = bfd_make_section_anyway (abfd, name);
825 if (newsect == NULL)
826 return FALSE;
828 hdr->bfd_section = newsect;
829 elf_section_data (newsect)->this_hdr = *hdr;
830 elf_section_data (newsect)->this_idx = shindex;
832 /* Always use the real type/flags. */
833 elf_section_type (newsect) = hdr->sh_type;
834 elf_section_flags (newsect) = hdr->sh_flags;
836 newsect->filepos = hdr->sh_offset;
838 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
839 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
840 || ! bfd_set_section_alignment (abfd, newsect,
841 bfd_log2 (hdr->sh_addralign)))
842 return FALSE;
844 flags = SEC_NO_FLAGS;
845 if (hdr->sh_type != SHT_NOBITS)
846 flags |= SEC_HAS_CONTENTS;
847 if (hdr->sh_type == SHT_GROUP)
848 flags |= SEC_GROUP | SEC_EXCLUDE;
849 if ((hdr->sh_flags & SHF_ALLOC) != 0)
851 flags |= SEC_ALLOC;
852 if (hdr->sh_type != SHT_NOBITS)
853 flags |= SEC_LOAD;
855 if ((hdr->sh_flags & SHF_WRITE) == 0)
856 flags |= SEC_READONLY;
857 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
858 flags |= SEC_CODE;
859 else if ((flags & SEC_LOAD) != 0)
860 flags |= SEC_DATA;
861 if ((hdr->sh_flags & SHF_MERGE) != 0)
863 flags |= SEC_MERGE;
864 newsect->entsize = hdr->sh_entsize;
865 if ((hdr->sh_flags & SHF_STRINGS) != 0)
866 flags |= SEC_STRINGS;
868 if (hdr->sh_flags & SHF_GROUP)
869 if (!setup_group (abfd, hdr, newsect))
870 return FALSE;
871 if ((hdr->sh_flags & SHF_TLS) != 0)
872 flags |= SEC_THREAD_LOCAL;
874 if ((flags & SEC_ALLOC) == 0)
876 /* The debugging sections appear to be recognized only by name,
877 not any sort of flag. Their SEC_ALLOC bits are cleared. */
878 static const struct
880 const char *name;
881 int len;
882 } debug_sections [] =
884 { STRING_COMMA_LEN ("debug") }, /* 'd' */
885 { NULL, 0 }, /* 'e' */
886 { NULL, 0 }, /* 'f' */
887 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
888 { NULL, 0 }, /* 'h' */
889 { NULL, 0 }, /* 'i' */
890 { NULL, 0 }, /* 'j' */
891 { NULL, 0 }, /* 'k' */
892 { STRING_COMMA_LEN ("line") }, /* 'l' */
893 { NULL, 0 }, /* 'm' */
894 { NULL, 0 }, /* 'n' */
895 { NULL, 0 }, /* 'o' */
896 { NULL, 0 }, /* 'p' */
897 { NULL, 0 }, /* 'q' */
898 { NULL, 0 }, /* 'r' */
899 { STRING_COMMA_LEN ("stab") }, /* 's' */
900 { NULL, 0 }, /* 't' */
901 { NULL, 0 }, /* 'u' */
902 { NULL, 0 }, /* 'v' */
903 { NULL, 0 }, /* 'w' */
904 { NULL, 0 }, /* 'x' */
905 { NULL, 0 }, /* 'y' */
906 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
909 if (name [0] == '.')
911 int i = name [1] - 'd';
912 if (i >= 0
913 && i < (int) ARRAY_SIZE (debug_sections)
914 && debug_sections [i].name != NULL
915 && strncmp (&name [1], debug_sections [i].name,
916 debug_sections [i].len) == 0)
917 flags |= SEC_DEBUGGING;
921 /* As a GNU extension, if the name begins with .gnu.linkonce, we
922 only link a single copy of the section. This is used to support
923 g++. g++ will emit each template expansion in its own section.
924 The symbols will be defined as weak, so that multiple definitions
925 are permitted. The GNU linker extension is to actually discard
926 all but one of the sections. */
927 if (CONST_STRNEQ (name, ".gnu.linkonce")
928 && elf_next_in_group (newsect) == NULL)
929 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
931 bed = get_elf_backend_data (abfd);
932 if (bed->elf_backend_section_flags)
933 if (! bed->elf_backend_section_flags (&flags, hdr))
934 return FALSE;
936 if (! bfd_set_section_flags (abfd, newsect, flags))
937 return FALSE;
939 /* We do not parse the PT_NOTE segments as we are interested even in the
940 separate debug info files which may have the segments offsets corrupted.
941 PT_NOTEs from the core files are currently not parsed using BFD. */
942 if (hdr->sh_type == SHT_NOTE)
944 bfd_byte *contents;
946 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
947 return FALSE;
949 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
950 free (contents);
953 if ((flags & SEC_ALLOC) != 0)
955 Elf_Internal_Phdr *phdr;
956 unsigned int i, nload;
958 /* Some ELF linkers produce binaries with all the program header
959 p_paddr fields zero. If we have such a binary with more than
960 one PT_LOAD header, then leave the section lma equal to vma
961 so that we don't create sections with overlapping lma. */
962 phdr = elf_tdata (abfd)->phdr;
963 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
964 if (phdr->p_paddr != 0)
965 break;
966 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
967 ++nload;
968 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
969 return TRUE;
971 phdr = elf_tdata (abfd)->phdr;
972 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
974 /* This section is part of this segment if its file
975 offset plus size lies within the segment's memory
976 span and, if the section is loaded, the extent of the
977 loaded data lies within the extent of the segment.
979 Note - we used to check the p_paddr field as well, and
980 refuse to set the LMA if it was 0. This is wrong
981 though, as a perfectly valid initialised segment can
982 have a p_paddr of zero. Some architectures, eg ARM,
983 place special significance on the address 0 and
984 executables need to be able to have a segment which
985 covers this address. */
986 if (phdr->p_type == PT_LOAD
987 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
988 && (hdr->sh_offset + hdr->sh_size
989 <= phdr->p_offset + phdr->p_memsz)
990 && ((flags & SEC_LOAD) == 0
991 || (hdr->sh_offset + hdr->sh_size
992 <= phdr->p_offset + phdr->p_filesz)))
994 if ((flags & SEC_LOAD) == 0)
995 newsect->lma = (phdr->p_paddr
996 + hdr->sh_addr - phdr->p_vaddr);
997 else
998 /* We used to use the same adjustment for SEC_LOAD
999 sections, but that doesn't work if the segment
1000 is packed with code from multiple VMAs.
1001 Instead we calculate the section LMA based on
1002 the segment LMA. It is assumed that the
1003 segment will contain sections with contiguous
1004 LMAs, even if the VMAs are not. */
1005 newsect->lma = (phdr->p_paddr
1006 + hdr->sh_offset - phdr->p_offset);
1008 /* With contiguous segments, we can't tell from file
1009 offsets whether a section with zero size should
1010 be placed at the end of one segment or the
1011 beginning of the next. Decide based on vaddr. */
1012 if (hdr->sh_addr >= phdr->p_vaddr
1013 && (hdr->sh_addr + hdr->sh_size
1014 <= phdr->p_vaddr + phdr->p_memsz))
1015 break;
1020 return TRUE;
1023 const char *const bfd_elf_section_type_names[] = {
1024 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1025 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1026 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1029 /* ELF relocs are against symbols. If we are producing relocatable
1030 output, and the reloc is against an external symbol, and nothing
1031 has given us any additional addend, the resulting reloc will also
1032 be against the same symbol. In such a case, we don't want to
1033 change anything about the way the reloc is handled, since it will
1034 all be done at final link time. Rather than put special case code
1035 into bfd_perform_relocation, all the reloc types use this howto
1036 function. It just short circuits the reloc if producing
1037 relocatable output against an external symbol. */
1039 bfd_reloc_status_type
1040 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1041 arelent *reloc_entry,
1042 asymbol *symbol,
1043 void *data ATTRIBUTE_UNUSED,
1044 asection *input_section,
1045 bfd *output_bfd,
1046 char **error_message ATTRIBUTE_UNUSED)
1048 if (output_bfd != NULL
1049 && (symbol->flags & BSF_SECTION_SYM) == 0
1050 && (! reloc_entry->howto->partial_inplace
1051 || reloc_entry->addend == 0))
1053 reloc_entry->address += input_section->output_offset;
1054 return bfd_reloc_ok;
1057 return bfd_reloc_continue;
1060 /* Copy the program header and other data from one object module to
1061 another. */
1063 bfd_boolean
1064 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1066 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1067 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1068 return TRUE;
1070 BFD_ASSERT (!elf_flags_init (obfd)
1071 || (elf_elfheader (obfd)->e_flags
1072 == elf_elfheader (ibfd)->e_flags));
1074 elf_gp (obfd) = elf_gp (ibfd);
1075 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1076 elf_flags_init (obfd) = TRUE;
1078 /* Copy object attributes. */
1079 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1081 return TRUE;
1084 static const char *
1085 get_segment_type (unsigned int p_type)
1087 const char *pt;
1088 switch (p_type)
1090 case PT_NULL: pt = "NULL"; break;
1091 case PT_LOAD: pt = "LOAD"; break;
1092 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1093 case PT_INTERP: pt = "INTERP"; break;
1094 case PT_NOTE: pt = "NOTE"; break;
1095 case PT_SHLIB: pt = "SHLIB"; break;
1096 case PT_PHDR: pt = "PHDR"; break;
1097 case PT_TLS: pt = "TLS"; break;
1098 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1099 case PT_GNU_STACK: pt = "STACK"; break;
1100 case PT_GNU_RELRO: pt = "RELRO"; break;
1101 default: pt = NULL; break;
1103 return pt;
1106 /* Print out the program headers. */
1108 bfd_boolean
1109 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1111 FILE *f = farg;
1112 Elf_Internal_Phdr *p;
1113 asection *s;
1114 bfd_byte *dynbuf = NULL;
1116 p = elf_tdata (abfd)->phdr;
1117 if (p != NULL)
1119 unsigned int i, c;
1121 fprintf (f, _("\nProgram Header:\n"));
1122 c = elf_elfheader (abfd)->e_phnum;
1123 for (i = 0; i < c; i++, p++)
1125 const char *pt = get_segment_type (p->p_type);
1126 char buf[20];
1128 if (pt == NULL)
1130 sprintf (buf, "0x%lx", p->p_type);
1131 pt = buf;
1133 fprintf (f, "%8s off 0x", pt);
1134 bfd_fprintf_vma (abfd, f, p->p_offset);
1135 fprintf (f, " vaddr 0x");
1136 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1137 fprintf (f, " paddr 0x");
1138 bfd_fprintf_vma (abfd, f, p->p_paddr);
1139 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1140 fprintf (f, " filesz 0x");
1141 bfd_fprintf_vma (abfd, f, p->p_filesz);
1142 fprintf (f, " memsz 0x");
1143 bfd_fprintf_vma (abfd, f, p->p_memsz);
1144 fprintf (f, " flags %c%c%c",
1145 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1146 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1147 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1148 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1149 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1150 fprintf (f, "\n");
1154 s = bfd_get_section_by_name (abfd, ".dynamic");
1155 if (s != NULL)
1157 unsigned int elfsec;
1158 unsigned long shlink;
1159 bfd_byte *extdyn, *extdynend;
1160 size_t extdynsize;
1161 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1163 fprintf (f, _("\nDynamic Section:\n"));
1165 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1166 goto error_return;
1168 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1169 if (elfsec == SHN_BAD)
1170 goto error_return;
1171 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1173 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1174 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1176 extdyn = dynbuf;
1177 extdynend = extdyn + s->size;
1178 for (; extdyn < extdynend; extdyn += extdynsize)
1180 Elf_Internal_Dyn dyn;
1181 const char *name = "";
1182 char ab[20];
1183 bfd_boolean stringp;
1184 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1186 (*swap_dyn_in) (abfd, extdyn, &dyn);
1188 if (dyn.d_tag == DT_NULL)
1189 break;
1191 stringp = FALSE;
1192 switch (dyn.d_tag)
1194 default:
1195 if (bed->elf_backend_get_target_dtag)
1196 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1198 if (!strcmp (name, ""))
1200 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1201 name = ab;
1203 break;
1205 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1206 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1207 case DT_PLTGOT: name = "PLTGOT"; break;
1208 case DT_HASH: name = "HASH"; break;
1209 case DT_STRTAB: name = "STRTAB"; break;
1210 case DT_SYMTAB: name = "SYMTAB"; break;
1211 case DT_RELA: name = "RELA"; break;
1212 case DT_RELASZ: name = "RELASZ"; break;
1213 case DT_RELAENT: name = "RELAENT"; break;
1214 case DT_STRSZ: name = "STRSZ"; break;
1215 case DT_SYMENT: name = "SYMENT"; break;
1216 case DT_INIT: name = "INIT"; break;
1217 case DT_FINI: name = "FINI"; break;
1218 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1219 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1220 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1221 case DT_REL: name = "REL"; break;
1222 case DT_RELSZ: name = "RELSZ"; break;
1223 case DT_RELENT: name = "RELENT"; break;
1224 case DT_PLTREL: name = "PLTREL"; break;
1225 case DT_DEBUG: name = "DEBUG"; break;
1226 case DT_TEXTREL: name = "TEXTREL"; break;
1227 case DT_JMPREL: name = "JMPREL"; break;
1228 case DT_BIND_NOW: name = "BIND_NOW"; break;
1229 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1230 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1231 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1232 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1233 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1234 case DT_FLAGS: name = "FLAGS"; break;
1235 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1236 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1237 case DT_CHECKSUM: name = "CHECKSUM"; break;
1238 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1239 case DT_MOVEENT: name = "MOVEENT"; break;
1240 case DT_MOVESZ: name = "MOVESZ"; break;
1241 case DT_FEATURE: name = "FEATURE"; break;
1242 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1243 case DT_SYMINSZ: name = "SYMINSZ"; break;
1244 case DT_SYMINENT: name = "SYMINENT"; break;
1245 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1246 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1247 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1248 case DT_PLTPAD: name = "PLTPAD"; break;
1249 case DT_MOVETAB: name = "MOVETAB"; break;
1250 case DT_SYMINFO: name = "SYMINFO"; break;
1251 case DT_RELACOUNT: name = "RELACOUNT"; break;
1252 case DT_RELCOUNT: name = "RELCOUNT"; break;
1253 case DT_FLAGS_1: name = "FLAGS_1"; break;
1254 case DT_VERSYM: name = "VERSYM"; break;
1255 case DT_VERDEF: name = "VERDEF"; break;
1256 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1257 case DT_VERNEED: name = "VERNEED"; break;
1258 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1259 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1260 case DT_USED: name = "USED"; break;
1261 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1262 case DT_GNU_HASH: name = "GNU_HASH"; break;
1265 fprintf (f, " %-20s ", name);
1266 if (! stringp)
1268 fprintf (f, "0x");
1269 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1271 else
1273 const char *string;
1274 unsigned int tagv = dyn.d_un.d_val;
1276 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1277 if (string == NULL)
1278 goto error_return;
1279 fprintf (f, "%s", string);
1281 fprintf (f, "\n");
1284 free (dynbuf);
1285 dynbuf = NULL;
1288 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1289 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1291 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1292 return FALSE;
1295 if (elf_dynverdef (abfd) != 0)
1297 Elf_Internal_Verdef *t;
1299 fprintf (f, _("\nVersion definitions:\n"));
1300 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1302 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1303 t->vd_flags, t->vd_hash,
1304 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1305 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1307 Elf_Internal_Verdaux *a;
1309 fprintf (f, "\t");
1310 for (a = t->vd_auxptr->vda_nextptr;
1311 a != NULL;
1312 a = a->vda_nextptr)
1313 fprintf (f, "%s ",
1314 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1315 fprintf (f, "\n");
1320 if (elf_dynverref (abfd) != 0)
1322 Elf_Internal_Verneed *t;
1324 fprintf (f, _("\nVersion References:\n"));
1325 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1327 Elf_Internal_Vernaux *a;
1329 fprintf (f, _(" required from %s:\n"),
1330 t->vn_filename ? t->vn_filename : "<corrupt>");
1331 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1332 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1333 a->vna_flags, a->vna_other,
1334 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1338 return TRUE;
1340 error_return:
1341 if (dynbuf != NULL)
1342 free (dynbuf);
1343 return FALSE;
1346 /* Display ELF-specific fields of a symbol. */
1348 void
1349 bfd_elf_print_symbol (bfd *abfd,
1350 void *filep,
1351 asymbol *symbol,
1352 bfd_print_symbol_type how)
1354 FILE *file = filep;
1355 switch (how)
1357 case bfd_print_symbol_name:
1358 fprintf (file, "%s", symbol->name);
1359 break;
1360 case bfd_print_symbol_more:
1361 fprintf (file, "elf ");
1362 bfd_fprintf_vma (abfd, file, symbol->value);
1363 fprintf (file, " %lx", (unsigned long) symbol->flags);
1364 break;
1365 case bfd_print_symbol_all:
1367 const char *section_name;
1368 const char *name = NULL;
1369 const struct elf_backend_data *bed;
1370 unsigned char st_other;
1371 bfd_vma val;
1373 section_name = symbol->section ? symbol->section->name : "(*none*)";
1375 bed = get_elf_backend_data (abfd);
1376 if (bed->elf_backend_print_symbol_all)
1377 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1379 if (name == NULL)
1381 name = symbol->name;
1382 bfd_print_symbol_vandf (abfd, file, symbol);
1385 fprintf (file, " %s\t", section_name);
1386 /* Print the "other" value for a symbol. For common symbols,
1387 we've already printed the size; now print the alignment.
1388 For other symbols, we have no specified alignment, and
1389 we've printed the address; now print the size. */
1390 if (symbol->section && bfd_is_com_section (symbol->section))
1391 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1392 else
1393 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1394 bfd_fprintf_vma (abfd, file, val);
1396 /* If we have version information, print it. */
1397 if (elf_tdata (abfd)->dynversym_section != 0
1398 && (elf_tdata (abfd)->dynverdef_section != 0
1399 || elf_tdata (abfd)->dynverref_section != 0))
1401 unsigned int vernum;
1402 const char *version_string;
1404 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1406 if (vernum == 0)
1407 version_string = "";
1408 else if (vernum == 1)
1409 version_string = "Base";
1410 else if (vernum <= elf_tdata (abfd)->cverdefs)
1411 version_string =
1412 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1413 else
1415 Elf_Internal_Verneed *t;
1417 version_string = "";
1418 for (t = elf_tdata (abfd)->verref;
1419 t != NULL;
1420 t = t->vn_nextref)
1422 Elf_Internal_Vernaux *a;
1424 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1426 if (a->vna_other == vernum)
1428 version_string = a->vna_nodename;
1429 break;
1435 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1436 fprintf (file, " %-11s", version_string);
1437 else
1439 int i;
1441 fprintf (file, " (%s)", version_string);
1442 for (i = 10 - strlen (version_string); i > 0; --i)
1443 putc (' ', file);
1447 /* If the st_other field is not zero, print it. */
1448 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1450 switch (st_other)
1452 case 0: break;
1453 case STV_INTERNAL: fprintf (file, " .internal"); break;
1454 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1455 case STV_PROTECTED: fprintf (file, " .protected"); break;
1456 default:
1457 /* Some other non-defined flags are also present, so print
1458 everything hex. */
1459 fprintf (file, " 0x%02x", (unsigned int) st_other);
1462 fprintf (file, " %s", name);
1464 break;
1468 /* Allocate an ELF string table--force the first byte to be zero. */
1470 struct bfd_strtab_hash *
1471 _bfd_elf_stringtab_init (void)
1473 struct bfd_strtab_hash *ret;
1475 ret = _bfd_stringtab_init ();
1476 if (ret != NULL)
1478 bfd_size_type loc;
1480 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1481 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1482 if (loc == (bfd_size_type) -1)
1484 _bfd_stringtab_free (ret);
1485 ret = NULL;
1488 return ret;
1491 /* ELF .o/exec file reading */
1493 /* Create a new bfd section from an ELF section header. */
1495 bfd_boolean
1496 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1498 Elf_Internal_Shdr *hdr;
1499 Elf_Internal_Ehdr *ehdr;
1500 const struct elf_backend_data *bed;
1501 const char *name;
1503 if (shindex >= elf_numsections (abfd))
1504 return FALSE;
1506 hdr = elf_elfsections (abfd)[shindex];
1507 ehdr = elf_elfheader (abfd);
1508 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1509 hdr->sh_name);
1510 if (name == NULL)
1511 return FALSE;
1513 bed = get_elf_backend_data (abfd);
1514 switch (hdr->sh_type)
1516 case SHT_NULL:
1517 /* Inactive section. Throw it away. */
1518 return TRUE;
1520 case SHT_PROGBITS: /* Normal section with contents. */
1521 case SHT_NOBITS: /* .bss section. */
1522 case SHT_HASH: /* .hash section. */
1523 case SHT_NOTE: /* .note section. */
1524 case SHT_INIT_ARRAY: /* .init_array section. */
1525 case SHT_FINI_ARRAY: /* .fini_array section. */
1526 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1527 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1528 case SHT_GNU_HASH: /* .gnu.hash section. */
1529 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1531 case SHT_DYNAMIC: /* Dynamic linking information. */
1532 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1533 return FALSE;
1534 if (hdr->sh_link > elf_numsections (abfd)
1535 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1536 return FALSE;
1537 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1539 Elf_Internal_Shdr *dynsymhdr;
1541 /* The shared libraries distributed with hpux11 have a bogus
1542 sh_link field for the ".dynamic" section. Find the
1543 string table for the ".dynsym" section instead. */
1544 if (elf_dynsymtab (abfd) != 0)
1546 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1547 hdr->sh_link = dynsymhdr->sh_link;
1549 else
1551 unsigned int i, num_sec;
1553 num_sec = elf_numsections (abfd);
1554 for (i = 1; i < num_sec; i++)
1556 dynsymhdr = elf_elfsections (abfd)[i];
1557 if (dynsymhdr->sh_type == SHT_DYNSYM)
1559 hdr->sh_link = dynsymhdr->sh_link;
1560 break;
1565 break;
1567 case SHT_SYMTAB: /* A symbol table */
1568 if (elf_onesymtab (abfd) == shindex)
1569 return TRUE;
1571 if (hdr->sh_entsize != bed->s->sizeof_sym)
1572 return FALSE;
1573 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1574 return FALSE;
1575 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1576 elf_onesymtab (abfd) = shindex;
1577 elf_tdata (abfd)->symtab_hdr = *hdr;
1578 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1579 abfd->flags |= HAS_SYMS;
1581 /* Sometimes a shared object will map in the symbol table. If
1582 SHF_ALLOC is set, and this is a shared object, then we also
1583 treat this section as a BFD section. We can not base the
1584 decision purely on SHF_ALLOC, because that flag is sometimes
1585 set in a relocatable object file, which would confuse the
1586 linker. */
1587 if ((hdr->sh_flags & SHF_ALLOC) != 0
1588 && (abfd->flags & DYNAMIC) != 0
1589 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1590 shindex))
1591 return FALSE;
1593 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1594 can't read symbols without that section loaded as well. It
1595 is most likely specified by the next section header. */
1596 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1598 unsigned int i, num_sec;
1600 num_sec = elf_numsections (abfd);
1601 for (i = shindex + 1; i < num_sec; i++)
1603 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1604 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1605 && hdr2->sh_link == shindex)
1606 break;
1608 if (i == num_sec)
1609 for (i = 1; i < shindex; i++)
1611 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1612 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1613 && hdr2->sh_link == shindex)
1614 break;
1616 if (i != shindex)
1617 return bfd_section_from_shdr (abfd, i);
1619 return TRUE;
1621 case SHT_DYNSYM: /* A dynamic symbol table */
1622 if (elf_dynsymtab (abfd) == shindex)
1623 return TRUE;
1625 if (hdr->sh_entsize != bed->s->sizeof_sym)
1626 return FALSE;
1627 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1628 elf_dynsymtab (abfd) = shindex;
1629 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1630 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1631 abfd->flags |= HAS_SYMS;
1633 /* Besides being a symbol table, we also treat this as a regular
1634 section, so that objcopy can handle it. */
1635 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1637 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1638 if (elf_symtab_shndx (abfd) == shindex)
1639 return TRUE;
1641 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1642 elf_symtab_shndx (abfd) = shindex;
1643 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1644 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1645 return TRUE;
1647 case SHT_STRTAB: /* A string table */
1648 if (hdr->bfd_section != NULL)
1649 return TRUE;
1650 if (ehdr->e_shstrndx == shindex)
1652 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1653 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1654 return TRUE;
1656 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1658 symtab_strtab:
1659 elf_tdata (abfd)->strtab_hdr = *hdr;
1660 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1661 return TRUE;
1663 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1665 dynsymtab_strtab:
1666 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1667 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1668 elf_elfsections (abfd)[shindex] = hdr;
1669 /* We also treat this as a regular section, so that objcopy
1670 can handle it. */
1671 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1672 shindex);
1675 /* If the string table isn't one of the above, then treat it as a
1676 regular section. We need to scan all the headers to be sure,
1677 just in case this strtab section appeared before the above. */
1678 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1680 unsigned int i, num_sec;
1682 num_sec = elf_numsections (abfd);
1683 for (i = 1; i < num_sec; i++)
1685 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1686 if (hdr2->sh_link == shindex)
1688 /* Prevent endless recursion on broken objects. */
1689 if (i == shindex)
1690 return FALSE;
1691 if (! bfd_section_from_shdr (abfd, i))
1692 return FALSE;
1693 if (elf_onesymtab (abfd) == i)
1694 goto symtab_strtab;
1695 if (elf_dynsymtab (abfd) == i)
1696 goto dynsymtab_strtab;
1700 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1702 case SHT_REL:
1703 case SHT_RELA:
1704 /* *These* do a lot of work -- but build no sections! */
1706 asection *target_sect;
1707 Elf_Internal_Shdr *hdr2;
1708 unsigned int num_sec = elf_numsections (abfd);
1710 if (hdr->sh_entsize
1711 != (bfd_size_type) (hdr->sh_type == SHT_REL
1712 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1713 return FALSE;
1715 /* Check for a bogus link to avoid crashing. */
1716 if (hdr->sh_link >= num_sec)
1718 ((*_bfd_error_handler)
1719 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1720 abfd, hdr->sh_link, name, shindex));
1721 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1722 shindex);
1725 /* For some incomprehensible reason Oracle distributes
1726 libraries for Solaris in which some of the objects have
1727 bogus sh_link fields. It would be nice if we could just
1728 reject them, but, unfortunately, some people need to use
1729 them. We scan through the section headers; if we find only
1730 one suitable symbol table, we clobber the sh_link to point
1731 to it. I hope this doesn't break anything. */
1732 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1733 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1735 unsigned int scan;
1736 int found;
1738 found = 0;
1739 for (scan = 1; scan < num_sec; scan++)
1741 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1742 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1744 if (found != 0)
1746 found = 0;
1747 break;
1749 found = scan;
1752 if (found != 0)
1753 hdr->sh_link = found;
1756 /* Get the symbol table. */
1757 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1758 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1759 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1760 return FALSE;
1762 /* If this reloc section does not use the main symbol table we
1763 don't treat it as a reloc section. BFD can't adequately
1764 represent such a section, so at least for now, we don't
1765 try. We just present it as a normal section. We also
1766 can't use it as a reloc section if it points to the null
1767 section, an invalid section, or another reloc section. */
1768 if (hdr->sh_link != elf_onesymtab (abfd)
1769 || hdr->sh_info == SHN_UNDEF
1770 || hdr->sh_info >= num_sec
1771 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1772 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1773 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1774 shindex);
1776 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1777 return FALSE;
1778 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1779 if (target_sect == NULL)
1780 return FALSE;
1782 if ((target_sect->flags & SEC_RELOC) == 0
1783 || target_sect->reloc_count == 0)
1784 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1785 else
1787 bfd_size_type amt;
1788 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1789 amt = sizeof (*hdr2);
1790 hdr2 = bfd_alloc (abfd, amt);
1791 if (hdr2 == NULL)
1792 return FALSE;
1793 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1795 *hdr2 = *hdr;
1796 elf_elfsections (abfd)[shindex] = hdr2;
1797 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1798 target_sect->flags |= SEC_RELOC;
1799 target_sect->relocation = NULL;
1800 target_sect->rel_filepos = hdr->sh_offset;
1801 /* In the section to which the relocations apply, mark whether
1802 its relocations are of the REL or RELA variety. */
1803 if (hdr->sh_size != 0)
1804 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1805 abfd->flags |= HAS_RELOC;
1806 return TRUE;
1809 case SHT_GNU_verdef:
1810 elf_dynverdef (abfd) = shindex;
1811 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1812 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1814 case SHT_GNU_versym:
1815 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1816 return FALSE;
1817 elf_dynversym (abfd) = shindex;
1818 elf_tdata (abfd)->dynversym_hdr = *hdr;
1819 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1821 case SHT_GNU_verneed:
1822 elf_dynverref (abfd) = shindex;
1823 elf_tdata (abfd)->dynverref_hdr = *hdr;
1824 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1826 case SHT_SHLIB:
1827 return TRUE;
1829 case SHT_GROUP:
1830 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1831 return FALSE;
1832 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1833 return FALSE;
1834 if (hdr->contents != NULL)
1836 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1837 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1838 asection *s;
1840 if (idx->flags & GRP_COMDAT)
1841 hdr->bfd_section->flags
1842 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1844 /* We try to keep the same section order as it comes in. */
1845 idx += n_elt;
1846 while (--n_elt != 0)
1848 --idx;
1850 if (idx->shdr != NULL
1851 && (s = idx->shdr->bfd_section) != NULL
1852 && elf_next_in_group (s) != NULL)
1854 elf_next_in_group (hdr->bfd_section) = s;
1855 break;
1859 break;
1861 default:
1862 /* Possibly an attributes section. */
1863 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1864 || hdr->sh_type == bed->obj_attrs_section_type)
1866 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1867 return FALSE;
1868 _bfd_elf_parse_attributes (abfd, hdr);
1869 return TRUE;
1872 /* Check for any processor-specific section types. */
1873 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1874 return TRUE;
1876 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1878 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1879 /* FIXME: How to properly handle allocated section reserved
1880 for applications? */
1881 (*_bfd_error_handler)
1882 (_("%B: don't know how to handle allocated, application "
1883 "specific section `%s' [0x%8x]"),
1884 abfd, name, hdr->sh_type);
1885 else
1886 /* Allow sections reserved for applications. */
1887 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1888 shindex);
1890 else if (hdr->sh_type >= SHT_LOPROC
1891 && hdr->sh_type <= SHT_HIPROC)
1892 /* FIXME: We should handle this section. */
1893 (*_bfd_error_handler)
1894 (_("%B: don't know how to handle processor specific section "
1895 "`%s' [0x%8x]"),
1896 abfd, name, hdr->sh_type);
1897 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1899 /* Unrecognised OS-specific sections. */
1900 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1901 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1902 required to correctly process the section and the file should
1903 be rejected with an error message. */
1904 (*_bfd_error_handler)
1905 (_("%B: don't know how to handle OS specific section "
1906 "`%s' [0x%8x]"),
1907 abfd, name, hdr->sh_type);
1908 else
1909 /* Otherwise it should be processed. */
1910 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1912 else
1913 /* FIXME: We should handle this section. */
1914 (*_bfd_error_handler)
1915 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1916 abfd, name, hdr->sh_type);
1918 return FALSE;
1921 return TRUE;
1924 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1925 Return SEC for sections that have no elf section, and NULL on error. */
1927 asection *
1928 bfd_section_from_r_symndx (bfd *abfd,
1929 struct sym_sec_cache *cache,
1930 asection *sec,
1931 unsigned long r_symndx)
1933 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1934 asection *s;
1936 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1938 Elf_Internal_Shdr *symtab_hdr;
1939 unsigned char esym[sizeof (Elf64_External_Sym)];
1940 Elf_External_Sym_Shndx eshndx;
1941 Elf_Internal_Sym isym;
1943 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1944 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1945 &isym, esym, &eshndx) == NULL)
1946 return NULL;
1948 if (cache->abfd != abfd)
1950 memset (cache->indx, -1, sizeof (cache->indx));
1951 cache->abfd = abfd;
1953 cache->indx[ent] = r_symndx;
1954 cache->shndx[ent] = isym.st_shndx;
1957 s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
1958 if (s != NULL)
1959 return s;
1961 return sec;
1964 /* Given an ELF section number, retrieve the corresponding BFD
1965 section. */
1967 asection *
1968 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
1970 if (index >= elf_numsections (abfd))
1971 return NULL;
1972 return elf_elfsections (abfd)[index]->bfd_section;
1975 static const struct bfd_elf_special_section special_sections_b[] =
1977 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1978 { NULL, 0, 0, 0, 0 }
1981 static const struct bfd_elf_special_section special_sections_c[] =
1983 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1984 { NULL, 0, 0, 0, 0 }
1987 static const struct bfd_elf_special_section special_sections_d[] =
1989 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1990 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1991 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
1992 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
1993 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
1994 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
1995 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
1996 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
1997 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
1998 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
1999 { NULL, 0, 0, 0, 0 }
2002 static const struct bfd_elf_special_section special_sections_f[] =
2004 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2005 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2006 { NULL, 0, 0, 0, 0 }
2009 static const struct bfd_elf_special_section special_sections_g[] =
2011 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2012 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2013 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2014 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2015 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2016 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2017 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2018 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2019 { NULL, 0, 0, 0, 0 }
2022 static const struct bfd_elf_special_section special_sections_h[] =
2024 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2025 { NULL, 0, 0, 0, 0 }
2028 static const struct bfd_elf_special_section special_sections_i[] =
2030 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2031 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2032 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2033 { NULL, 0, 0, 0, 0 }
2036 static const struct bfd_elf_special_section special_sections_l[] =
2038 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2039 { NULL, 0, 0, 0, 0 }
2042 static const struct bfd_elf_special_section special_sections_n[] =
2044 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2045 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2046 { NULL, 0, 0, 0, 0 }
2049 static const struct bfd_elf_special_section special_sections_p[] =
2051 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2052 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2053 { NULL, 0, 0, 0, 0 }
2056 static const struct bfd_elf_special_section special_sections_r[] =
2058 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2059 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2060 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2061 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2062 { NULL, 0, 0, 0, 0 }
2065 static const struct bfd_elf_special_section special_sections_s[] =
2067 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2068 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2069 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2070 /* See struct bfd_elf_special_section declaration for the semantics of
2071 this special case where .prefix_length != strlen (.prefix). */
2072 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2073 { NULL, 0, 0, 0, 0 }
2076 static const struct bfd_elf_special_section special_sections_t[] =
2078 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2079 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2080 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2081 { NULL, 0, 0, 0, 0 }
2084 static const struct bfd_elf_special_section special_sections_z[] =
2086 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2087 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2088 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2089 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2090 { NULL, 0, 0, 0, 0 }
2093 static const struct bfd_elf_special_section *special_sections[] =
2095 special_sections_b, /* 'b' */
2096 special_sections_c, /* 'c' */
2097 special_sections_d, /* 'd' */
2098 NULL, /* 'e' */
2099 special_sections_f, /* 'f' */
2100 special_sections_g, /* 'g' */
2101 special_sections_h, /* 'h' */
2102 special_sections_i, /* 'i' */
2103 NULL, /* 'j' */
2104 NULL, /* 'k' */
2105 special_sections_l, /* 'l' */
2106 NULL, /* 'm' */
2107 special_sections_n, /* 'n' */
2108 NULL, /* 'o' */
2109 special_sections_p, /* 'p' */
2110 NULL, /* 'q' */
2111 special_sections_r, /* 'r' */
2112 special_sections_s, /* 's' */
2113 special_sections_t, /* 't' */
2114 NULL, /* 'u' */
2115 NULL, /* 'v' */
2116 NULL, /* 'w' */
2117 NULL, /* 'x' */
2118 NULL, /* 'y' */
2119 special_sections_z /* 'z' */
2122 const struct bfd_elf_special_section *
2123 _bfd_elf_get_special_section (const char *name,
2124 const struct bfd_elf_special_section *spec,
2125 unsigned int rela)
2127 int i;
2128 int len;
2130 len = strlen (name);
2132 for (i = 0; spec[i].prefix != NULL; i++)
2134 int suffix_len;
2135 int prefix_len = spec[i].prefix_length;
2137 if (len < prefix_len)
2138 continue;
2139 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2140 continue;
2142 suffix_len = spec[i].suffix_length;
2143 if (suffix_len <= 0)
2145 if (name[prefix_len] != 0)
2147 if (suffix_len == 0)
2148 continue;
2149 if (name[prefix_len] != '.'
2150 && (suffix_len == -2
2151 || (rela && spec[i].type == SHT_REL)))
2152 continue;
2155 else
2157 if (len < prefix_len + suffix_len)
2158 continue;
2159 if (memcmp (name + len - suffix_len,
2160 spec[i].prefix + prefix_len,
2161 suffix_len) != 0)
2162 continue;
2164 return &spec[i];
2167 return NULL;
2170 const struct bfd_elf_special_section *
2171 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2173 int i;
2174 const struct bfd_elf_special_section *spec;
2175 const struct elf_backend_data *bed;
2177 /* See if this is one of the special sections. */
2178 if (sec->name == NULL)
2179 return NULL;
2181 bed = get_elf_backend_data (abfd);
2182 spec = bed->special_sections;
2183 if (spec)
2185 spec = _bfd_elf_get_special_section (sec->name,
2186 bed->special_sections,
2187 sec->use_rela_p);
2188 if (spec != NULL)
2189 return spec;
2192 if (sec->name[0] != '.')
2193 return NULL;
2195 i = sec->name[1] - 'b';
2196 if (i < 0 || i > 'z' - 'b')
2197 return NULL;
2199 spec = special_sections[i];
2201 if (spec == NULL)
2202 return NULL;
2204 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2207 bfd_boolean
2208 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2210 struct bfd_elf_section_data *sdata;
2211 const struct elf_backend_data *bed;
2212 const struct bfd_elf_special_section *ssect;
2214 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2215 if (sdata == NULL)
2217 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2218 if (sdata == NULL)
2219 return FALSE;
2220 sec->used_by_bfd = sdata;
2223 /* Indicate whether or not this section should use RELA relocations. */
2224 bed = get_elf_backend_data (abfd);
2225 sec->use_rela_p = bed->default_use_rela_p;
2227 /* When we read a file, we don't need to set ELF section type and
2228 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2229 anyway. We will set ELF section type and flags for all linker
2230 created sections. If user specifies BFD section flags, we will
2231 set ELF section type and flags based on BFD section flags in
2232 elf_fake_sections. */
2233 if ((!sec->flags && abfd->direction != read_direction)
2234 || (sec->flags & SEC_LINKER_CREATED) != 0)
2236 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2237 if (ssect != NULL)
2239 elf_section_type (sec) = ssect->type;
2240 elf_section_flags (sec) = ssect->attr;
2244 return _bfd_generic_new_section_hook (abfd, sec);
2247 /* Create a new bfd section from an ELF program header.
2249 Since program segments have no names, we generate a synthetic name
2250 of the form segment<NUM>, where NUM is generally the index in the
2251 program header table. For segments that are split (see below) we
2252 generate the names segment<NUM>a and segment<NUM>b.
2254 Note that some program segments may have a file size that is different than
2255 (less than) the memory size. All this means is that at execution the
2256 system must allocate the amount of memory specified by the memory size,
2257 but only initialize it with the first "file size" bytes read from the
2258 file. This would occur for example, with program segments consisting
2259 of combined data+bss.
2261 To handle the above situation, this routine generates TWO bfd sections
2262 for the single program segment. The first has the length specified by
2263 the file size of the segment, and the second has the length specified
2264 by the difference between the two sizes. In effect, the segment is split
2265 into its initialized and uninitialized parts.
2269 bfd_boolean
2270 _bfd_elf_make_section_from_phdr (bfd *abfd,
2271 Elf_Internal_Phdr *hdr,
2272 int index,
2273 const char *typename)
2275 asection *newsect;
2276 char *name;
2277 char namebuf[64];
2278 size_t len;
2279 int split;
2281 split = ((hdr->p_memsz > 0)
2282 && (hdr->p_filesz > 0)
2283 && (hdr->p_memsz > hdr->p_filesz));
2285 if (hdr->p_filesz > 0)
2287 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2288 len = strlen (namebuf) + 1;
2289 name = bfd_alloc (abfd, len);
2290 if (!name)
2291 return FALSE;
2292 memcpy (name, namebuf, len);
2293 newsect = bfd_make_section (abfd, name);
2294 if (newsect == NULL)
2295 return FALSE;
2296 newsect->vma = hdr->p_vaddr;
2297 newsect->lma = hdr->p_paddr;
2298 newsect->size = hdr->p_filesz;
2299 newsect->filepos = hdr->p_offset;
2300 newsect->flags |= SEC_HAS_CONTENTS;
2301 newsect->alignment_power = bfd_log2 (hdr->p_align);
2302 if (hdr->p_type == PT_LOAD)
2304 newsect->flags |= SEC_ALLOC;
2305 newsect->flags |= SEC_LOAD;
2306 if (hdr->p_flags & PF_X)
2308 /* FIXME: all we known is that it has execute PERMISSION,
2309 may be data. */
2310 newsect->flags |= SEC_CODE;
2313 if (!(hdr->p_flags & PF_W))
2315 newsect->flags |= SEC_READONLY;
2319 if (hdr->p_memsz > hdr->p_filesz)
2321 bfd_vma align;
2323 sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : "");
2324 len = strlen (namebuf) + 1;
2325 name = bfd_alloc (abfd, len);
2326 if (!name)
2327 return FALSE;
2328 memcpy (name, namebuf, len);
2329 newsect = bfd_make_section (abfd, name);
2330 if (newsect == NULL)
2331 return FALSE;
2332 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2333 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2334 newsect->size = hdr->p_memsz - hdr->p_filesz;
2335 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2336 align = newsect->vma & -newsect->vma;
2337 if (align == 0 || align > hdr->p_align)
2338 align = hdr->p_align;
2339 newsect->alignment_power = bfd_log2 (align);
2340 if (hdr->p_type == PT_LOAD)
2342 /* Hack for gdb. Segments that have not been modified do
2343 not have their contents written to a core file, on the
2344 assumption that a debugger can find the contents in the
2345 executable. We flag this case by setting the fake
2346 section size to zero. Note that "real" bss sections will
2347 always have their contents dumped to the core file. */
2348 if (bfd_get_format (abfd) == bfd_core)
2349 newsect->size = 0;
2350 newsect->flags |= SEC_ALLOC;
2351 if (hdr->p_flags & PF_X)
2352 newsect->flags |= SEC_CODE;
2354 if (!(hdr->p_flags & PF_W))
2355 newsect->flags |= SEC_READONLY;
2358 return TRUE;
2361 bfd_boolean
2362 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2364 const struct elf_backend_data *bed;
2366 switch (hdr->p_type)
2368 case PT_NULL:
2369 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2371 case PT_LOAD:
2372 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2374 case PT_DYNAMIC:
2375 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2377 case PT_INTERP:
2378 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2380 case PT_NOTE:
2381 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2382 return FALSE;
2383 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2384 return FALSE;
2385 return TRUE;
2387 case PT_SHLIB:
2388 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2390 case PT_PHDR:
2391 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2393 case PT_GNU_EH_FRAME:
2394 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2395 "eh_frame_hdr");
2397 case PT_GNU_STACK:
2398 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2400 case PT_GNU_RELRO:
2401 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2403 default:
2404 /* Check for any processor-specific program segment types. */
2405 bed = get_elf_backend_data (abfd);
2406 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2410 /* Initialize REL_HDR, the section-header for new section, containing
2411 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2412 relocations; otherwise, we use REL relocations. */
2414 bfd_boolean
2415 _bfd_elf_init_reloc_shdr (bfd *abfd,
2416 Elf_Internal_Shdr *rel_hdr,
2417 asection *asect,
2418 bfd_boolean use_rela_p)
2420 char *name;
2421 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2422 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2424 name = bfd_alloc (abfd, amt);
2425 if (name == NULL)
2426 return FALSE;
2427 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2428 rel_hdr->sh_name =
2429 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2430 FALSE);
2431 if (rel_hdr->sh_name == (unsigned int) -1)
2432 return FALSE;
2433 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2434 rel_hdr->sh_entsize = (use_rela_p
2435 ? bed->s->sizeof_rela
2436 : bed->s->sizeof_rel);
2437 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2438 rel_hdr->sh_flags = 0;
2439 rel_hdr->sh_addr = 0;
2440 rel_hdr->sh_size = 0;
2441 rel_hdr->sh_offset = 0;
2443 return TRUE;
2446 /* Set up an ELF internal section header for a section. */
2448 static void
2449 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2451 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2452 bfd_boolean *failedptr = failedptrarg;
2453 Elf_Internal_Shdr *this_hdr;
2454 unsigned int sh_type;
2456 if (*failedptr)
2458 /* We already failed; just get out of the bfd_map_over_sections
2459 loop. */
2460 return;
2463 this_hdr = &elf_section_data (asect)->this_hdr;
2465 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2466 asect->name, FALSE);
2467 if (this_hdr->sh_name == (unsigned int) -1)
2469 *failedptr = TRUE;
2470 return;
2473 /* Don't clear sh_flags. Assembler may set additional bits. */
2475 if ((asect->flags & SEC_ALLOC) != 0
2476 || asect->user_set_vma)
2477 this_hdr->sh_addr = asect->vma;
2478 else
2479 this_hdr->sh_addr = 0;
2481 this_hdr->sh_offset = 0;
2482 this_hdr->sh_size = asect->size;
2483 this_hdr->sh_link = 0;
2484 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2485 /* The sh_entsize and sh_info fields may have been set already by
2486 copy_private_section_data. */
2488 this_hdr->bfd_section = asect;
2489 this_hdr->contents = NULL;
2491 /* If the section type is unspecified, we set it based on
2492 asect->flags. */
2493 if ((asect->flags & SEC_GROUP) != 0)
2494 sh_type = SHT_GROUP;
2495 else if ((asect->flags & SEC_ALLOC) != 0
2496 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2497 || (asect->flags & SEC_NEVER_LOAD) != 0))
2498 sh_type = SHT_NOBITS;
2499 else
2500 sh_type = SHT_PROGBITS;
2502 if (this_hdr->sh_type == SHT_NULL)
2503 this_hdr->sh_type = sh_type;
2504 else if (this_hdr->sh_type == SHT_NOBITS
2505 && sh_type == SHT_PROGBITS
2506 && (asect->flags & SEC_ALLOC) != 0)
2508 /* Warn if we are changing a NOBITS section to PROGBITS, but
2509 allow the link to proceed. This can happen when users link
2510 non-bss input sections to bss output sections, or emit data
2511 to a bss output section via a linker script. */
2512 (*_bfd_error_handler)
2513 (_("warning: section `%A' type changed to PROGBITS"), asect);
2514 this_hdr->sh_type = sh_type;
2517 switch (this_hdr->sh_type)
2519 default:
2520 break;
2522 case SHT_STRTAB:
2523 case SHT_INIT_ARRAY:
2524 case SHT_FINI_ARRAY:
2525 case SHT_PREINIT_ARRAY:
2526 case SHT_NOTE:
2527 case SHT_NOBITS:
2528 case SHT_PROGBITS:
2529 break;
2531 case SHT_HASH:
2532 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2533 break;
2535 case SHT_DYNSYM:
2536 this_hdr->sh_entsize = bed->s->sizeof_sym;
2537 break;
2539 case SHT_DYNAMIC:
2540 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2541 break;
2543 case SHT_RELA:
2544 if (get_elf_backend_data (abfd)->may_use_rela_p)
2545 this_hdr->sh_entsize = bed->s->sizeof_rela;
2546 break;
2548 case SHT_REL:
2549 if (get_elf_backend_data (abfd)->may_use_rel_p)
2550 this_hdr->sh_entsize = bed->s->sizeof_rel;
2551 break;
2553 case SHT_GNU_versym:
2554 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2555 break;
2557 case SHT_GNU_verdef:
2558 this_hdr->sh_entsize = 0;
2559 /* objcopy or strip will copy over sh_info, but may not set
2560 cverdefs. The linker will set cverdefs, but sh_info will be
2561 zero. */
2562 if (this_hdr->sh_info == 0)
2563 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2564 else
2565 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2566 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2567 break;
2569 case SHT_GNU_verneed:
2570 this_hdr->sh_entsize = 0;
2571 /* objcopy or strip will copy over sh_info, but may not set
2572 cverrefs. The linker will set cverrefs, but sh_info will be
2573 zero. */
2574 if (this_hdr->sh_info == 0)
2575 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2576 else
2577 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2578 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2579 break;
2581 case SHT_GROUP:
2582 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2583 break;
2585 case SHT_GNU_HASH:
2586 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2587 break;
2590 if ((asect->flags & SEC_ALLOC) != 0)
2591 this_hdr->sh_flags |= SHF_ALLOC;
2592 if ((asect->flags & SEC_READONLY) == 0)
2593 this_hdr->sh_flags |= SHF_WRITE;
2594 if ((asect->flags & SEC_CODE) != 0)
2595 this_hdr->sh_flags |= SHF_EXECINSTR;
2596 if ((asect->flags & SEC_MERGE) != 0)
2598 this_hdr->sh_flags |= SHF_MERGE;
2599 this_hdr->sh_entsize = asect->entsize;
2600 if ((asect->flags & SEC_STRINGS) != 0)
2601 this_hdr->sh_flags |= SHF_STRINGS;
2603 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2604 this_hdr->sh_flags |= SHF_GROUP;
2605 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2607 this_hdr->sh_flags |= SHF_TLS;
2608 if (asect->size == 0
2609 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2611 struct bfd_link_order *o = asect->map_tail.link_order;
2613 this_hdr->sh_size = 0;
2614 if (o != NULL)
2616 this_hdr->sh_size = o->offset + o->size;
2617 if (this_hdr->sh_size != 0)
2618 this_hdr->sh_type = SHT_NOBITS;
2623 /* Check for processor-specific section types. */
2624 sh_type = this_hdr->sh_type;
2625 if (bed->elf_backend_fake_sections
2626 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2627 *failedptr = TRUE;
2629 if (sh_type == SHT_NOBITS && asect->size != 0)
2631 /* Don't change the header type from NOBITS if we are being
2632 called for objcopy --only-keep-debug. */
2633 this_hdr->sh_type = sh_type;
2636 /* If the section has relocs, set up a section header for the
2637 SHT_REL[A] section. If two relocation sections are required for
2638 this section, it is up to the processor-specific back-end to
2639 create the other. */
2640 if ((asect->flags & SEC_RELOC) != 0
2641 && !_bfd_elf_init_reloc_shdr (abfd,
2642 &elf_section_data (asect)->rel_hdr,
2643 asect,
2644 asect->use_rela_p))
2645 *failedptr = TRUE;
2648 /* Fill in the contents of a SHT_GROUP section. Called from
2649 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2650 when ELF targets use the generic linker, ld. Called for ld -r
2651 from bfd_elf_final_link. */
2653 void
2654 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2656 bfd_boolean *failedptr = failedptrarg;
2657 asection *elt, *first;
2658 unsigned char *loc;
2659 bfd_boolean gas;
2661 /* Ignore linker created group section. See elfNN_ia64_object_p in
2662 elfxx-ia64.c. */
2663 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2664 || *failedptr)
2665 return;
2667 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2669 unsigned long symindx = 0;
2671 /* elf_group_id will have been set up by objcopy and the
2672 generic linker. */
2673 if (elf_group_id (sec) != NULL)
2674 symindx = elf_group_id (sec)->udata.i;
2676 if (symindx == 0)
2678 /* If called from the assembler, swap_out_syms will have set up
2679 elf_section_syms. */
2680 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2681 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2683 elf_section_data (sec)->this_hdr.sh_info = symindx;
2685 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2687 /* The ELF backend linker sets sh_info to -2 when the group
2688 signature symbol is global, and thus the index can't be
2689 set until all local symbols are output. */
2690 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2691 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2692 unsigned long symndx = sec_data->this_hdr.sh_info;
2693 unsigned long extsymoff = 0;
2694 struct elf_link_hash_entry *h;
2696 if (!elf_bad_symtab (igroup->owner))
2698 Elf_Internal_Shdr *symtab_hdr;
2700 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2701 extsymoff = symtab_hdr->sh_info;
2703 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2704 while (h->root.type == bfd_link_hash_indirect
2705 || h->root.type == bfd_link_hash_warning)
2706 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2708 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2711 /* The contents won't be allocated for "ld -r" or objcopy. */
2712 gas = TRUE;
2713 if (sec->contents == NULL)
2715 gas = FALSE;
2716 sec->contents = bfd_alloc (abfd, sec->size);
2718 /* Arrange for the section to be written out. */
2719 elf_section_data (sec)->this_hdr.contents = sec->contents;
2720 if (sec->contents == NULL)
2722 *failedptr = TRUE;
2723 return;
2727 loc = sec->contents + sec->size;
2729 /* Get the pointer to the first section in the group that gas
2730 squirreled away here. objcopy arranges for this to be set to the
2731 start of the input section group. */
2732 first = elt = elf_next_in_group (sec);
2734 /* First element is a flag word. Rest of section is elf section
2735 indices for all the sections of the group. Write them backwards
2736 just to keep the group in the same order as given in .section
2737 directives, not that it matters. */
2738 while (elt != NULL)
2740 asection *s;
2741 unsigned int idx;
2743 s = elt;
2744 if (! elf_discarded_section (s))
2746 loc -= 4;
2747 if (!gas)
2748 s = s->output_section;
2749 idx = 0;
2750 if (s != NULL)
2751 idx = elf_section_data (s)->this_idx;
2752 H_PUT_32 (abfd, idx, loc);
2754 elt = elf_next_in_group (elt);
2755 if (elt == first)
2756 break;
2759 if ((loc -= 4) != sec->contents)
2760 abort ();
2762 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2765 /* Assign all ELF section numbers. The dummy first section is handled here
2766 too. The link/info pointers for the standard section types are filled
2767 in here too, while we're at it. */
2769 static bfd_boolean
2770 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2772 struct elf_obj_tdata *t = elf_tdata (abfd);
2773 asection *sec;
2774 unsigned int section_number, secn;
2775 Elf_Internal_Shdr **i_shdrp;
2776 struct bfd_elf_section_data *d;
2777 bfd_boolean need_symtab;
2779 section_number = 1;
2781 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2783 /* SHT_GROUP sections are in relocatable files only. */
2784 if (link_info == NULL || link_info->relocatable)
2786 /* Put SHT_GROUP sections first. */
2787 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2789 d = elf_section_data (sec);
2791 if (d->this_hdr.sh_type == SHT_GROUP)
2793 if (sec->flags & SEC_LINKER_CREATED)
2795 /* Remove the linker created SHT_GROUP sections. */
2796 bfd_section_list_remove (abfd, sec);
2797 abfd->section_count--;
2799 else
2800 d->this_idx = section_number++;
2805 for (sec = abfd->sections; sec; sec = sec->next)
2807 d = elf_section_data (sec);
2809 if (d->this_hdr.sh_type != SHT_GROUP)
2810 d->this_idx = section_number++;
2811 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2812 if ((sec->flags & SEC_RELOC) == 0)
2813 d->rel_idx = 0;
2814 else
2816 d->rel_idx = section_number++;
2817 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2820 if (d->rel_hdr2)
2822 d->rel_idx2 = section_number++;
2823 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2825 else
2826 d->rel_idx2 = 0;
2829 t->shstrtab_section = section_number++;
2830 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2831 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2833 need_symtab = (bfd_get_symcount (abfd) > 0
2834 || (link_info == NULL
2835 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
2836 == HAS_RELOC)));
2837 if (need_symtab)
2839 t->symtab_section = section_number++;
2840 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2841 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2843 t->symtab_shndx_section = section_number++;
2844 t->symtab_shndx_hdr.sh_name
2845 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2846 ".symtab_shndx", FALSE);
2847 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2848 return FALSE;
2850 t->strtab_section = section_number++;
2851 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2854 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2855 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2857 elf_numsections (abfd) = section_number;
2858 elf_elfheader (abfd)->e_shnum = section_number;
2860 /* Set up the list of section header pointers, in agreement with the
2861 indices. */
2862 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
2863 if (i_shdrp == NULL)
2864 return FALSE;
2866 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2867 if (i_shdrp[0] == NULL)
2869 bfd_release (abfd, i_shdrp);
2870 return FALSE;
2873 elf_elfsections (abfd) = i_shdrp;
2875 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2876 if (need_symtab)
2878 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2879 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2881 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2882 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2884 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2885 t->symtab_hdr.sh_link = t->strtab_section;
2888 for (sec = abfd->sections; sec; sec = sec->next)
2890 struct bfd_elf_section_data *d = elf_section_data (sec);
2891 asection *s;
2892 const char *name;
2894 i_shdrp[d->this_idx] = &d->this_hdr;
2895 if (d->rel_idx != 0)
2896 i_shdrp[d->rel_idx] = &d->rel_hdr;
2897 if (d->rel_idx2 != 0)
2898 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2900 /* Fill in the sh_link and sh_info fields while we're at it. */
2902 /* sh_link of a reloc section is the section index of the symbol
2903 table. sh_info is the section index of the section to which
2904 the relocation entries apply. */
2905 if (d->rel_idx != 0)
2907 d->rel_hdr.sh_link = t->symtab_section;
2908 d->rel_hdr.sh_info = d->this_idx;
2910 if (d->rel_idx2 != 0)
2912 d->rel_hdr2->sh_link = t->symtab_section;
2913 d->rel_hdr2->sh_info = d->this_idx;
2916 /* We need to set up sh_link for SHF_LINK_ORDER. */
2917 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2919 s = elf_linked_to_section (sec);
2920 if (s)
2922 /* elf_linked_to_section points to the input section. */
2923 if (link_info != NULL)
2925 /* Check discarded linkonce section. */
2926 if (elf_discarded_section (s))
2928 asection *kept;
2929 (*_bfd_error_handler)
2930 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2931 abfd, d->this_hdr.bfd_section,
2932 s, s->owner);
2933 /* Point to the kept section if it has the same
2934 size as the discarded one. */
2935 kept = _bfd_elf_check_kept_section (s, link_info);
2936 if (kept == NULL)
2938 bfd_set_error (bfd_error_bad_value);
2939 return FALSE;
2941 s = kept;
2944 s = s->output_section;
2945 BFD_ASSERT (s != NULL);
2947 else
2949 /* Handle objcopy. */
2950 if (s->output_section == NULL)
2952 (*_bfd_error_handler)
2953 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2954 abfd, d->this_hdr.bfd_section, s, s->owner);
2955 bfd_set_error (bfd_error_bad_value);
2956 return FALSE;
2958 s = s->output_section;
2960 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2962 else
2964 /* PR 290:
2965 The Intel C compiler generates SHT_IA_64_UNWIND with
2966 SHF_LINK_ORDER. But it doesn't set the sh_link or
2967 sh_info fields. Hence we could get the situation
2968 where s is NULL. */
2969 const struct elf_backend_data *bed
2970 = get_elf_backend_data (abfd);
2971 if (bed->link_order_error_handler)
2972 bed->link_order_error_handler
2973 (_("%B: warning: sh_link not set for section `%A'"),
2974 abfd, sec);
2978 switch (d->this_hdr.sh_type)
2980 case SHT_REL:
2981 case SHT_RELA:
2982 /* A reloc section which we are treating as a normal BFD
2983 section. sh_link is the section index of the symbol
2984 table. sh_info is the section index of the section to
2985 which the relocation entries apply. We assume that an
2986 allocated reloc section uses the dynamic symbol table.
2987 FIXME: How can we be sure? */
2988 s = bfd_get_section_by_name (abfd, ".dynsym");
2989 if (s != NULL)
2990 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2992 /* We look up the section the relocs apply to by name. */
2993 name = sec->name;
2994 if (d->this_hdr.sh_type == SHT_REL)
2995 name += 4;
2996 else
2997 name += 5;
2998 s = bfd_get_section_by_name (abfd, name);
2999 if (s != NULL)
3000 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3001 break;
3003 case SHT_STRTAB:
3004 /* We assume that a section named .stab*str is a stabs
3005 string section. We look for a section with the same name
3006 but without the trailing ``str'', and set its sh_link
3007 field to point to this section. */
3008 if (CONST_STRNEQ (sec->name, ".stab")
3009 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3011 size_t len;
3012 char *alc;
3014 len = strlen (sec->name);
3015 alc = bfd_malloc (len - 2);
3016 if (alc == NULL)
3017 return FALSE;
3018 memcpy (alc, sec->name, len - 3);
3019 alc[len - 3] = '\0';
3020 s = bfd_get_section_by_name (abfd, alc);
3021 free (alc);
3022 if (s != NULL)
3024 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3026 /* This is a .stab section. */
3027 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3028 elf_section_data (s)->this_hdr.sh_entsize
3029 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3032 break;
3034 case SHT_DYNAMIC:
3035 case SHT_DYNSYM:
3036 case SHT_GNU_verneed:
3037 case SHT_GNU_verdef:
3038 /* sh_link is the section header index of the string table
3039 used for the dynamic entries, or the symbol table, or the
3040 version strings. */
3041 s = bfd_get_section_by_name (abfd, ".dynstr");
3042 if (s != NULL)
3043 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3044 break;
3046 case SHT_GNU_LIBLIST:
3047 /* sh_link is the section header index of the prelink library
3048 list used for the dynamic entries, or the symbol table, or
3049 the version strings. */
3050 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3051 ? ".dynstr" : ".gnu.libstr");
3052 if (s != NULL)
3053 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3054 break;
3056 case SHT_HASH:
3057 case SHT_GNU_HASH:
3058 case SHT_GNU_versym:
3059 /* sh_link is the section header index of the symbol table
3060 this hash table or version table is for. */
3061 s = bfd_get_section_by_name (abfd, ".dynsym");
3062 if (s != NULL)
3063 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3064 break;
3066 case SHT_GROUP:
3067 d->this_hdr.sh_link = t->symtab_section;
3071 for (secn = 1; secn < section_number; ++secn)
3072 if (i_shdrp[secn] == NULL)
3073 i_shdrp[secn] = i_shdrp[0];
3074 else
3075 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3076 i_shdrp[secn]->sh_name);
3077 return TRUE;
3080 /* Map symbol from it's internal number to the external number, moving
3081 all local symbols to be at the head of the list. */
3083 static bfd_boolean
3084 sym_is_global (bfd *abfd, asymbol *sym)
3086 /* If the backend has a special mapping, use it. */
3087 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3088 if (bed->elf_backend_sym_is_global)
3089 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3091 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3092 || bfd_is_und_section (bfd_get_section (sym))
3093 || bfd_is_com_section (bfd_get_section (sym)));
3096 /* Don't output section symbols for sections that are not going to be
3097 output. */
3099 static bfd_boolean
3100 ignore_section_sym (bfd *abfd, asymbol *sym)
3102 return ((sym->flags & BSF_SECTION_SYM) != 0
3103 && !(sym->section->owner == abfd
3104 || (sym->section->output_section->owner == abfd
3105 && sym->section->output_offset == 0)));
3108 static bfd_boolean
3109 elf_map_symbols (bfd *abfd)
3111 unsigned int symcount = bfd_get_symcount (abfd);
3112 asymbol **syms = bfd_get_outsymbols (abfd);
3113 asymbol **sect_syms;
3114 unsigned int num_locals = 0;
3115 unsigned int num_globals = 0;
3116 unsigned int num_locals2 = 0;
3117 unsigned int num_globals2 = 0;
3118 int max_index = 0;
3119 unsigned int idx;
3120 asection *asect;
3121 asymbol **new_syms;
3123 #ifdef DEBUG
3124 fprintf (stderr, "elf_map_symbols\n");
3125 fflush (stderr);
3126 #endif
3128 for (asect = abfd->sections; asect; asect = asect->next)
3130 if (max_index < asect->index)
3131 max_index = asect->index;
3134 max_index++;
3135 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3136 if (sect_syms == NULL)
3137 return FALSE;
3138 elf_section_syms (abfd) = sect_syms;
3139 elf_num_section_syms (abfd) = max_index;
3141 /* Init sect_syms entries for any section symbols we have already
3142 decided to output. */
3143 for (idx = 0; idx < symcount; idx++)
3145 asymbol *sym = syms[idx];
3147 if ((sym->flags & BSF_SECTION_SYM) != 0
3148 && sym->value == 0
3149 && !ignore_section_sym (abfd, sym))
3151 asection *sec = sym->section;
3153 if (sec->owner != abfd)
3154 sec = sec->output_section;
3156 sect_syms[sec->index] = syms[idx];
3160 /* Classify all of the symbols. */
3161 for (idx = 0; idx < symcount; idx++)
3163 if (ignore_section_sym (abfd, syms[idx]))
3164 continue;
3165 if (!sym_is_global (abfd, syms[idx]))
3166 num_locals++;
3167 else
3168 num_globals++;
3171 /* We will be adding a section symbol for each normal BFD section. Most
3172 sections will already have a section symbol in outsymbols, but
3173 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3174 at least in that case. */
3175 for (asect = abfd->sections; asect; asect = asect->next)
3177 if (sect_syms[asect->index] == NULL)
3179 if (!sym_is_global (abfd, asect->symbol))
3180 num_locals++;
3181 else
3182 num_globals++;
3186 /* Now sort the symbols so the local symbols are first. */
3187 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3189 if (new_syms == NULL)
3190 return FALSE;
3192 for (idx = 0; idx < symcount; idx++)
3194 asymbol *sym = syms[idx];
3195 unsigned int i;
3197 if (ignore_section_sym (abfd, sym))
3198 continue;
3199 if (!sym_is_global (abfd, sym))
3200 i = num_locals2++;
3201 else
3202 i = num_locals + num_globals2++;
3203 new_syms[i] = sym;
3204 sym->udata.i = i + 1;
3206 for (asect = abfd->sections; asect; asect = asect->next)
3208 if (sect_syms[asect->index] == NULL)
3210 asymbol *sym = asect->symbol;
3211 unsigned int i;
3213 sect_syms[asect->index] = sym;
3214 if (!sym_is_global (abfd, sym))
3215 i = num_locals2++;
3216 else
3217 i = num_locals + num_globals2++;
3218 new_syms[i] = sym;
3219 sym->udata.i = i + 1;
3223 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3225 elf_num_locals (abfd) = num_locals;
3226 elf_num_globals (abfd) = num_globals;
3227 return TRUE;
3230 /* Align to the maximum file alignment that could be required for any
3231 ELF data structure. */
3233 static inline file_ptr
3234 align_file_position (file_ptr off, int align)
3236 return (off + align - 1) & ~(align - 1);
3239 /* Assign a file position to a section, optionally aligning to the
3240 required section alignment. */
3242 file_ptr
3243 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3244 file_ptr offset,
3245 bfd_boolean align)
3247 if (align && i_shdrp->sh_addralign > 1)
3248 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3249 i_shdrp->sh_offset = offset;
3250 if (i_shdrp->bfd_section != NULL)
3251 i_shdrp->bfd_section->filepos = offset;
3252 if (i_shdrp->sh_type != SHT_NOBITS)
3253 offset += i_shdrp->sh_size;
3254 return offset;
3257 /* Compute the file positions we are going to put the sections at, and
3258 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3259 is not NULL, this is being called by the ELF backend linker. */
3261 bfd_boolean
3262 _bfd_elf_compute_section_file_positions (bfd *abfd,
3263 struct bfd_link_info *link_info)
3265 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3266 bfd_boolean failed;
3267 struct bfd_strtab_hash *strtab = NULL;
3268 Elf_Internal_Shdr *shstrtab_hdr;
3269 bfd_boolean need_symtab;
3271 if (abfd->output_has_begun)
3272 return TRUE;
3274 /* Do any elf backend specific processing first. */
3275 if (bed->elf_backend_begin_write_processing)
3276 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3278 if (! prep_headers (abfd))
3279 return FALSE;
3281 /* Post process the headers if necessary. */
3282 if (bed->elf_backend_post_process_headers)
3283 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3285 failed = FALSE;
3286 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3287 if (failed)
3288 return FALSE;
3290 if (!assign_section_numbers (abfd, link_info))
3291 return FALSE;
3293 /* The backend linker builds symbol table information itself. */
3294 need_symtab = (link_info == NULL
3295 && (bfd_get_symcount (abfd) > 0
3296 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3297 == HAS_RELOC)));
3298 if (need_symtab)
3300 /* Non-zero if doing a relocatable link. */
3301 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3303 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3304 return FALSE;
3307 if (link_info == NULL)
3309 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3310 if (failed)
3311 return FALSE;
3314 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3315 /* sh_name was set in prep_headers. */
3316 shstrtab_hdr->sh_type = SHT_STRTAB;
3317 shstrtab_hdr->sh_flags = 0;
3318 shstrtab_hdr->sh_addr = 0;
3319 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3320 shstrtab_hdr->sh_entsize = 0;
3321 shstrtab_hdr->sh_link = 0;
3322 shstrtab_hdr->sh_info = 0;
3323 /* sh_offset is set in assign_file_positions_except_relocs. */
3324 shstrtab_hdr->sh_addralign = 1;
3326 if (!assign_file_positions_except_relocs (abfd, link_info))
3327 return FALSE;
3329 if (need_symtab)
3331 file_ptr off;
3332 Elf_Internal_Shdr *hdr;
3334 off = elf_tdata (abfd)->next_file_pos;
3336 hdr = &elf_tdata (abfd)->symtab_hdr;
3337 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3339 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3340 if (hdr->sh_size != 0)
3341 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3343 hdr = &elf_tdata (abfd)->strtab_hdr;
3344 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3346 elf_tdata (abfd)->next_file_pos = off;
3348 /* Now that we know where the .strtab section goes, write it
3349 out. */
3350 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3351 || ! _bfd_stringtab_emit (abfd, strtab))
3352 return FALSE;
3353 _bfd_stringtab_free (strtab);
3356 abfd->output_has_begun = TRUE;
3358 return TRUE;
3361 /* Make an initial estimate of the size of the program header. If we
3362 get the number wrong here, we'll redo section placement. */
3364 static bfd_size_type
3365 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3367 size_t segs;
3368 asection *s;
3369 const struct elf_backend_data *bed;
3371 /* Assume we will need exactly two PT_LOAD segments: one for text
3372 and one for data. */
3373 segs = 2;
3375 s = bfd_get_section_by_name (abfd, ".interp");
3376 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3378 /* If we have a loadable interpreter section, we need a
3379 PT_INTERP segment. In this case, assume we also need a
3380 PT_PHDR segment, although that may not be true for all
3381 targets. */
3382 segs += 2;
3385 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3387 /* We need a PT_DYNAMIC segment. */
3388 ++segs;
3391 if (info != NULL && info->relro)
3393 /* We need a PT_GNU_RELRO segment. */
3394 ++segs;
3397 if (elf_tdata (abfd)->eh_frame_hdr)
3399 /* We need a PT_GNU_EH_FRAME segment. */
3400 ++segs;
3403 if (elf_tdata (abfd)->stack_flags)
3405 /* We need a PT_GNU_STACK segment. */
3406 ++segs;
3409 for (s = abfd->sections; s != NULL; s = s->next)
3411 if ((s->flags & SEC_LOAD) != 0
3412 && CONST_STRNEQ (s->name, ".note"))
3414 /* We need a PT_NOTE segment. */
3415 ++segs;
3416 /* Try to create just one PT_NOTE segment
3417 for all adjacent loadable .note* sections.
3418 gABI requires that within a PT_NOTE segment
3419 (and also inside of each SHT_NOTE section)
3420 each note is padded to a multiple of 4 size,
3421 so we check whether the sections are correctly
3422 aligned. */
3423 if (s->alignment_power == 2)
3424 while (s->next != NULL
3425 && s->next->alignment_power == 2
3426 && (s->next->flags & SEC_LOAD) != 0
3427 && CONST_STRNEQ (s->next->name, ".note"))
3428 s = s->next;
3432 for (s = abfd->sections; s != NULL; s = s->next)
3434 if (s->flags & SEC_THREAD_LOCAL)
3436 /* We need a PT_TLS segment. */
3437 ++segs;
3438 break;
3442 /* Let the backend count up any program headers it might need. */
3443 bed = get_elf_backend_data (abfd);
3444 if (bed->elf_backend_additional_program_headers)
3446 int a;
3448 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3449 if (a == -1)
3450 abort ();
3451 segs += a;
3454 return segs * bed->s->sizeof_phdr;
3457 /* Find the segment that contains the output_section of section. */
3459 Elf_Internal_Phdr *
3460 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3462 struct elf_segment_map *m;
3463 Elf_Internal_Phdr *p;
3465 for (m = elf_tdata (abfd)->segment_map,
3466 p = elf_tdata (abfd)->phdr;
3467 m != NULL;
3468 m = m->next, p++)
3470 int i;
3472 for (i = m->count - 1; i >= 0; i--)
3473 if (m->sections[i] == section)
3474 return p;
3477 return NULL;
3480 /* Create a mapping from a set of sections to a program segment. */
3482 static struct elf_segment_map *
3483 make_mapping (bfd *abfd,
3484 asection **sections,
3485 unsigned int from,
3486 unsigned int to,
3487 bfd_boolean phdr)
3489 struct elf_segment_map *m;
3490 unsigned int i;
3491 asection **hdrpp;
3492 bfd_size_type amt;
3494 amt = sizeof (struct elf_segment_map);
3495 amt += (to - from - 1) * sizeof (asection *);
3496 m = bfd_zalloc (abfd, amt);
3497 if (m == NULL)
3498 return NULL;
3499 m->next = NULL;
3500 m->p_type = PT_LOAD;
3501 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3502 m->sections[i - from] = *hdrpp;
3503 m->count = to - from;
3505 if (from == 0 && phdr)
3507 /* Include the headers in the first PT_LOAD segment. */
3508 m->includes_filehdr = 1;
3509 m->includes_phdrs = 1;
3512 return m;
3515 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3516 on failure. */
3518 struct elf_segment_map *
3519 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3521 struct elf_segment_map *m;
3523 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3524 if (m == NULL)
3525 return NULL;
3526 m->next = NULL;
3527 m->p_type = PT_DYNAMIC;
3528 m->count = 1;
3529 m->sections[0] = dynsec;
3531 return m;
3534 /* Possibly add or remove segments from the segment map. */
3536 static bfd_boolean
3537 elf_modify_segment_map (bfd *abfd,
3538 struct bfd_link_info *info,
3539 bfd_boolean remove_empty_load)
3541 struct elf_segment_map **m;
3542 const struct elf_backend_data *bed;
3544 /* The placement algorithm assumes that non allocated sections are
3545 not in PT_LOAD segments. We ensure this here by removing such
3546 sections from the segment map. We also remove excluded
3547 sections. Finally, any PT_LOAD segment without sections is
3548 removed. */
3549 m = &elf_tdata (abfd)->segment_map;
3550 while (*m)
3552 unsigned int i, new_count;
3554 for (new_count = 0, i = 0; i < (*m)->count; i++)
3556 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3557 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3558 || (*m)->p_type != PT_LOAD))
3560 (*m)->sections[new_count] = (*m)->sections[i];
3561 new_count++;
3564 (*m)->count = new_count;
3566 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3567 *m = (*m)->next;
3568 else
3569 m = &(*m)->next;
3572 bed = get_elf_backend_data (abfd);
3573 if (bed->elf_backend_modify_segment_map != NULL)
3575 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3576 return FALSE;
3579 return TRUE;
3582 /* Set up a mapping from BFD sections to program segments. */
3584 bfd_boolean
3585 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3587 unsigned int count;
3588 struct elf_segment_map *m;
3589 asection **sections = NULL;
3590 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3591 bfd_boolean no_user_phdrs;
3593 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3594 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3596 asection *s;
3597 unsigned int i;
3598 struct elf_segment_map *mfirst;
3599 struct elf_segment_map **pm;
3600 asection *last_hdr;
3601 bfd_vma last_size;
3602 unsigned int phdr_index;
3603 bfd_vma maxpagesize;
3604 asection **hdrpp;
3605 bfd_boolean phdr_in_segment = TRUE;
3606 bfd_boolean writable;
3607 int tls_count = 0;
3608 asection *first_tls = NULL;
3609 asection *dynsec, *eh_frame_hdr;
3610 bfd_size_type amt;
3612 /* Select the allocated sections, and sort them. */
3614 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3615 if (sections == NULL)
3616 goto error_return;
3618 i = 0;
3619 for (s = abfd->sections; s != NULL; s = s->next)
3621 if ((s->flags & SEC_ALLOC) != 0)
3623 sections[i] = s;
3624 ++i;
3627 BFD_ASSERT (i <= bfd_count_sections (abfd));
3628 count = i;
3630 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3632 /* Build the mapping. */
3634 mfirst = NULL;
3635 pm = &mfirst;
3637 /* If we have a .interp section, then create a PT_PHDR segment for
3638 the program headers and a PT_INTERP segment for the .interp
3639 section. */
3640 s = bfd_get_section_by_name (abfd, ".interp");
3641 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3643 amt = sizeof (struct elf_segment_map);
3644 m = bfd_zalloc (abfd, amt);
3645 if (m == NULL)
3646 goto error_return;
3647 m->next = NULL;
3648 m->p_type = PT_PHDR;
3649 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3650 m->p_flags = PF_R | PF_X;
3651 m->p_flags_valid = 1;
3652 m->includes_phdrs = 1;
3654 *pm = m;
3655 pm = &m->next;
3657 amt = sizeof (struct elf_segment_map);
3658 m = bfd_zalloc (abfd, amt);
3659 if (m == NULL)
3660 goto error_return;
3661 m->next = NULL;
3662 m->p_type = PT_INTERP;
3663 m->count = 1;
3664 m->sections[0] = s;
3666 *pm = m;
3667 pm = &m->next;
3670 /* Look through the sections. We put sections in the same program
3671 segment when the start of the second section can be placed within
3672 a few bytes of the end of the first section. */
3673 last_hdr = NULL;
3674 last_size = 0;
3675 phdr_index = 0;
3676 maxpagesize = bed->maxpagesize;
3677 writable = FALSE;
3678 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3679 if (dynsec != NULL
3680 && (dynsec->flags & SEC_LOAD) == 0)
3681 dynsec = NULL;
3683 /* Deal with -Ttext or something similar such that the first section
3684 is not adjacent to the program headers. This is an
3685 approximation, since at this point we don't know exactly how many
3686 program headers we will need. */
3687 if (count > 0)
3689 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3691 if (phdr_size == (bfd_size_type) -1)
3692 phdr_size = get_program_header_size (abfd, info);
3693 if ((abfd->flags & D_PAGED) == 0
3694 || sections[0]->lma < phdr_size
3695 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3696 phdr_in_segment = FALSE;
3699 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3701 asection *hdr;
3702 bfd_boolean new_segment;
3704 hdr = *hdrpp;
3706 /* See if this section and the last one will fit in the same
3707 segment. */
3709 if (last_hdr == NULL)
3711 /* If we don't have a segment yet, then we don't need a new
3712 one (we build the last one after this loop). */
3713 new_segment = FALSE;
3715 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3717 /* If this section has a different relation between the
3718 virtual address and the load address, then we need a new
3719 segment. */
3720 new_segment = TRUE;
3722 /* In the next test we have to be careful when last_hdr->lma is close
3723 to the end of the address space. If the aligned address wraps
3724 around to the start of the address space, then there are no more
3725 pages left in memory and it is OK to assume that the current
3726 section can be included in the current segment. */
3727 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3728 > last_hdr->lma)
3729 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3730 <= hdr->lma))
3732 /* If putting this section in this segment would force us to
3733 skip a page in the segment, then we need a new segment. */
3734 new_segment = TRUE;
3736 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3737 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3739 /* We don't want to put a loadable section after a
3740 nonloadable section in the same segment.
3741 Consider .tbss sections as loadable for this purpose. */
3742 new_segment = TRUE;
3744 else if ((abfd->flags & D_PAGED) == 0)
3746 /* If the file is not demand paged, which means that we
3747 don't require the sections to be correctly aligned in the
3748 file, then there is no other reason for a new segment. */
3749 new_segment = FALSE;
3751 else if (! writable
3752 && (hdr->flags & SEC_READONLY) == 0
3753 && (((last_hdr->lma + last_size - 1)
3754 & ~(maxpagesize - 1))
3755 != (hdr->lma & ~(maxpagesize - 1))))
3757 /* We don't want to put a writable section in a read only
3758 segment, unless they are on the same page in memory
3759 anyhow. We already know that the last section does not
3760 bring us past the current section on the page, so the
3761 only case in which the new section is not on the same
3762 page as the previous section is when the previous section
3763 ends precisely on a page boundary. */
3764 new_segment = TRUE;
3766 else
3768 /* Otherwise, we can use the same segment. */
3769 new_segment = FALSE;
3772 /* Allow interested parties a chance to override our decision. */
3773 if (last_hdr != NULL
3774 && info != NULL
3775 && info->callbacks->override_segment_assignment != NULL)
3776 new_segment
3777 = info->callbacks->override_segment_assignment (info, abfd, hdr,
3778 last_hdr,
3779 new_segment);
3781 if (! new_segment)
3783 if ((hdr->flags & SEC_READONLY) == 0)
3784 writable = TRUE;
3785 last_hdr = hdr;
3786 /* .tbss sections effectively have zero size. */
3787 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3788 != SEC_THREAD_LOCAL)
3789 last_size = hdr->size;
3790 else
3791 last_size = 0;
3792 continue;
3795 /* We need a new program segment. We must create a new program
3796 header holding all the sections from phdr_index until hdr. */
3798 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3799 if (m == NULL)
3800 goto error_return;
3802 *pm = m;
3803 pm = &m->next;
3805 if ((hdr->flags & SEC_READONLY) == 0)
3806 writable = TRUE;
3807 else
3808 writable = FALSE;
3810 last_hdr = hdr;
3811 /* .tbss sections effectively have zero size. */
3812 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3813 last_size = hdr->size;
3814 else
3815 last_size = 0;
3816 phdr_index = i;
3817 phdr_in_segment = FALSE;
3820 /* Create a final PT_LOAD program segment. */
3821 if (last_hdr != NULL)
3823 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3824 if (m == NULL)
3825 goto error_return;
3827 *pm = m;
3828 pm = &m->next;
3831 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3832 if (dynsec != NULL)
3834 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3835 if (m == NULL)
3836 goto error_return;
3837 *pm = m;
3838 pm = &m->next;
3841 /* For each batch of consecutive loadable .note sections,
3842 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3843 because if we link together nonloadable .note sections and
3844 loadable .note sections, we will generate two .note sections
3845 in the output file. FIXME: Using names for section types is
3846 bogus anyhow. */
3847 for (s = abfd->sections; s != NULL; s = s->next)
3849 if ((s->flags & SEC_LOAD) != 0
3850 && CONST_STRNEQ (s->name, ".note"))
3852 asection *s2;
3853 unsigned count = 1;
3854 amt = sizeof (struct elf_segment_map);
3855 if (s->alignment_power == 2)
3856 for (s2 = s; s2->next != NULL; s2 = s2->next)
3858 if (s2->next->alignment_power == 2
3859 && (s2->next->flags & SEC_LOAD) != 0
3860 && CONST_STRNEQ (s2->next->name, ".note")
3861 && align_power (s2->vma + s2->size, 2)
3862 == s2->next->vma)
3863 count++;
3864 else
3865 break;
3867 amt += (count - 1) * sizeof (asection *);
3868 m = bfd_zalloc (abfd, amt);
3869 if (m == NULL)
3870 goto error_return;
3871 m->next = NULL;
3872 m->p_type = PT_NOTE;
3873 m->count = count;
3874 while (count > 1)
3876 m->sections[m->count - count--] = s;
3877 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3878 s = s->next;
3880 m->sections[m->count - 1] = s;
3881 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3882 *pm = m;
3883 pm = &m->next;
3885 if (s->flags & SEC_THREAD_LOCAL)
3887 if (! tls_count)
3888 first_tls = s;
3889 tls_count++;
3893 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3894 if (tls_count > 0)
3896 int i;
3898 amt = sizeof (struct elf_segment_map);
3899 amt += (tls_count - 1) * sizeof (asection *);
3900 m = bfd_zalloc (abfd, amt);
3901 if (m == NULL)
3902 goto error_return;
3903 m->next = NULL;
3904 m->p_type = PT_TLS;
3905 m->count = tls_count;
3906 /* Mandated PF_R. */
3907 m->p_flags = PF_R;
3908 m->p_flags_valid = 1;
3909 for (i = 0; i < tls_count; ++i)
3911 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3912 m->sections[i] = first_tls;
3913 first_tls = first_tls->next;
3916 *pm = m;
3917 pm = &m->next;
3920 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3921 segment. */
3922 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3923 if (eh_frame_hdr != NULL
3924 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3926 amt = sizeof (struct elf_segment_map);
3927 m = bfd_zalloc (abfd, amt);
3928 if (m == NULL)
3929 goto error_return;
3930 m->next = NULL;
3931 m->p_type = PT_GNU_EH_FRAME;
3932 m->count = 1;
3933 m->sections[0] = eh_frame_hdr->output_section;
3935 *pm = m;
3936 pm = &m->next;
3939 if (elf_tdata (abfd)->stack_flags)
3941 amt = sizeof (struct elf_segment_map);
3942 m = bfd_zalloc (abfd, amt);
3943 if (m == NULL)
3944 goto error_return;
3945 m->next = NULL;
3946 m->p_type = PT_GNU_STACK;
3947 m->p_flags = elf_tdata (abfd)->stack_flags;
3948 m->p_flags_valid = 1;
3950 *pm = m;
3951 pm = &m->next;
3954 if (info != NULL && info->relro)
3956 for (m = mfirst; m != NULL; m = m->next)
3958 if (m->p_type == PT_LOAD)
3960 asection *last = m->sections[m->count - 1];
3961 bfd_vma vaddr = m->sections[0]->vma;
3962 bfd_vma filesz = last->vma - vaddr + last->size;
3964 if (vaddr < info->relro_end
3965 && vaddr >= info->relro_start
3966 && (vaddr + filesz) >= info->relro_end)
3967 break;
3971 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3972 if (m != NULL)
3974 amt = sizeof (struct elf_segment_map);
3975 m = bfd_zalloc (abfd, amt);
3976 if (m == NULL)
3977 goto error_return;
3978 m->next = NULL;
3979 m->p_type = PT_GNU_RELRO;
3980 m->p_flags = PF_R;
3981 m->p_flags_valid = 1;
3983 *pm = m;
3984 pm = &m->next;
3988 free (sections);
3989 elf_tdata (abfd)->segment_map = mfirst;
3992 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
3993 return FALSE;
3995 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3996 ++count;
3997 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
3999 return TRUE;
4001 error_return:
4002 if (sections != NULL)
4003 free (sections);
4004 return FALSE;
4007 /* Sort sections by address. */
4009 static int
4010 elf_sort_sections (const void *arg1, const void *arg2)
4012 const asection *sec1 = *(const asection **) arg1;
4013 const asection *sec2 = *(const asection **) arg2;
4014 bfd_size_type size1, size2;
4016 /* Sort by LMA first, since this is the address used to
4017 place the section into a segment. */
4018 if (sec1->lma < sec2->lma)
4019 return -1;
4020 else if (sec1->lma > sec2->lma)
4021 return 1;
4023 /* Then sort by VMA. Normally the LMA and the VMA will be
4024 the same, and this will do nothing. */
4025 if (sec1->vma < sec2->vma)
4026 return -1;
4027 else if (sec1->vma > sec2->vma)
4028 return 1;
4030 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4032 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4034 if (TOEND (sec1))
4036 if (TOEND (sec2))
4038 /* If the indicies are the same, do not return 0
4039 here, but continue to try the next comparison. */
4040 if (sec1->target_index - sec2->target_index != 0)
4041 return sec1->target_index - sec2->target_index;
4043 else
4044 return 1;
4046 else if (TOEND (sec2))
4047 return -1;
4049 #undef TOEND
4051 /* Sort by size, to put zero sized sections
4052 before others at the same address. */
4054 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4055 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4057 if (size1 < size2)
4058 return -1;
4059 if (size1 > size2)
4060 return 1;
4062 return sec1->target_index - sec2->target_index;
4065 /* Ian Lance Taylor writes:
4067 We shouldn't be using % with a negative signed number. That's just
4068 not good. We have to make sure either that the number is not
4069 negative, or that the number has an unsigned type. When the types
4070 are all the same size they wind up as unsigned. When file_ptr is a
4071 larger signed type, the arithmetic winds up as signed long long,
4072 which is wrong.
4074 What we're trying to say here is something like ``increase OFF by
4075 the least amount that will cause it to be equal to the VMA modulo
4076 the page size.'' */
4077 /* In other words, something like:
4079 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4080 off_offset = off % bed->maxpagesize;
4081 if (vma_offset < off_offset)
4082 adjustment = vma_offset + bed->maxpagesize - off_offset;
4083 else
4084 adjustment = vma_offset - off_offset;
4086 which can can be collapsed into the expression below. */
4088 static file_ptr
4089 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4091 return ((vma - off) % maxpagesize);
4094 static void
4095 print_segment_map (const struct elf_segment_map *m)
4097 unsigned int j;
4098 const char *pt = get_segment_type (m->p_type);
4099 char buf[32];
4101 if (pt == NULL)
4103 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4104 sprintf (buf, "LOPROC+%7.7x",
4105 (unsigned int) (m->p_type - PT_LOPROC));
4106 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4107 sprintf (buf, "LOOS+%7.7x",
4108 (unsigned int) (m->p_type - PT_LOOS));
4109 else
4110 snprintf (buf, sizeof (buf), "%8.8x",
4111 (unsigned int) m->p_type);
4112 pt = buf;
4114 fprintf (stderr, "%s:", pt);
4115 for (j = 0; j < m->count; j++)
4116 fprintf (stderr, " %s", m->sections [j]->name);
4117 putc ('\n',stderr);
4120 /* Assign file positions to the sections based on the mapping from
4121 sections to segments. This function also sets up some fields in
4122 the file header. */
4124 static bfd_boolean
4125 assign_file_positions_for_load_sections (bfd *abfd,
4126 struct bfd_link_info *link_info)
4128 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4129 struct elf_segment_map *m;
4130 Elf_Internal_Phdr *phdrs;
4131 Elf_Internal_Phdr *p;
4132 file_ptr off;
4133 bfd_size_type maxpagesize;
4134 unsigned int alloc;
4135 unsigned int i, j;
4136 bfd_vma header_pad = 0;
4138 if (link_info == NULL
4139 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4140 return FALSE;
4142 alloc = 0;
4143 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4145 ++alloc;
4146 if (m->header_size)
4147 header_pad = m->header_size;
4150 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4151 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4152 elf_elfheader (abfd)->e_phnum = alloc;
4154 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4155 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4156 else
4157 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4158 >= alloc * bed->s->sizeof_phdr);
4160 if (alloc == 0)
4162 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4163 return TRUE;
4166 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4167 see assign_file_positions_except_relocs, so make sure we have
4168 that amount allocated, with trailing space cleared.
4169 The variable alloc contains the computed need, while elf_tdata
4170 (abfd)->program_header_size contains the size used for the
4171 layout.
4172 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4173 where the layout is forced to according to a larger size in the
4174 last iterations for the testcase ld-elf/header. */
4175 BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr
4176 == 0);
4177 phdrs = bfd_zalloc2 (abfd,
4178 (elf_tdata (abfd)->program_header_size
4179 / bed->s->sizeof_phdr),
4180 sizeof (Elf_Internal_Phdr));
4181 elf_tdata (abfd)->phdr = phdrs;
4182 if (phdrs == NULL)
4183 return FALSE;
4185 maxpagesize = 1;
4186 if ((abfd->flags & D_PAGED) != 0)
4187 maxpagesize = bed->maxpagesize;
4189 off = bed->s->sizeof_ehdr;
4190 off += alloc * bed->s->sizeof_phdr;
4191 if (header_pad < (bfd_vma) off)
4192 header_pad = 0;
4193 else
4194 header_pad -= off;
4195 off += header_pad;
4197 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4198 m != NULL;
4199 m = m->next, p++, j++)
4201 asection **secpp;
4202 bfd_vma off_adjust;
4203 bfd_boolean no_contents;
4205 /* If elf_segment_map is not from map_sections_to_segments, the
4206 sections may not be correctly ordered. NOTE: sorting should
4207 not be done to the PT_NOTE section of a corefile, which may
4208 contain several pseudo-sections artificially created by bfd.
4209 Sorting these pseudo-sections breaks things badly. */
4210 if (m->count > 1
4211 && !(elf_elfheader (abfd)->e_type == ET_CORE
4212 && m->p_type == PT_NOTE))
4213 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4214 elf_sort_sections);
4216 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4217 number of sections with contents contributing to both p_filesz
4218 and p_memsz, followed by a number of sections with no contents
4219 that just contribute to p_memsz. In this loop, OFF tracks next
4220 available file offset for PT_LOAD and PT_NOTE segments. */
4221 p->p_type = m->p_type;
4222 p->p_flags = m->p_flags;
4224 if (m->count == 0)
4225 p->p_vaddr = 0;
4226 else
4227 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4229 if (m->p_paddr_valid)
4230 p->p_paddr = m->p_paddr;
4231 else if (m->count == 0)
4232 p->p_paddr = 0;
4233 else
4234 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4236 if (p->p_type == PT_LOAD
4237 && (abfd->flags & D_PAGED) != 0)
4239 /* p_align in demand paged PT_LOAD segments effectively stores
4240 the maximum page size. When copying an executable with
4241 objcopy, we set m->p_align from the input file. Use this
4242 value for maxpagesize rather than bed->maxpagesize, which
4243 may be different. Note that we use maxpagesize for PT_TLS
4244 segment alignment later in this function, so we are relying
4245 on at least one PT_LOAD segment appearing before a PT_TLS
4246 segment. */
4247 if (m->p_align_valid)
4248 maxpagesize = m->p_align;
4250 p->p_align = maxpagesize;
4252 else if (m->p_align_valid)
4253 p->p_align = m->p_align;
4254 else if (m->count == 0)
4255 p->p_align = 1 << bed->s->log_file_align;
4256 else
4257 p->p_align = 0;
4259 no_contents = FALSE;
4260 off_adjust = 0;
4261 if (p->p_type == PT_LOAD
4262 && m->count > 0)
4264 bfd_size_type align;
4265 unsigned int align_power = 0;
4267 if (m->p_align_valid)
4268 align = p->p_align;
4269 else
4271 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4273 unsigned int secalign;
4275 secalign = bfd_get_section_alignment (abfd, *secpp);
4276 if (secalign > align_power)
4277 align_power = secalign;
4279 align = (bfd_size_type) 1 << align_power;
4280 if (align < maxpagesize)
4281 align = maxpagesize;
4284 for (i = 0; i < m->count; i++)
4285 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4286 /* If we aren't making room for this section, then
4287 it must be SHT_NOBITS regardless of what we've
4288 set via struct bfd_elf_special_section. */
4289 elf_section_type (m->sections[i]) = SHT_NOBITS;
4291 /* Find out whether this segment contains any loadable
4292 sections. */
4293 no_contents = TRUE;
4294 for (i = 0; i < m->count; i++)
4295 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4297 no_contents = FALSE;
4298 break;
4301 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4302 off += off_adjust;
4303 if (no_contents)
4305 /* We shouldn't need to align the segment on disk since
4306 the segment doesn't need file space, but the gABI
4307 arguably requires the alignment and glibc ld.so
4308 checks it. So to comply with the alignment
4309 requirement but not waste file space, we adjust
4310 p_offset for just this segment. (OFF_ADJUST is
4311 subtracted from OFF later.) This may put p_offset
4312 past the end of file, but that shouldn't matter. */
4314 else
4315 off_adjust = 0;
4317 /* Make sure the .dynamic section is the first section in the
4318 PT_DYNAMIC segment. */
4319 else if (p->p_type == PT_DYNAMIC
4320 && m->count > 1
4321 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4323 _bfd_error_handler
4324 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4325 abfd);
4326 bfd_set_error (bfd_error_bad_value);
4327 return FALSE;
4329 /* Set the note section type to SHT_NOTE. */
4330 else if (p->p_type == PT_NOTE)
4331 for (i = 0; i < m->count; i++)
4332 elf_section_type (m->sections[i]) = SHT_NOTE;
4334 p->p_offset = 0;
4335 p->p_filesz = 0;
4336 p->p_memsz = 0;
4338 if (m->includes_filehdr)
4340 if (!m->p_flags_valid)
4341 p->p_flags |= PF_R;
4342 p->p_filesz = bed->s->sizeof_ehdr;
4343 p->p_memsz = bed->s->sizeof_ehdr;
4344 if (m->count > 0)
4346 BFD_ASSERT (p->p_type == PT_LOAD);
4348 if (p->p_vaddr < (bfd_vma) off)
4350 (*_bfd_error_handler)
4351 (_("%B: Not enough room for program headers, try linking with -N"),
4352 abfd);
4353 bfd_set_error (bfd_error_bad_value);
4354 return FALSE;
4357 p->p_vaddr -= off;
4358 if (!m->p_paddr_valid)
4359 p->p_paddr -= off;
4363 if (m->includes_phdrs)
4365 if (!m->p_flags_valid)
4366 p->p_flags |= PF_R;
4368 if (!m->includes_filehdr)
4370 p->p_offset = bed->s->sizeof_ehdr;
4372 if (m->count > 0)
4374 BFD_ASSERT (p->p_type == PT_LOAD);
4375 p->p_vaddr -= off - p->p_offset;
4376 if (!m->p_paddr_valid)
4377 p->p_paddr -= off - p->p_offset;
4381 p->p_filesz += alloc * bed->s->sizeof_phdr;
4382 p->p_memsz += alloc * bed->s->sizeof_phdr;
4383 if (m->count)
4385 p->p_filesz += header_pad;
4386 p->p_memsz += header_pad;
4390 if (p->p_type == PT_LOAD
4391 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4393 if (!m->includes_filehdr && !m->includes_phdrs)
4394 p->p_offset = off;
4395 else
4397 file_ptr adjust;
4399 adjust = off - (p->p_offset + p->p_filesz);
4400 if (!no_contents)
4401 p->p_filesz += adjust;
4402 p->p_memsz += adjust;
4406 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4407 maps. Set filepos for sections in PT_LOAD segments, and in
4408 core files, for sections in PT_NOTE segments.
4409 assign_file_positions_for_non_load_sections will set filepos
4410 for other sections and update p_filesz for other segments. */
4411 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4413 asection *sec;
4414 bfd_size_type align;
4415 Elf_Internal_Shdr *this_hdr;
4417 sec = *secpp;
4418 this_hdr = &elf_section_data (sec)->this_hdr;
4419 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4421 if ((p->p_type == PT_LOAD
4422 || p->p_type == PT_TLS)
4423 && (this_hdr->sh_type != SHT_NOBITS
4424 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4425 && ((this_hdr->sh_flags & SHF_TLS) == 0
4426 || p->p_type == PT_TLS))))
4428 bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
4430 if (adjust < 0)
4432 (*_bfd_error_handler)
4433 (_("%B: section %A vma 0x%lx overlaps previous sections"),
4434 abfd, sec, (unsigned long) sec->vma);
4435 adjust = 0;
4437 p->p_memsz += adjust;
4439 if (this_hdr->sh_type != SHT_NOBITS)
4441 off += adjust;
4442 p->p_filesz += adjust;
4446 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4448 /* The section at i == 0 is the one that actually contains
4449 everything. */
4450 if (i == 0)
4452 this_hdr->sh_offset = sec->filepos = off;
4453 off += this_hdr->sh_size;
4454 p->p_filesz = this_hdr->sh_size;
4455 p->p_memsz = 0;
4456 p->p_align = 1;
4458 else
4460 /* The rest are fake sections that shouldn't be written. */
4461 sec->filepos = 0;
4462 sec->size = 0;
4463 sec->flags = 0;
4464 continue;
4467 else
4469 if (p->p_type == PT_LOAD)
4471 this_hdr->sh_offset = sec->filepos = off;
4472 if (this_hdr->sh_type != SHT_NOBITS)
4473 off += this_hdr->sh_size;
4476 if (this_hdr->sh_type != SHT_NOBITS)
4478 p->p_filesz += this_hdr->sh_size;
4479 /* A load section without SHF_ALLOC is something like
4480 a note section in a PT_NOTE segment. These take
4481 file space but are not loaded into memory. */
4482 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4483 p->p_memsz += this_hdr->sh_size;
4485 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4487 if (p->p_type == PT_TLS)
4488 p->p_memsz += this_hdr->sh_size;
4490 /* .tbss is special. It doesn't contribute to p_memsz of
4491 normal segments. */
4492 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4493 p->p_memsz += this_hdr->sh_size;
4496 if (align > p->p_align
4497 && !m->p_align_valid
4498 && (p->p_type != PT_LOAD
4499 || (abfd->flags & D_PAGED) == 0))
4500 p->p_align = align;
4503 if (!m->p_flags_valid)
4505 p->p_flags |= PF_R;
4506 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4507 p->p_flags |= PF_X;
4508 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4509 p->p_flags |= PF_W;
4512 off -= off_adjust;
4514 /* Check that all sections are in a PT_LOAD segment.
4515 Don't check funky gdb generated core files. */
4516 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4517 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4519 Elf_Internal_Shdr *this_hdr;
4520 asection *sec;
4522 sec = *secpp;
4523 this_hdr = &(elf_section_data(sec)->this_hdr);
4524 if (this_hdr->sh_size != 0
4525 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4527 (*_bfd_error_handler)
4528 (_("%B: section `%A' can't be allocated in segment %d"),
4529 abfd, sec, j);
4530 print_segment_map (m);
4531 bfd_set_error (bfd_error_bad_value);
4532 return FALSE;
4537 elf_tdata (abfd)->next_file_pos = off;
4538 return TRUE;
4541 /* Assign file positions for the other sections. */
4543 static bfd_boolean
4544 assign_file_positions_for_non_load_sections (bfd *abfd,
4545 struct bfd_link_info *link_info)
4547 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4548 Elf_Internal_Shdr **i_shdrpp;
4549 Elf_Internal_Shdr **hdrpp;
4550 Elf_Internal_Phdr *phdrs;
4551 Elf_Internal_Phdr *p;
4552 struct elf_segment_map *m;
4553 bfd_vma filehdr_vaddr, filehdr_paddr;
4554 bfd_vma phdrs_vaddr, phdrs_paddr;
4555 file_ptr off;
4556 unsigned int num_sec;
4557 unsigned int i;
4558 unsigned int count;
4560 i_shdrpp = elf_elfsections (abfd);
4561 num_sec = elf_numsections (abfd);
4562 off = elf_tdata (abfd)->next_file_pos;
4563 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4565 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4566 Elf_Internal_Shdr *hdr;
4568 hdr = *hdrpp;
4569 if (hdr->bfd_section != NULL
4570 && (hdr->bfd_section->filepos != 0
4571 || (hdr->sh_type == SHT_NOBITS
4572 && hdr->contents == NULL)))
4573 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4574 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4576 if (hdr->sh_size != 0)
4577 ((*_bfd_error_handler)
4578 (_("%B: warning: allocated section `%s' not in segment"),
4579 abfd,
4580 (hdr->bfd_section == NULL
4581 ? "*unknown*"
4582 : hdr->bfd_section->name)));
4583 /* We don't need to page align empty sections. */
4584 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4585 off += vma_page_aligned_bias (hdr->sh_addr, off,
4586 bed->maxpagesize);
4587 else
4588 off += vma_page_aligned_bias (hdr->sh_addr, off,
4589 hdr->sh_addralign);
4590 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4591 FALSE);
4593 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4594 && hdr->bfd_section == NULL)
4595 || hdr == i_shdrpp[tdata->symtab_section]
4596 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4597 || hdr == i_shdrpp[tdata->strtab_section])
4598 hdr->sh_offset = -1;
4599 else
4600 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4603 /* Now that we have set the section file positions, we can set up
4604 the file positions for the non PT_LOAD segments. */
4605 count = 0;
4606 filehdr_vaddr = 0;
4607 filehdr_paddr = 0;
4608 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4609 phdrs_paddr = 0;
4610 phdrs = elf_tdata (abfd)->phdr;
4611 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4612 m != NULL;
4613 m = m->next, p++)
4615 ++count;
4616 if (p->p_type != PT_LOAD)
4617 continue;
4619 if (m->includes_filehdr)
4621 filehdr_vaddr = p->p_vaddr;
4622 filehdr_paddr = p->p_paddr;
4624 if (m->includes_phdrs)
4626 phdrs_vaddr = p->p_vaddr;
4627 phdrs_paddr = p->p_paddr;
4628 if (m->includes_filehdr)
4630 phdrs_vaddr += bed->s->sizeof_ehdr;
4631 phdrs_paddr += bed->s->sizeof_ehdr;
4636 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4637 m != NULL;
4638 m = m->next, p++)
4640 if (p->p_type == PT_GNU_RELRO)
4642 const Elf_Internal_Phdr *lp;
4644 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4646 if (link_info != NULL)
4648 /* During linking the range of the RELRO segment is passed
4649 in link_info. */
4650 for (lp = phdrs; lp < phdrs + count; ++lp)
4652 if (lp->p_type == PT_LOAD
4653 && lp->p_vaddr >= link_info->relro_start
4654 && lp->p_vaddr < link_info->relro_end
4655 && lp->p_vaddr + lp->p_filesz >= link_info->relro_end)
4656 break;
4659 else
4661 /* Otherwise we are copying an executable or shared
4662 library, but we need to use the same linker logic. */
4663 for (lp = phdrs; lp < phdrs + count; ++lp)
4665 if (lp->p_type == PT_LOAD
4666 && lp->p_paddr == p->p_paddr)
4667 break;
4671 if (lp < phdrs + count)
4673 p->p_vaddr = lp->p_vaddr;
4674 p->p_paddr = lp->p_paddr;
4675 p->p_offset = lp->p_offset;
4676 if (link_info != NULL)
4677 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4678 else if (m->p_size_valid)
4679 p->p_filesz = m->p_size;
4680 else
4681 abort ();
4682 p->p_memsz = p->p_filesz;
4683 p->p_align = 1;
4684 p->p_flags = (lp->p_flags & ~PF_W);
4686 else
4688 memset (p, 0, sizeof *p);
4689 p->p_type = PT_NULL;
4692 else if (m->count != 0)
4694 if (p->p_type != PT_LOAD
4695 && (p->p_type != PT_NOTE
4696 || bfd_get_format (abfd) != bfd_core))
4698 Elf_Internal_Shdr *hdr;
4699 asection *sect;
4701 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4703 sect = m->sections[m->count - 1];
4704 hdr = &elf_section_data (sect)->this_hdr;
4705 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4706 if (hdr->sh_type != SHT_NOBITS)
4707 p->p_filesz += hdr->sh_size;
4708 p->p_offset = m->sections[0]->filepos;
4711 else if (m->includes_filehdr)
4713 p->p_vaddr = filehdr_vaddr;
4714 if (! m->p_paddr_valid)
4715 p->p_paddr = filehdr_paddr;
4717 else if (m->includes_phdrs)
4719 p->p_vaddr = phdrs_vaddr;
4720 if (! m->p_paddr_valid)
4721 p->p_paddr = phdrs_paddr;
4725 elf_tdata (abfd)->next_file_pos = off;
4727 return TRUE;
4730 /* Work out the file positions of all the sections. This is called by
4731 _bfd_elf_compute_section_file_positions. All the section sizes and
4732 VMAs must be known before this is called.
4734 Reloc sections come in two flavours: Those processed specially as
4735 "side-channel" data attached to a section to which they apply, and
4736 those that bfd doesn't process as relocations. The latter sort are
4737 stored in a normal bfd section by bfd_section_from_shdr. We don't
4738 consider the former sort here, unless they form part of the loadable
4739 image. Reloc sections not assigned here will be handled later by
4740 assign_file_positions_for_relocs.
4742 We also don't set the positions of the .symtab and .strtab here. */
4744 static bfd_boolean
4745 assign_file_positions_except_relocs (bfd *abfd,
4746 struct bfd_link_info *link_info)
4748 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4749 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4750 file_ptr off;
4751 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4753 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4754 && bfd_get_format (abfd) != bfd_core)
4756 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4757 unsigned int num_sec = elf_numsections (abfd);
4758 Elf_Internal_Shdr **hdrpp;
4759 unsigned int i;
4761 /* Start after the ELF header. */
4762 off = i_ehdrp->e_ehsize;
4764 /* We are not creating an executable, which means that we are
4765 not creating a program header, and that the actual order of
4766 the sections in the file is unimportant. */
4767 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4769 Elf_Internal_Shdr *hdr;
4771 hdr = *hdrpp;
4772 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4773 && hdr->bfd_section == NULL)
4774 || i == tdata->symtab_section
4775 || i == tdata->symtab_shndx_section
4776 || i == tdata->strtab_section)
4778 hdr->sh_offset = -1;
4780 else
4781 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4784 else
4786 unsigned int alloc;
4788 /* Assign file positions for the loaded sections based on the
4789 assignment of sections to segments. */
4790 if (!assign_file_positions_for_load_sections (abfd, link_info))
4791 return FALSE;
4793 /* And for non-load sections. */
4794 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4795 return FALSE;
4797 if (bed->elf_backend_modify_program_headers != NULL)
4799 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4800 return FALSE;
4803 /* Write out the program headers. */
4804 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4805 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4806 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4807 return FALSE;
4809 off = tdata->next_file_pos;
4812 /* Place the section headers. */
4813 off = align_file_position (off, 1 << bed->s->log_file_align);
4814 i_ehdrp->e_shoff = off;
4815 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4817 tdata->next_file_pos = off;
4819 return TRUE;
4822 static bfd_boolean
4823 prep_headers (bfd *abfd)
4825 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4826 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4827 struct elf_strtab_hash *shstrtab;
4828 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4830 i_ehdrp = elf_elfheader (abfd);
4832 shstrtab = _bfd_elf_strtab_init ();
4833 if (shstrtab == NULL)
4834 return FALSE;
4836 elf_shstrtab (abfd) = shstrtab;
4838 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4839 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4840 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4841 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4843 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4844 i_ehdrp->e_ident[EI_DATA] =
4845 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4846 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4848 if ((abfd->flags & DYNAMIC) != 0)
4849 i_ehdrp->e_type = ET_DYN;
4850 else if ((abfd->flags & EXEC_P) != 0)
4851 i_ehdrp->e_type = ET_EXEC;
4852 else if (bfd_get_format (abfd) == bfd_core)
4853 i_ehdrp->e_type = ET_CORE;
4854 else
4855 i_ehdrp->e_type = ET_REL;
4857 switch (bfd_get_arch (abfd))
4859 case bfd_arch_unknown:
4860 i_ehdrp->e_machine = EM_NONE;
4861 break;
4863 /* There used to be a long list of cases here, each one setting
4864 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4865 in the corresponding bfd definition. To avoid duplication,
4866 the switch was removed. Machines that need special handling
4867 can generally do it in elf_backend_final_write_processing(),
4868 unless they need the information earlier than the final write.
4869 Such need can generally be supplied by replacing the tests for
4870 e_machine with the conditions used to determine it. */
4871 default:
4872 i_ehdrp->e_machine = bed->elf_machine_code;
4875 i_ehdrp->e_version = bed->s->ev_current;
4876 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4878 /* No program header, for now. */
4879 i_ehdrp->e_phoff = 0;
4880 i_ehdrp->e_phentsize = 0;
4881 i_ehdrp->e_phnum = 0;
4883 /* Each bfd section is section header entry. */
4884 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4885 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4887 /* If we're building an executable, we'll need a program header table. */
4888 if (abfd->flags & EXEC_P)
4889 /* It all happens later. */
4891 else
4893 i_ehdrp->e_phentsize = 0;
4894 i_phdrp = 0;
4895 i_ehdrp->e_phoff = 0;
4898 elf_tdata (abfd)->symtab_hdr.sh_name =
4899 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4900 elf_tdata (abfd)->strtab_hdr.sh_name =
4901 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4902 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4903 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4904 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4905 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4906 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4907 return FALSE;
4909 return TRUE;
4912 /* Assign file positions for all the reloc sections which are not part
4913 of the loadable file image. */
4915 void
4916 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4918 file_ptr off;
4919 unsigned int i, num_sec;
4920 Elf_Internal_Shdr **shdrpp;
4922 off = elf_tdata (abfd)->next_file_pos;
4924 num_sec = elf_numsections (abfd);
4925 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4927 Elf_Internal_Shdr *shdrp;
4929 shdrp = *shdrpp;
4930 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4931 && shdrp->sh_offset == -1)
4932 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4935 elf_tdata (abfd)->next_file_pos = off;
4938 bfd_boolean
4939 _bfd_elf_write_object_contents (bfd *abfd)
4941 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4942 Elf_Internal_Ehdr *i_ehdrp;
4943 Elf_Internal_Shdr **i_shdrp;
4944 bfd_boolean failed;
4945 unsigned int count, num_sec;
4947 if (! abfd->output_has_begun
4948 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4949 return FALSE;
4951 i_shdrp = elf_elfsections (abfd);
4952 i_ehdrp = elf_elfheader (abfd);
4954 failed = FALSE;
4955 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4956 if (failed)
4957 return FALSE;
4959 _bfd_elf_assign_file_positions_for_relocs (abfd);
4961 /* After writing the headers, we need to write the sections too... */
4962 num_sec = elf_numsections (abfd);
4963 for (count = 1; count < num_sec; count++)
4965 if (bed->elf_backend_section_processing)
4966 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4967 if (i_shdrp[count]->contents)
4969 bfd_size_type amt = i_shdrp[count]->sh_size;
4971 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4972 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4973 return FALSE;
4977 /* Write out the section header names. */
4978 if (elf_shstrtab (abfd) != NULL
4979 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4980 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4981 return FALSE;
4983 if (bed->elf_backend_final_write_processing)
4984 (*bed->elf_backend_final_write_processing) (abfd,
4985 elf_tdata (abfd)->linker);
4987 if (!bed->s->write_shdrs_and_ehdr (abfd))
4988 return FALSE;
4990 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4991 if (elf_tdata (abfd)->after_write_object_contents)
4992 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
4994 return TRUE;
4997 bfd_boolean
4998 _bfd_elf_write_corefile_contents (bfd *abfd)
5000 /* Hopefully this can be done just like an object file. */
5001 return _bfd_elf_write_object_contents (abfd);
5004 /* Given a section, search the header to find them. */
5006 unsigned int
5007 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5009 const struct elf_backend_data *bed;
5010 unsigned int index;
5012 if (elf_section_data (asect) != NULL
5013 && elf_section_data (asect)->this_idx != 0)
5014 return elf_section_data (asect)->this_idx;
5016 if (bfd_is_abs_section (asect))
5017 index = SHN_ABS;
5018 else if (bfd_is_com_section (asect))
5019 index = SHN_COMMON;
5020 else if (bfd_is_und_section (asect))
5021 index = SHN_UNDEF;
5022 else
5023 index = SHN_BAD;
5025 bed = get_elf_backend_data (abfd);
5026 if (bed->elf_backend_section_from_bfd_section)
5028 int retval = index;
5030 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5031 return retval;
5034 if (index == SHN_BAD)
5035 bfd_set_error (bfd_error_nonrepresentable_section);
5037 return index;
5040 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5041 on error. */
5044 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5046 asymbol *asym_ptr = *asym_ptr_ptr;
5047 int idx;
5048 flagword flags = asym_ptr->flags;
5050 /* When gas creates relocations against local labels, it creates its
5051 own symbol for the section, but does put the symbol into the
5052 symbol chain, so udata is 0. When the linker is generating
5053 relocatable output, this section symbol may be for one of the
5054 input sections rather than the output section. */
5055 if (asym_ptr->udata.i == 0
5056 && (flags & BSF_SECTION_SYM)
5057 && asym_ptr->section)
5059 asection *sec;
5060 int indx;
5062 sec = asym_ptr->section;
5063 if (sec->owner != abfd && sec->output_section != NULL)
5064 sec = sec->output_section;
5065 if (sec->owner == abfd
5066 && (indx = sec->index) < elf_num_section_syms (abfd)
5067 && elf_section_syms (abfd)[indx] != NULL)
5068 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5071 idx = asym_ptr->udata.i;
5073 if (idx == 0)
5075 /* This case can occur when using --strip-symbol on a symbol
5076 which is used in a relocation entry. */
5077 (*_bfd_error_handler)
5078 (_("%B: symbol `%s' required but not present"),
5079 abfd, bfd_asymbol_name (asym_ptr));
5080 bfd_set_error (bfd_error_no_symbols);
5081 return -1;
5084 #if DEBUG & 4
5086 fprintf (stderr,
5087 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5088 (long) asym_ptr, asym_ptr->name, idx, flags,
5089 elf_symbol_flags (flags));
5090 fflush (stderr);
5092 #endif
5094 return idx;
5097 /* Rewrite program header information. */
5099 static bfd_boolean
5100 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5102 Elf_Internal_Ehdr *iehdr;
5103 struct elf_segment_map *map;
5104 struct elf_segment_map *map_first;
5105 struct elf_segment_map **pointer_to_map;
5106 Elf_Internal_Phdr *segment;
5107 asection *section;
5108 unsigned int i;
5109 unsigned int num_segments;
5110 bfd_boolean phdr_included = FALSE;
5111 bfd_boolean p_paddr_valid;
5112 bfd_vma maxpagesize;
5113 struct elf_segment_map *phdr_adjust_seg = NULL;
5114 unsigned int phdr_adjust_num = 0;
5115 const struct elf_backend_data *bed;
5117 bed = get_elf_backend_data (ibfd);
5118 iehdr = elf_elfheader (ibfd);
5120 map_first = NULL;
5121 pointer_to_map = &map_first;
5123 num_segments = elf_elfheader (ibfd)->e_phnum;
5124 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5126 /* Returns the end address of the segment + 1. */
5127 #define SEGMENT_END(segment, start) \
5128 (start + (segment->p_memsz > segment->p_filesz \
5129 ? segment->p_memsz : segment->p_filesz))
5131 #define SECTION_SIZE(section, segment) \
5132 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5133 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5134 ? section->size : 0)
5136 /* Returns TRUE if the given section is contained within
5137 the given segment. VMA addresses are compared. */
5138 #define IS_CONTAINED_BY_VMA(section, segment) \
5139 (section->vma >= segment->p_vaddr \
5140 && (section->vma + SECTION_SIZE (section, segment) \
5141 <= (SEGMENT_END (segment, segment->p_vaddr))))
5143 /* Returns TRUE if the given section is contained within
5144 the given segment. LMA addresses are compared. */
5145 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5146 (section->lma >= base \
5147 && (section->lma + SECTION_SIZE (section, segment) \
5148 <= SEGMENT_END (segment, base)))
5150 /* Handle PT_NOTE segment. */
5151 #define IS_NOTE(p, s) \
5152 (p->p_type == PT_NOTE \
5153 && elf_section_type (s) == SHT_NOTE \
5154 && (bfd_vma) s->filepos >= p->p_offset \
5155 && ((bfd_vma) s->filepos + s->size \
5156 <= p->p_offset + p->p_filesz))
5158 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5159 etc. */
5160 #define IS_COREFILE_NOTE(p, s) \
5161 (IS_NOTE (p, s) \
5162 && bfd_get_format (ibfd) == bfd_core \
5163 && s->vma == 0 \
5164 && s->lma == 0)
5166 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5167 linker, which generates a PT_INTERP section with p_vaddr and
5168 p_memsz set to 0. */
5169 #define IS_SOLARIS_PT_INTERP(p, s) \
5170 (p->p_vaddr == 0 \
5171 && p->p_paddr == 0 \
5172 && p->p_memsz == 0 \
5173 && p->p_filesz > 0 \
5174 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5175 && s->size > 0 \
5176 && (bfd_vma) s->filepos >= p->p_offset \
5177 && ((bfd_vma) s->filepos + s->size \
5178 <= p->p_offset + p->p_filesz))
5180 /* Decide if the given section should be included in the given segment.
5181 A section will be included if:
5182 1. It is within the address space of the segment -- we use the LMA
5183 if that is set for the segment and the VMA otherwise,
5184 2. It is an allocated section or a NOTE section in a PT_NOTE
5185 segment.
5186 3. There is an output section associated with it,
5187 4. The section has not already been allocated to a previous segment.
5188 5. PT_GNU_STACK segments do not include any sections.
5189 6. PT_TLS segment includes only SHF_TLS sections.
5190 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5191 8. PT_DYNAMIC should not contain empty sections at the beginning
5192 (with the possible exception of .dynamic). */
5193 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5194 ((((segment->p_paddr \
5195 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5196 : IS_CONTAINED_BY_VMA (section, segment)) \
5197 && (section->flags & SEC_ALLOC) != 0) \
5198 || IS_NOTE (segment, section)) \
5199 && segment->p_type != PT_GNU_STACK \
5200 && (segment->p_type != PT_TLS \
5201 || (section->flags & SEC_THREAD_LOCAL)) \
5202 && (segment->p_type == PT_LOAD \
5203 || segment->p_type == PT_TLS \
5204 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5205 && (segment->p_type != PT_DYNAMIC \
5206 || SECTION_SIZE (section, segment) > 0 \
5207 || (segment->p_paddr \
5208 ? segment->p_paddr != section->lma \
5209 : segment->p_vaddr != section->vma) \
5210 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5211 == 0)) \
5212 && !section->segment_mark)
5214 /* If the output section of a section in the input segment is NULL,
5215 it is removed from the corresponding output segment. */
5216 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5217 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5218 && section->output_section != NULL)
5220 /* Returns TRUE iff seg1 starts after the end of seg2. */
5221 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5222 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5224 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5225 their VMA address ranges and their LMA address ranges overlap.
5226 It is possible to have overlapping VMA ranges without overlapping LMA
5227 ranges. RedBoot images for example can have both .data and .bss mapped
5228 to the same VMA range, but with the .data section mapped to a different
5229 LMA. */
5230 #define SEGMENT_OVERLAPS(seg1, seg2) \
5231 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5232 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5233 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5234 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5236 /* Initialise the segment mark field. */
5237 for (section = ibfd->sections; section != NULL; section = section->next)
5238 section->segment_mark = FALSE;
5240 /* The Solaris linker creates program headers in which all the
5241 p_paddr fields are zero. When we try to objcopy or strip such a
5242 file, we get confused. Check for this case, and if we find it
5243 don't set the p_paddr_valid fields. */
5244 p_paddr_valid = FALSE;
5245 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5246 i < num_segments;
5247 i++, segment++)
5248 if (segment->p_paddr != 0)
5250 p_paddr_valid = TRUE;
5251 break;
5254 /* Scan through the segments specified in the program header
5255 of the input BFD. For this first scan we look for overlaps
5256 in the loadable segments. These can be created by weird
5257 parameters to objcopy. Also, fix some solaris weirdness. */
5258 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5259 i < num_segments;
5260 i++, segment++)
5262 unsigned int j;
5263 Elf_Internal_Phdr *segment2;
5265 if (segment->p_type == PT_INTERP)
5266 for (section = ibfd->sections; section; section = section->next)
5267 if (IS_SOLARIS_PT_INTERP (segment, section))
5269 /* Mininal change so that the normal section to segment
5270 assignment code will work. */
5271 segment->p_vaddr = section->vma;
5272 break;
5275 if (segment->p_type != PT_LOAD)
5277 /* Remove PT_GNU_RELRO segment. */
5278 if (segment->p_type == PT_GNU_RELRO)
5279 segment->p_type = PT_NULL;
5280 continue;
5283 /* Determine if this segment overlaps any previous segments. */
5284 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5286 bfd_signed_vma extra_length;
5288 if (segment2->p_type != PT_LOAD
5289 || !SEGMENT_OVERLAPS (segment, segment2))
5290 continue;
5292 /* Merge the two segments together. */
5293 if (segment2->p_vaddr < segment->p_vaddr)
5295 /* Extend SEGMENT2 to include SEGMENT and then delete
5296 SEGMENT. */
5297 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5298 - SEGMENT_END (segment2, segment2->p_vaddr));
5300 if (extra_length > 0)
5302 segment2->p_memsz += extra_length;
5303 segment2->p_filesz += extra_length;
5306 segment->p_type = PT_NULL;
5308 /* Since we have deleted P we must restart the outer loop. */
5309 i = 0;
5310 segment = elf_tdata (ibfd)->phdr;
5311 break;
5313 else
5315 /* Extend SEGMENT to include SEGMENT2 and then delete
5316 SEGMENT2. */
5317 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5318 - SEGMENT_END (segment, segment->p_vaddr));
5320 if (extra_length > 0)
5322 segment->p_memsz += extra_length;
5323 segment->p_filesz += extra_length;
5326 segment2->p_type = PT_NULL;
5331 /* The second scan attempts to assign sections to segments. */
5332 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5333 i < num_segments;
5334 i++, segment++)
5336 unsigned int section_count;
5337 asection **sections;
5338 asection *output_section;
5339 unsigned int isec;
5340 bfd_vma matching_lma;
5341 bfd_vma suggested_lma;
5342 unsigned int j;
5343 bfd_size_type amt;
5344 asection *first_section;
5345 bfd_boolean first_matching_lma;
5346 bfd_boolean first_suggested_lma;
5348 if (segment->p_type == PT_NULL)
5349 continue;
5351 first_section = NULL;
5352 /* Compute how many sections might be placed into this segment. */
5353 for (section = ibfd->sections, section_count = 0;
5354 section != NULL;
5355 section = section->next)
5357 /* Find the first section in the input segment, which may be
5358 removed from the corresponding output segment. */
5359 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5361 if (first_section == NULL)
5362 first_section = section;
5363 if (section->output_section != NULL)
5364 ++section_count;
5368 /* Allocate a segment map big enough to contain
5369 all of the sections we have selected. */
5370 amt = sizeof (struct elf_segment_map);
5371 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5372 map = bfd_zalloc (obfd, amt);
5373 if (map == NULL)
5374 return FALSE;
5376 /* Initialise the fields of the segment map. Default to
5377 using the physical address of the segment in the input BFD. */
5378 map->next = NULL;
5379 map->p_type = segment->p_type;
5380 map->p_flags = segment->p_flags;
5381 map->p_flags_valid = 1;
5383 /* If the first section in the input segment is removed, there is
5384 no need to preserve segment physical address in the corresponding
5385 output segment. */
5386 if (!first_section || first_section->output_section != NULL)
5388 map->p_paddr = segment->p_paddr;
5389 map->p_paddr_valid = p_paddr_valid;
5392 /* Determine if this segment contains the ELF file header
5393 and if it contains the program headers themselves. */
5394 map->includes_filehdr = (segment->p_offset == 0
5395 && segment->p_filesz >= iehdr->e_ehsize);
5396 map->includes_phdrs = 0;
5398 if (!phdr_included || segment->p_type != PT_LOAD)
5400 map->includes_phdrs =
5401 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5402 && (segment->p_offset + segment->p_filesz
5403 >= ((bfd_vma) iehdr->e_phoff
5404 + iehdr->e_phnum * iehdr->e_phentsize)));
5406 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5407 phdr_included = TRUE;
5410 if (section_count == 0)
5412 /* Special segments, such as the PT_PHDR segment, may contain
5413 no sections, but ordinary, loadable segments should contain
5414 something. They are allowed by the ELF spec however, so only
5415 a warning is produced. */
5416 if (segment->p_type == PT_LOAD)
5417 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5418 " detected, is this intentional ?\n"),
5419 ibfd);
5421 map->count = 0;
5422 *pointer_to_map = map;
5423 pointer_to_map = &map->next;
5425 continue;
5428 /* Now scan the sections in the input BFD again and attempt
5429 to add their corresponding output sections to the segment map.
5430 The problem here is how to handle an output section which has
5431 been moved (ie had its LMA changed). There are four possibilities:
5433 1. None of the sections have been moved.
5434 In this case we can continue to use the segment LMA from the
5435 input BFD.
5437 2. All of the sections have been moved by the same amount.
5438 In this case we can change the segment's LMA to match the LMA
5439 of the first section.
5441 3. Some of the sections have been moved, others have not.
5442 In this case those sections which have not been moved can be
5443 placed in the current segment which will have to have its size,
5444 and possibly its LMA changed, and a new segment or segments will
5445 have to be created to contain the other sections.
5447 4. The sections have been moved, but not by the same amount.
5448 In this case we can change the segment's LMA to match the LMA
5449 of the first section and we will have to create a new segment
5450 or segments to contain the other sections.
5452 In order to save time, we allocate an array to hold the section
5453 pointers that we are interested in. As these sections get assigned
5454 to a segment, they are removed from this array. */
5456 sections = bfd_malloc2 (section_count, sizeof (asection *));
5457 if (sections == NULL)
5458 return FALSE;
5460 /* Step One: Scan for segment vs section LMA conflicts.
5461 Also add the sections to the section array allocated above.
5462 Also add the sections to the current segment. In the common
5463 case, where the sections have not been moved, this means that
5464 we have completely filled the segment, and there is nothing
5465 more to do. */
5466 isec = 0;
5467 matching_lma = 0;
5468 suggested_lma = 0;
5469 first_matching_lma = TRUE;
5470 first_suggested_lma = TRUE;
5472 for (section = ibfd->sections;
5473 section != NULL;
5474 section = section->next)
5475 if (section == first_section)
5476 break;
5478 for (j = 0; section != NULL; section = section->next)
5480 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5482 output_section = section->output_section;
5484 sections[j++] = section;
5486 /* The Solaris native linker always sets p_paddr to 0.
5487 We try to catch that case here, and set it to the
5488 correct value. Note - some backends require that
5489 p_paddr be left as zero. */
5490 if (!p_paddr_valid
5491 && segment->p_vaddr != 0
5492 && !bed->want_p_paddr_set_to_zero
5493 && isec == 0
5494 && output_section->lma != 0
5495 && output_section->vma == (segment->p_vaddr
5496 + (map->includes_filehdr
5497 ? iehdr->e_ehsize
5498 : 0)
5499 + (map->includes_phdrs
5500 ? (iehdr->e_phnum
5501 * iehdr->e_phentsize)
5502 : 0)))
5503 map->p_paddr = segment->p_vaddr;
5505 /* Match up the physical address of the segment with the
5506 LMA address of the output section. */
5507 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5508 || IS_COREFILE_NOTE (segment, section)
5509 || (bed->want_p_paddr_set_to_zero
5510 && IS_CONTAINED_BY_VMA (output_section, segment)))
5512 if (first_matching_lma || output_section->lma < matching_lma)
5514 matching_lma = output_section->lma;
5515 first_matching_lma = FALSE;
5518 /* We assume that if the section fits within the segment
5519 then it does not overlap any other section within that
5520 segment. */
5521 map->sections[isec++] = output_section;
5523 else if (first_suggested_lma)
5525 suggested_lma = output_section->lma;
5526 first_suggested_lma = FALSE;
5529 if (j == section_count)
5530 break;
5534 BFD_ASSERT (j == section_count);
5536 /* Step Two: Adjust the physical address of the current segment,
5537 if necessary. */
5538 if (isec == section_count)
5540 /* All of the sections fitted within the segment as currently
5541 specified. This is the default case. Add the segment to
5542 the list of built segments and carry on to process the next
5543 program header in the input BFD. */
5544 map->count = section_count;
5545 *pointer_to_map = map;
5546 pointer_to_map = &map->next;
5548 if (p_paddr_valid
5549 && !bed->want_p_paddr_set_to_zero
5550 && matching_lma != map->p_paddr
5551 && !map->includes_filehdr
5552 && !map->includes_phdrs)
5553 /* There is some padding before the first section in the
5554 segment. So, we must account for that in the output
5555 segment's vma. */
5556 map->p_vaddr_offset = matching_lma - map->p_paddr;
5558 free (sections);
5559 continue;
5561 else
5563 if (!first_matching_lma)
5565 /* At least one section fits inside the current segment.
5566 Keep it, but modify its physical address to match the
5567 LMA of the first section that fitted. */
5568 map->p_paddr = matching_lma;
5570 else
5572 /* None of the sections fitted inside the current segment.
5573 Change the current segment's physical address to match
5574 the LMA of the first section. */
5575 map->p_paddr = suggested_lma;
5578 /* Offset the segment physical address from the lma
5579 to allow for space taken up by elf headers. */
5580 if (map->includes_filehdr)
5582 if (map->p_paddr >= iehdr->e_ehsize)
5583 map->p_paddr -= iehdr->e_ehsize;
5584 else
5586 map->includes_filehdr = FALSE;
5587 map->includes_phdrs = FALSE;
5591 if (map->includes_phdrs)
5593 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5595 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5597 /* iehdr->e_phnum is just an estimate of the number
5598 of program headers that we will need. Make a note
5599 here of the number we used and the segment we chose
5600 to hold these headers, so that we can adjust the
5601 offset when we know the correct value. */
5602 phdr_adjust_num = iehdr->e_phnum;
5603 phdr_adjust_seg = map;
5605 else
5606 map->includes_phdrs = FALSE;
5610 /* Step Three: Loop over the sections again, this time assigning
5611 those that fit to the current segment and removing them from the
5612 sections array; but making sure not to leave large gaps. Once all
5613 possible sections have been assigned to the current segment it is
5614 added to the list of built segments and if sections still remain
5615 to be assigned, a new segment is constructed before repeating
5616 the loop. */
5617 isec = 0;
5620 map->count = 0;
5621 suggested_lma = 0;
5622 first_suggested_lma = TRUE;
5624 /* Fill the current segment with sections that fit. */
5625 for (j = 0; j < section_count; j++)
5627 section = sections[j];
5629 if (section == NULL)
5630 continue;
5632 output_section = section->output_section;
5634 BFD_ASSERT (output_section != NULL);
5636 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5637 || IS_COREFILE_NOTE (segment, section))
5639 if (map->count == 0)
5641 /* If the first section in a segment does not start at
5642 the beginning of the segment, then something is
5643 wrong. */
5644 if (output_section->lma
5645 != (map->p_paddr
5646 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5647 + (map->includes_phdrs
5648 ? iehdr->e_phnum * iehdr->e_phentsize
5649 : 0)))
5650 abort ();
5652 else
5654 asection *prev_sec;
5656 prev_sec = map->sections[map->count - 1];
5658 /* If the gap between the end of the previous section
5659 and the start of this section is more than
5660 maxpagesize then we need to start a new segment. */
5661 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5662 maxpagesize)
5663 < BFD_ALIGN (output_section->lma, maxpagesize))
5664 || (prev_sec->lma + prev_sec->size
5665 > output_section->lma))
5667 if (first_suggested_lma)
5669 suggested_lma = output_section->lma;
5670 first_suggested_lma = FALSE;
5673 continue;
5677 map->sections[map->count++] = output_section;
5678 ++isec;
5679 sections[j] = NULL;
5680 section->segment_mark = TRUE;
5682 else if (first_suggested_lma)
5684 suggested_lma = output_section->lma;
5685 first_suggested_lma = FALSE;
5689 BFD_ASSERT (map->count > 0);
5691 /* Add the current segment to the list of built segments. */
5692 *pointer_to_map = map;
5693 pointer_to_map = &map->next;
5695 if (isec < section_count)
5697 /* We still have not allocated all of the sections to
5698 segments. Create a new segment here, initialise it
5699 and carry on looping. */
5700 amt = sizeof (struct elf_segment_map);
5701 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5702 map = bfd_alloc (obfd, amt);
5703 if (map == NULL)
5705 free (sections);
5706 return FALSE;
5709 /* Initialise the fields of the segment map. Set the physical
5710 physical address to the LMA of the first section that has
5711 not yet been assigned. */
5712 map->next = NULL;
5713 map->p_type = segment->p_type;
5714 map->p_flags = segment->p_flags;
5715 map->p_flags_valid = 1;
5716 map->p_paddr = suggested_lma;
5717 map->p_paddr_valid = p_paddr_valid;
5718 map->includes_filehdr = 0;
5719 map->includes_phdrs = 0;
5722 while (isec < section_count);
5724 free (sections);
5727 elf_tdata (obfd)->segment_map = map_first;
5729 /* If we had to estimate the number of program headers that were
5730 going to be needed, then check our estimate now and adjust
5731 the offset if necessary. */
5732 if (phdr_adjust_seg != NULL)
5734 unsigned int count;
5736 for (count = 0, map = map_first; map != NULL; map = map->next)
5737 count++;
5739 if (count > phdr_adjust_num)
5740 phdr_adjust_seg->p_paddr
5741 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5744 #undef SEGMENT_END
5745 #undef SECTION_SIZE
5746 #undef IS_CONTAINED_BY_VMA
5747 #undef IS_CONTAINED_BY_LMA
5748 #undef IS_NOTE
5749 #undef IS_COREFILE_NOTE
5750 #undef IS_SOLARIS_PT_INTERP
5751 #undef IS_SECTION_IN_INPUT_SEGMENT
5752 #undef INCLUDE_SECTION_IN_SEGMENT
5753 #undef SEGMENT_AFTER_SEGMENT
5754 #undef SEGMENT_OVERLAPS
5755 return TRUE;
5758 /* Copy ELF program header information. */
5760 static bfd_boolean
5761 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5763 Elf_Internal_Ehdr *iehdr;
5764 struct elf_segment_map *map;
5765 struct elf_segment_map *map_first;
5766 struct elf_segment_map **pointer_to_map;
5767 Elf_Internal_Phdr *segment;
5768 unsigned int i;
5769 unsigned int num_segments;
5770 bfd_boolean phdr_included = FALSE;
5771 bfd_boolean p_paddr_valid;
5773 iehdr = elf_elfheader (ibfd);
5775 map_first = NULL;
5776 pointer_to_map = &map_first;
5778 /* If all the segment p_paddr fields are zero, don't set
5779 map->p_paddr_valid. */
5780 p_paddr_valid = FALSE;
5781 num_segments = elf_elfheader (ibfd)->e_phnum;
5782 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5783 i < num_segments;
5784 i++, segment++)
5785 if (segment->p_paddr != 0)
5787 p_paddr_valid = TRUE;
5788 break;
5791 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5792 i < num_segments;
5793 i++, segment++)
5795 asection *section;
5796 unsigned int section_count;
5797 bfd_size_type amt;
5798 Elf_Internal_Shdr *this_hdr;
5799 asection *first_section = NULL;
5800 asection *lowest_section = NULL;
5802 /* Compute how many sections are in this segment. */
5803 for (section = ibfd->sections, section_count = 0;
5804 section != NULL;
5805 section = section->next)
5807 this_hdr = &(elf_section_data(section)->this_hdr);
5808 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5810 if (!first_section)
5811 first_section = lowest_section = section;
5812 if (section->lma < lowest_section->lma)
5813 lowest_section = section;
5814 section_count++;
5818 /* Allocate a segment map big enough to contain
5819 all of the sections we have selected. */
5820 amt = sizeof (struct elf_segment_map);
5821 if (section_count != 0)
5822 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5823 map = bfd_zalloc (obfd, amt);
5824 if (map == NULL)
5825 return FALSE;
5827 /* Initialize the fields of the output segment map with the
5828 input segment. */
5829 map->next = NULL;
5830 map->p_type = segment->p_type;
5831 map->p_flags = segment->p_flags;
5832 map->p_flags_valid = 1;
5833 map->p_paddr = segment->p_paddr;
5834 map->p_paddr_valid = p_paddr_valid;
5835 map->p_align = segment->p_align;
5836 map->p_align_valid = 1;
5837 map->p_vaddr_offset = 0;
5839 if (map->p_type == PT_GNU_RELRO)
5841 /* The PT_GNU_RELRO segment may contain the first a few
5842 bytes in the .got.plt section even if the whole .got.plt
5843 section isn't in the PT_GNU_RELRO segment. We won't
5844 change the size of the PT_GNU_RELRO segment. */
5845 map->p_size = segment->p_memsz;
5846 map->p_size_valid = 1;
5849 /* Determine if this segment contains the ELF file header
5850 and if it contains the program headers themselves. */
5851 map->includes_filehdr = (segment->p_offset == 0
5852 && segment->p_filesz >= iehdr->e_ehsize);
5854 map->includes_phdrs = 0;
5855 if (! phdr_included || segment->p_type != PT_LOAD)
5857 map->includes_phdrs =
5858 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5859 && (segment->p_offset + segment->p_filesz
5860 >= ((bfd_vma) iehdr->e_phoff
5861 + iehdr->e_phnum * iehdr->e_phentsize)));
5863 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5864 phdr_included = TRUE;
5867 if (map->includes_filehdr && first_section)
5868 /* We need to keep the space used by the headers fixed. */
5869 map->header_size = first_section->vma - segment->p_vaddr;
5871 if (!map->includes_phdrs
5872 && !map->includes_filehdr
5873 && map->p_paddr_valid)
5874 /* There is some other padding before the first section. */
5875 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5876 - segment->p_paddr);
5878 if (section_count != 0)
5880 unsigned int isec = 0;
5882 for (section = first_section;
5883 section != NULL;
5884 section = section->next)
5886 this_hdr = &(elf_section_data(section)->this_hdr);
5887 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5889 map->sections[isec++] = section->output_section;
5890 if (isec == section_count)
5891 break;
5896 map->count = section_count;
5897 *pointer_to_map = map;
5898 pointer_to_map = &map->next;
5901 elf_tdata (obfd)->segment_map = map_first;
5902 return TRUE;
5905 /* Copy private BFD data. This copies or rewrites ELF program header
5906 information. */
5908 static bfd_boolean
5909 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5911 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5912 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5913 return TRUE;
5915 if (elf_tdata (ibfd)->phdr == NULL)
5916 return TRUE;
5918 if (ibfd->xvec == obfd->xvec)
5920 /* Check to see if any sections in the input BFD
5921 covered by ELF program header have changed. */
5922 Elf_Internal_Phdr *segment;
5923 asection *section, *osec;
5924 unsigned int i, num_segments;
5925 Elf_Internal_Shdr *this_hdr;
5926 const struct elf_backend_data *bed;
5928 bed = get_elf_backend_data (ibfd);
5930 /* Regenerate the segment map if p_paddr is set to 0. */
5931 if (bed->want_p_paddr_set_to_zero)
5932 goto rewrite;
5934 /* Initialize the segment mark field. */
5935 for (section = obfd->sections; section != NULL;
5936 section = section->next)
5937 section->segment_mark = FALSE;
5939 num_segments = elf_elfheader (ibfd)->e_phnum;
5940 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5941 i < num_segments;
5942 i++, segment++)
5944 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5945 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5946 which severly confuses things, so always regenerate the segment
5947 map in this case. */
5948 if (segment->p_paddr == 0
5949 && segment->p_memsz == 0
5950 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5951 goto rewrite;
5953 for (section = ibfd->sections;
5954 section != NULL; section = section->next)
5956 /* We mark the output section so that we know it comes
5957 from the input BFD. */
5958 osec = section->output_section;
5959 if (osec)
5960 osec->segment_mark = TRUE;
5962 /* Check if this section is covered by the segment. */
5963 this_hdr = &(elf_section_data(section)->this_hdr);
5964 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5966 /* FIXME: Check if its output section is changed or
5967 removed. What else do we need to check? */
5968 if (osec == NULL
5969 || section->flags != osec->flags
5970 || section->lma != osec->lma
5971 || section->vma != osec->vma
5972 || section->size != osec->size
5973 || section->rawsize != osec->rawsize
5974 || section->alignment_power != osec->alignment_power)
5975 goto rewrite;
5980 /* Check to see if any output section do not come from the
5981 input BFD. */
5982 for (section = obfd->sections; section != NULL;
5983 section = section->next)
5985 if (section->segment_mark == FALSE)
5986 goto rewrite;
5987 else
5988 section->segment_mark = FALSE;
5991 return copy_elf_program_header (ibfd, obfd);
5994 rewrite:
5995 return rewrite_elf_program_header (ibfd, obfd);
5998 /* Initialize private output section information from input section. */
6000 bfd_boolean
6001 _bfd_elf_init_private_section_data (bfd *ibfd,
6002 asection *isec,
6003 bfd *obfd,
6004 asection *osec,
6005 struct bfd_link_info *link_info)
6008 Elf_Internal_Shdr *ihdr, *ohdr;
6009 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
6011 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6012 || obfd->xvec->flavour != bfd_target_elf_flavour)
6013 return TRUE;
6015 /* Don't copy the output ELF section type from input if the
6016 output BFD section flags have been set to something different.
6017 elf_fake_sections will set ELF section type based on BFD
6018 section flags. */
6019 if (elf_section_type (osec) == SHT_NULL
6020 && (osec->flags == isec->flags || !osec->flags))
6021 elf_section_type (osec) = elf_section_type (isec);
6023 /* FIXME: Is this correct for all OS/PROC specific flags? */
6024 elf_section_flags (osec) |= (elf_section_flags (isec)
6025 & (SHF_MASKOS | SHF_MASKPROC));
6027 /* Set things up for objcopy and relocatable link. The output
6028 SHT_GROUP section will have its elf_next_in_group pointing back
6029 to the input group members. Ignore linker created group section.
6030 See elfNN_ia64_object_p in elfxx-ia64.c. */
6031 if (need_group)
6033 if (elf_sec_group (isec) == NULL
6034 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6036 if (elf_section_flags (isec) & SHF_GROUP)
6037 elf_section_flags (osec) |= SHF_GROUP;
6038 elf_next_in_group (osec) = elf_next_in_group (isec);
6039 elf_section_data (osec)->group = elf_section_data (isec)->group;
6043 ihdr = &elf_section_data (isec)->this_hdr;
6045 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6046 don't use the output section of the linked-to section since it
6047 may be NULL at this point. */
6048 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6050 ohdr = &elf_section_data (osec)->this_hdr;
6051 ohdr->sh_flags |= SHF_LINK_ORDER;
6052 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6055 osec->use_rela_p = isec->use_rela_p;
6057 return TRUE;
6060 /* Copy private section information. This copies over the entsize
6061 field, and sometimes the info field. */
6063 bfd_boolean
6064 _bfd_elf_copy_private_section_data (bfd *ibfd,
6065 asection *isec,
6066 bfd *obfd,
6067 asection *osec)
6069 Elf_Internal_Shdr *ihdr, *ohdr;
6071 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6072 || obfd->xvec->flavour != bfd_target_elf_flavour)
6073 return TRUE;
6075 ihdr = &elf_section_data (isec)->this_hdr;
6076 ohdr = &elf_section_data (osec)->this_hdr;
6078 ohdr->sh_entsize = ihdr->sh_entsize;
6080 if (ihdr->sh_type == SHT_SYMTAB
6081 || ihdr->sh_type == SHT_DYNSYM
6082 || ihdr->sh_type == SHT_GNU_verneed
6083 || ihdr->sh_type == SHT_GNU_verdef)
6084 ohdr->sh_info = ihdr->sh_info;
6086 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6087 NULL);
6090 /* Copy private header information. */
6092 bfd_boolean
6093 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6095 asection *isec;
6097 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6098 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6099 return TRUE;
6101 /* Copy over private BFD data if it has not already been copied.
6102 This must be done here, rather than in the copy_private_bfd_data
6103 entry point, because the latter is called after the section
6104 contents have been set, which means that the program headers have
6105 already been worked out. */
6106 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6108 if (! copy_private_bfd_data (ibfd, obfd))
6109 return FALSE;
6112 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6113 but this might be wrong if we deleted the group section. */
6114 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6115 if (elf_section_type (isec) == SHT_GROUP
6116 && isec->output_section == NULL)
6118 asection *first = elf_next_in_group (isec);
6119 asection *s = first;
6120 while (s != NULL)
6122 if (s->output_section != NULL)
6124 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6125 elf_group_name (s->output_section) = NULL;
6127 s = elf_next_in_group (s);
6128 if (s == first)
6129 break;
6133 return TRUE;
6136 /* Copy private symbol information. If this symbol is in a section
6137 which we did not map into a BFD section, try to map the section
6138 index correctly. We use special macro definitions for the mapped
6139 section indices; these definitions are interpreted by the
6140 swap_out_syms function. */
6142 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6143 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6144 #define MAP_STRTAB (SHN_HIOS + 3)
6145 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6146 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6148 bfd_boolean
6149 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6150 asymbol *isymarg,
6151 bfd *obfd,
6152 asymbol *osymarg)
6154 elf_symbol_type *isym, *osym;
6156 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6157 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6158 return TRUE;
6160 isym = elf_symbol_from (ibfd, isymarg);
6161 osym = elf_symbol_from (obfd, osymarg);
6163 if (isym != NULL
6164 && isym->internal_elf_sym.st_shndx != 0
6165 && osym != NULL
6166 && bfd_is_abs_section (isym->symbol.section))
6168 unsigned int shndx;
6170 shndx = isym->internal_elf_sym.st_shndx;
6171 if (shndx == elf_onesymtab (ibfd))
6172 shndx = MAP_ONESYMTAB;
6173 else if (shndx == elf_dynsymtab (ibfd))
6174 shndx = MAP_DYNSYMTAB;
6175 else if (shndx == elf_tdata (ibfd)->strtab_section)
6176 shndx = MAP_STRTAB;
6177 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6178 shndx = MAP_SHSTRTAB;
6179 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6180 shndx = MAP_SYM_SHNDX;
6181 osym->internal_elf_sym.st_shndx = shndx;
6184 return TRUE;
6187 /* Swap out the symbols. */
6189 static bfd_boolean
6190 swap_out_syms (bfd *abfd,
6191 struct bfd_strtab_hash **sttp,
6192 int relocatable_p)
6194 const struct elf_backend_data *bed;
6195 int symcount;
6196 asymbol **syms;
6197 struct bfd_strtab_hash *stt;
6198 Elf_Internal_Shdr *symtab_hdr;
6199 Elf_Internal_Shdr *symtab_shndx_hdr;
6200 Elf_Internal_Shdr *symstrtab_hdr;
6201 bfd_byte *outbound_syms;
6202 bfd_byte *outbound_shndx;
6203 int idx;
6204 bfd_size_type amt;
6205 bfd_boolean name_local_sections;
6207 if (!elf_map_symbols (abfd))
6208 return FALSE;
6210 /* Dump out the symtabs. */
6211 stt = _bfd_elf_stringtab_init ();
6212 if (stt == NULL)
6213 return FALSE;
6215 bed = get_elf_backend_data (abfd);
6216 symcount = bfd_get_symcount (abfd);
6217 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6218 symtab_hdr->sh_type = SHT_SYMTAB;
6219 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6220 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6221 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6222 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6224 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6225 symstrtab_hdr->sh_type = SHT_STRTAB;
6227 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
6228 if (outbound_syms == NULL)
6230 _bfd_stringtab_free (stt);
6231 return FALSE;
6233 symtab_hdr->contents = outbound_syms;
6235 outbound_shndx = NULL;
6236 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6237 if (symtab_shndx_hdr->sh_name != 0)
6239 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6240 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6241 sizeof (Elf_External_Sym_Shndx));
6242 if (outbound_shndx == NULL)
6244 _bfd_stringtab_free (stt);
6245 return FALSE;
6248 symtab_shndx_hdr->contents = outbound_shndx;
6249 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6250 symtab_shndx_hdr->sh_size = amt;
6251 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6252 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6255 /* Now generate the data (for "contents"). */
6257 /* Fill in zeroth symbol and swap it out. */
6258 Elf_Internal_Sym sym;
6259 sym.st_name = 0;
6260 sym.st_value = 0;
6261 sym.st_size = 0;
6262 sym.st_info = 0;
6263 sym.st_other = 0;
6264 sym.st_shndx = SHN_UNDEF;
6265 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6266 outbound_syms += bed->s->sizeof_sym;
6267 if (outbound_shndx != NULL)
6268 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6271 name_local_sections
6272 = (bed->elf_backend_name_local_section_symbols
6273 && bed->elf_backend_name_local_section_symbols (abfd));
6275 syms = bfd_get_outsymbols (abfd);
6276 for (idx = 0; idx < symcount; idx++)
6278 Elf_Internal_Sym sym;
6279 bfd_vma value = syms[idx]->value;
6280 elf_symbol_type *type_ptr;
6281 flagword flags = syms[idx]->flags;
6282 int type;
6284 if (!name_local_sections
6285 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6287 /* Local section symbols have no name. */
6288 sym.st_name = 0;
6290 else
6292 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6293 syms[idx]->name,
6294 TRUE, FALSE);
6295 if (sym.st_name == (unsigned long) -1)
6297 _bfd_stringtab_free (stt);
6298 return FALSE;
6302 type_ptr = elf_symbol_from (abfd, syms[idx]);
6304 if ((flags & BSF_SECTION_SYM) == 0
6305 && bfd_is_com_section (syms[idx]->section))
6307 /* ELF common symbols put the alignment into the `value' field,
6308 and the size into the `size' field. This is backwards from
6309 how BFD handles it, so reverse it here. */
6310 sym.st_size = value;
6311 if (type_ptr == NULL
6312 || type_ptr->internal_elf_sym.st_value == 0)
6313 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6314 else
6315 sym.st_value = type_ptr->internal_elf_sym.st_value;
6316 sym.st_shndx = _bfd_elf_section_from_bfd_section
6317 (abfd, syms[idx]->section);
6319 else
6321 asection *sec = syms[idx]->section;
6322 unsigned int shndx;
6324 if (sec->output_section)
6326 value += sec->output_offset;
6327 sec = sec->output_section;
6330 /* Don't add in the section vma for relocatable output. */
6331 if (! relocatable_p)
6332 value += sec->vma;
6333 sym.st_value = value;
6334 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6336 if (bfd_is_abs_section (sec)
6337 && type_ptr != NULL
6338 && type_ptr->internal_elf_sym.st_shndx != 0)
6340 /* This symbol is in a real ELF section which we did
6341 not create as a BFD section. Undo the mapping done
6342 by copy_private_symbol_data. */
6343 shndx = type_ptr->internal_elf_sym.st_shndx;
6344 switch (shndx)
6346 case MAP_ONESYMTAB:
6347 shndx = elf_onesymtab (abfd);
6348 break;
6349 case MAP_DYNSYMTAB:
6350 shndx = elf_dynsymtab (abfd);
6351 break;
6352 case MAP_STRTAB:
6353 shndx = elf_tdata (abfd)->strtab_section;
6354 break;
6355 case MAP_SHSTRTAB:
6356 shndx = elf_tdata (abfd)->shstrtab_section;
6357 break;
6358 case MAP_SYM_SHNDX:
6359 shndx = elf_tdata (abfd)->symtab_shndx_section;
6360 break;
6361 default:
6362 break;
6365 else
6367 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6369 if (shndx == SHN_BAD)
6371 asection *sec2;
6373 /* Writing this would be a hell of a lot easier if
6374 we had some decent documentation on bfd, and
6375 knew what to expect of the library, and what to
6376 demand of applications. For example, it
6377 appears that `objcopy' might not set the
6378 section of a symbol to be a section that is
6379 actually in the output file. */
6380 sec2 = bfd_get_section_by_name (abfd, sec->name);
6381 if (sec2 == NULL)
6383 _bfd_error_handler (_("\
6384 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6385 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6386 sec->name);
6387 bfd_set_error (bfd_error_invalid_operation);
6388 _bfd_stringtab_free (stt);
6389 return FALSE;
6392 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6393 BFD_ASSERT (shndx != SHN_BAD);
6397 sym.st_shndx = shndx;
6400 if ((flags & BSF_THREAD_LOCAL) != 0)
6401 type = STT_TLS;
6402 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6403 type = STT_GNU_IFUNC;
6404 else if ((flags & BSF_FUNCTION) != 0)
6405 type = STT_FUNC;
6406 else if ((flags & BSF_OBJECT) != 0)
6407 type = STT_OBJECT;
6408 else if ((flags & BSF_RELC) != 0)
6409 type = STT_RELC;
6410 else if ((flags & BSF_SRELC) != 0)
6411 type = STT_SRELC;
6412 else
6413 type = STT_NOTYPE;
6415 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6416 type = STT_TLS;
6418 /* Processor-specific types. */
6419 if (type_ptr != NULL
6420 && bed->elf_backend_get_symbol_type)
6421 type = ((*bed->elf_backend_get_symbol_type)
6422 (&type_ptr->internal_elf_sym, type));
6424 if (flags & BSF_SECTION_SYM)
6426 if (flags & BSF_GLOBAL)
6427 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6428 else
6429 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6431 else if (bfd_is_com_section (syms[idx]->section))
6433 #ifdef USE_STT_COMMON
6434 if (type == STT_OBJECT)
6435 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6436 else
6437 #endif
6438 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6440 else if (bfd_is_und_section (syms[idx]->section))
6441 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6442 ? STB_WEAK
6443 : STB_GLOBAL),
6444 type);
6445 else if (flags & BSF_FILE)
6446 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6447 else
6449 int bind = STB_LOCAL;
6451 if (flags & BSF_LOCAL)
6452 bind = STB_LOCAL;
6453 else if (flags & BSF_WEAK)
6454 bind = STB_WEAK;
6455 else if (flags & BSF_GLOBAL)
6456 bind = STB_GLOBAL;
6458 sym.st_info = ELF_ST_INFO (bind, type);
6461 if (type_ptr != NULL)
6462 sym.st_other = type_ptr->internal_elf_sym.st_other;
6463 else
6464 sym.st_other = 0;
6466 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6467 outbound_syms += bed->s->sizeof_sym;
6468 if (outbound_shndx != NULL)
6469 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6472 *sttp = stt;
6473 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6474 symstrtab_hdr->sh_type = SHT_STRTAB;
6476 symstrtab_hdr->sh_flags = 0;
6477 symstrtab_hdr->sh_addr = 0;
6478 symstrtab_hdr->sh_entsize = 0;
6479 symstrtab_hdr->sh_link = 0;
6480 symstrtab_hdr->sh_info = 0;
6481 symstrtab_hdr->sh_addralign = 1;
6483 return TRUE;
6486 /* Return the number of bytes required to hold the symtab vector.
6488 Note that we base it on the count plus 1, since we will null terminate
6489 the vector allocated based on this size. However, the ELF symbol table
6490 always has a dummy entry as symbol #0, so it ends up even. */
6492 long
6493 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6495 long symcount;
6496 long symtab_size;
6497 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6499 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6500 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6501 if (symcount > 0)
6502 symtab_size -= sizeof (asymbol *);
6504 return symtab_size;
6507 long
6508 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6510 long symcount;
6511 long symtab_size;
6512 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6514 if (elf_dynsymtab (abfd) == 0)
6516 bfd_set_error (bfd_error_invalid_operation);
6517 return -1;
6520 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6521 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6522 if (symcount > 0)
6523 symtab_size -= sizeof (asymbol *);
6525 return symtab_size;
6528 long
6529 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6530 sec_ptr asect)
6532 return (asect->reloc_count + 1) * sizeof (arelent *);
6535 /* Canonicalize the relocs. */
6537 long
6538 _bfd_elf_canonicalize_reloc (bfd *abfd,
6539 sec_ptr section,
6540 arelent **relptr,
6541 asymbol **symbols)
6543 arelent *tblptr;
6544 unsigned int i;
6545 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6547 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6548 return -1;
6550 tblptr = section->relocation;
6551 for (i = 0; i < section->reloc_count; i++)
6552 *relptr++ = tblptr++;
6554 *relptr = NULL;
6556 return section->reloc_count;
6559 long
6560 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6562 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6563 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6565 if (symcount >= 0)
6566 bfd_get_symcount (abfd) = symcount;
6567 return symcount;
6570 long
6571 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6572 asymbol **allocation)
6574 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6575 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6577 if (symcount >= 0)
6578 bfd_get_dynamic_symcount (abfd) = symcount;
6579 return symcount;
6582 /* Return the size required for the dynamic reloc entries. Any loadable
6583 section that was actually installed in the BFD, and has type SHT_REL
6584 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6585 dynamic reloc section. */
6587 long
6588 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6590 long ret;
6591 asection *s;
6593 if (elf_dynsymtab (abfd) == 0)
6595 bfd_set_error (bfd_error_invalid_operation);
6596 return -1;
6599 ret = sizeof (arelent *);
6600 for (s = abfd->sections; s != NULL; s = s->next)
6601 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6602 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6603 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6604 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6605 * sizeof (arelent *));
6607 return ret;
6610 /* Canonicalize the dynamic relocation entries. Note that we return the
6611 dynamic relocations as a single block, although they are actually
6612 associated with particular sections; the interface, which was
6613 designed for SunOS style shared libraries, expects that there is only
6614 one set of dynamic relocs. Any loadable section that was actually
6615 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6616 dynamic symbol table, is considered to be a dynamic reloc section. */
6618 long
6619 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6620 arelent **storage,
6621 asymbol **syms)
6623 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6624 asection *s;
6625 long ret;
6627 if (elf_dynsymtab (abfd) == 0)
6629 bfd_set_error (bfd_error_invalid_operation);
6630 return -1;
6633 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6634 ret = 0;
6635 for (s = abfd->sections; s != NULL; s = s->next)
6637 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6638 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6639 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6641 arelent *p;
6642 long count, i;
6644 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6645 return -1;
6646 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6647 p = s->relocation;
6648 for (i = 0; i < count; i++)
6649 *storage++ = p++;
6650 ret += count;
6654 *storage = NULL;
6656 return ret;
6659 /* Read in the version information. */
6661 bfd_boolean
6662 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6664 bfd_byte *contents = NULL;
6665 unsigned int freeidx = 0;
6667 if (elf_dynverref (abfd) != 0)
6669 Elf_Internal_Shdr *hdr;
6670 Elf_External_Verneed *everneed;
6671 Elf_Internal_Verneed *iverneed;
6672 unsigned int i;
6673 bfd_byte *contents_end;
6675 hdr = &elf_tdata (abfd)->dynverref_hdr;
6677 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6678 sizeof (Elf_Internal_Verneed));
6679 if (elf_tdata (abfd)->verref == NULL)
6680 goto error_return;
6682 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6684 contents = bfd_malloc (hdr->sh_size);
6685 if (contents == NULL)
6687 error_return_verref:
6688 elf_tdata (abfd)->verref = NULL;
6689 elf_tdata (abfd)->cverrefs = 0;
6690 goto error_return;
6692 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6693 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6694 goto error_return_verref;
6696 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6697 goto error_return_verref;
6699 BFD_ASSERT (sizeof (Elf_External_Verneed)
6700 == sizeof (Elf_External_Vernaux));
6701 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6702 everneed = (Elf_External_Verneed *) contents;
6703 iverneed = elf_tdata (abfd)->verref;
6704 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6706 Elf_External_Vernaux *evernaux;
6707 Elf_Internal_Vernaux *ivernaux;
6708 unsigned int j;
6710 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6712 iverneed->vn_bfd = abfd;
6714 iverneed->vn_filename =
6715 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6716 iverneed->vn_file);
6717 if (iverneed->vn_filename == NULL)
6718 goto error_return_verref;
6720 if (iverneed->vn_cnt == 0)
6721 iverneed->vn_auxptr = NULL;
6722 else
6724 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6725 sizeof (Elf_Internal_Vernaux));
6726 if (iverneed->vn_auxptr == NULL)
6727 goto error_return_verref;
6730 if (iverneed->vn_aux
6731 > (size_t) (contents_end - (bfd_byte *) everneed))
6732 goto error_return_verref;
6734 evernaux = ((Elf_External_Vernaux *)
6735 ((bfd_byte *) everneed + iverneed->vn_aux));
6736 ivernaux = iverneed->vn_auxptr;
6737 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6739 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6741 ivernaux->vna_nodename =
6742 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6743 ivernaux->vna_name);
6744 if (ivernaux->vna_nodename == NULL)
6745 goto error_return_verref;
6747 if (j + 1 < iverneed->vn_cnt)
6748 ivernaux->vna_nextptr = ivernaux + 1;
6749 else
6750 ivernaux->vna_nextptr = NULL;
6752 if (ivernaux->vna_next
6753 > (size_t) (contents_end - (bfd_byte *) evernaux))
6754 goto error_return_verref;
6756 evernaux = ((Elf_External_Vernaux *)
6757 ((bfd_byte *) evernaux + ivernaux->vna_next));
6759 if (ivernaux->vna_other > freeidx)
6760 freeidx = ivernaux->vna_other;
6763 if (i + 1 < hdr->sh_info)
6764 iverneed->vn_nextref = iverneed + 1;
6765 else
6766 iverneed->vn_nextref = NULL;
6768 if (iverneed->vn_next
6769 > (size_t) (contents_end - (bfd_byte *) everneed))
6770 goto error_return_verref;
6772 everneed = ((Elf_External_Verneed *)
6773 ((bfd_byte *) everneed + iverneed->vn_next));
6776 free (contents);
6777 contents = NULL;
6780 if (elf_dynverdef (abfd) != 0)
6782 Elf_Internal_Shdr *hdr;
6783 Elf_External_Verdef *everdef;
6784 Elf_Internal_Verdef *iverdef;
6785 Elf_Internal_Verdef *iverdefarr;
6786 Elf_Internal_Verdef iverdefmem;
6787 unsigned int i;
6788 unsigned int maxidx;
6789 bfd_byte *contents_end_def, *contents_end_aux;
6791 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6793 contents = bfd_malloc (hdr->sh_size);
6794 if (contents == NULL)
6795 goto error_return;
6796 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6797 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6798 goto error_return;
6800 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6801 goto error_return;
6803 BFD_ASSERT (sizeof (Elf_External_Verdef)
6804 >= sizeof (Elf_External_Verdaux));
6805 contents_end_def = contents + hdr->sh_size
6806 - sizeof (Elf_External_Verdef);
6807 contents_end_aux = contents + hdr->sh_size
6808 - sizeof (Elf_External_Verdaux);
6810 /* We know the number of entries in the section but not the maximum
6811 index. Therefore we have to run through all entries and find
6812 the maximum. */
6813 everdef = (Elf_External_Verdef *) contents;
6814 maxidx = 0;
6815 for (i = 0; i < hdr->sh_info; ++i)
6817 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6819 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6820 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6822 if (iverdefmem.vd_next
6823 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6824 goto error_return;
6826 everdef = ((Elf_External_Verdef *)
6827 ((bfd_byte *) everdef + iverdefmem.vd_next));
6830 if (default_imported_symver)
6832 if (freeidx > maxidx)
6833 maxidx = ++freeidx;
6834 else
6835 freeidx = ++maxidx;
6837 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6838 sizeof (Elf_Internal_Verdef));
6839 if (elf_tdata (abfd)->verdef == NULL)
6840 goto error_return;
6842 elf_tdata (abfd)->cverdefs = maxidx;
6844 everdef = (Elf_External_Verdef *) contents;
6845 iverdefarr = elf_tdata (abfd)->verdef;
6846 for (i = 0; i < hdr->sh_info; i++)
6848 Elf_External_Verdaux *everdaux;
6849 Elf_Internal_Verdaux *iverdaux;
6850 unsigned int j;
6852 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6854 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6856 error_return_verdef:
6857 elf_tdata (abfd)->verdef = NULL;
6858 elf_tdata (abfd)->cverdefs = 0;
6859 goto error_return;
6862 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6863 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6865 iverdef->vd_bfd = abfd;
6867 if (iverdef->vd_cnt == 0)
6868 iverdef->vd_auxptr = NULL;
6869 else
6871 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6872 sizeof (Elf_Internal_Verdaux));
6873 if (iverdef->vd_auxptr == NULL)
6874 goto error_return_verdef;
6877 if (iverdef->vd_aux
6878 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6879 goto error_return_verdef;
6881 everdaux = ((Elf_External_Verdaux *)
6882 ((bfd_byte *) everdef + iverdef->vd_aux));
6883 iverdaux = iverdef->vd_auxptr;
6884 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6886 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6888 iverdaux->vda_nodename =
6889 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6890 iverdaux->vda_name);
6891 if (iverdaux->vda_nodename == NULL)
6892 goto error_return_verdef;
6894 if (j + 1 < iverdef->vd_cnt)
6895 iverdaux->vda_nextptr = iverdaux + 1;
6896 else
6897 iverdaux->vda_nextptr = NULL;
6899 if (iverdaux->vda_next
6900 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6901 goto error_return_verdef;
6903 everdaux = ((Elf_External_Verdaux *)
6904 ((bfd_byte *) everdaux + iverdaux->vda_next));
6907 if (iverdef->vd_cnt)
6908 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6910 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6911 iverdef->vd_nextdef = iverdef + 1;
6912 else
6913 iverdef->vd_nextdef = NULL;
6915 everdef = ((Elf_External_Verdef *)
6916 ((bfd_byte *) everdef + iverdef->vd_next));
6919 free (contents);
6920 contents = NULL;
6922 else if (default_imported_symver)
6924 if (freeidx < 3)
6925 freeidx = 3;
6926 else
6927 freeidx++;
6929 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6930 sizeof (Elf_Internal_Verdef));
6931 if (elf_tdata (abfd)->verdef == NULL)
6932 goto error_return;
6934 elf_tdata (abfd)->cverdefs = freeidx;
6937 /* Create a default version based on the soname. */
6938 if (default_imported_symver)
6940 Elf_Internal_Verdef *iverdef;
6941 Elf_Internal_Verdaux *iverdaux;
6943 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6945 iverdef->vd_version = VER_DEF_CURRENT;
6946 iverdef->vd_flags = 0;
6947 iverdef->vd_ndx = freeidx;
6948 iverdef->vd_cnt = 1;
6950 iverdef->vd_bfd = abfd;
6952 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6953 if (iverdef->vd_nodename == NULL)
6954 goto error_return_verdef;
6955 iverdef->vd_nextdef = NULL;
6956 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6957 if (iverdef->vd_auxptr == NULL)
6958 goto error_return_verdef;
6960 iverdaux = iverdef->vd_auxptr;
6961 iverdaux->vda_nodename = iverdef->vd_nodename;
6962 iverdaux->vda_nextptr = NULL;
6965 return TRUE;
6967 error_return:
6968 if (contents != NULL)
6969 free (contents);
6970 return FALSE;
6973 asymbol *
6974 _bfd_elf_make_empty_symbol (bfd *abfd)
6976 elf_symbol_type *newsym;
6977 bfd_size_type amt = sizeof (elf_symbol_type);
6979 newsym = bfd_zalloc (abfd, amt);
6980 if (!newsym)
6981 return NULL;
6982 else
6984 newsym->symbol.the_bfd = abfd;
6985 return &newsym->symbol;
6989 void
6990 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6991 asymbol *symbol,
6992 symbol_info *ret)
6994 bfd_symbol_info (symbol, ret);
6997 /* Return whether a symbol name implies a local symbol. Most targets
6998 use this function for the is_local_label_name entry point, but some
6999 override it. */
7001 bfd_boolean
7002 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7003 const char *name)
7005 /* Normal local symbols start with ``.L''. */
7006 if (name[0] == '.' && name[1] == 'L')
7007 return TRUE;
7009 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7010 DWARF debugging symbols starting with ``..''. */
7011 if (name[0] == '.' && name[1] == '.')
7012 return TRUE;
7014 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7015 emitting DWARF debugging output. I suspect this is actually a
7016 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7017 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7018 underscore to be emitted on some ELF targets). For ease of use,
7019 we treat such symbols as local. */
7020 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7021 return TRUE;
7023 return FALSE;
7026 alent *
7027 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7028 asymbol *symbol ATTRIBUTE_UNUSED)
7030 abort ();
7031 return NULL;
7034 bfd_boolean
7035 _bfd_elf_set_arch_mach (bfd *abfd,
7036 enum bfd_architecture arch,
7037 unsigned long machine)
7039 /* If this isn't the right architecture for this backend, and this
7040 isn't the generic backend, fail. */
7041 if (arch != get_elf_backend_data (abfd)->arch
7042 && arch != bfd_arch_unknown
7043 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7044 return FALSE;
7046 return bfd_default_set_arch_mach (abfd, arch, machine);
7049 /* Find the function to a particular section and offset,
7050 for error reporting. */
7052 static bfd_boolean
7053 elf_find_function (bfd *abfd,
7054 asection *section,
7055 asymbol **symbols,
7056 bfd_vma offset,
7057 const char **filename_ptr,
7058 const char **functionname_ptr)
7060 const char *filename;
7061 asymbol *func, *file;
7062 bfd_vma low_func;
7063 asymbol **p;
7064 /* ??? Given multiple file symbols, it is impossible to reliably
7065 choose the right file name for global symbols. File symbols are
7066 local symbols, and thus all file symbols must sort before any
7067 global symbols. The ELF spec may be interpreted to say that a
7068 file symbol must sort before other local symbols, but currently
7069 ld -r doesn't do this. So, for ld -r output, it is possible to
7070 make a better choice of file name for local symbols by ignoring
7071 file symbols appearing after a given local symbol. */
7072 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7073 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7075 filename = NULL;
7076 func = NULL;
7077 file = NULL;
7078 low_func = 0;
7079 state = nothing_seen;
7081 for (p = symbols; *p != NULL; p++)
7083 elf_symbol_type *q;
7084 unsigned int type;
7086 q = (elf_symbol_type *) *p;
7088 type = ELF_ST_TYPE (q->internal_elf_sym.st_info);
7089 switch (type)
7091 case STT_FILE:
7092 file = &q->symbol;
7093 if (state == symbol_seen)
7094 state = file_after_symbol_seen;
7095 continue;
7096 default:
7097 if (!bed->is_function_type (type))
7098 break;
7099 case STT_NOTYPE:
7100 if (bfd_get_section (&q->symbol) == section
7101 && q->symbol.value >= low_func
7102 && q->symbol.value <= offset)
7104 func = (asymbol *) q;
7105 low_func = q->symbol.value;
7106 filename = NULL;
7107 if (file != NULL
7108 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7109 || state != file_after_symbol_seen))
7110 filename = bfd_asymbol_name (file);
7112 break;
7114 if (state == nothing_seen)
7115 state = symbol_seen;
7118 if (func == NULL)
7119 return FALSE;
7121 if (filename_ptr)
7122 *filename_ptr = filename;
7123 if (functionname_ptr)
7124 *functionname_ptr = bfd_asymbol_name (func);
7126 return TRUE;
7129 /* Find the nearest line to a particular section and offset,
7130 for error reporting. */
7132 bfd_boolean
7133 _bfd_elf_find_nearest_line (bfd *abfd,
7134 asection *section,
7135 asymbol **symbols,
7136 bfd_vma offset,
7137 const char **filename_ptr,
7138 const char **functionname_ptr,
7139 unsigned int *line_ptr)
7141 bfd_boolean found;
7143 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7144 filename_ptr, functionname_ptr,
7145 line_ptr))
7147 if (!*functionname_ptr)
7148 elf_find_function (abfd, section, symbols, offset,
7149 *filename_ptr ? NULL : filename_ptr,
7150 functionname_ptr);
7152 return TRUE;
7155 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7156 filename_ptr, functionname_ptr,
7157 line_ptr, 0,
7158 &elf_tdata (abfd)->dwarf2_find_line_info))
7160 if (!*functionname_ptr)
7161 elf_find_function (abfd, section, symbols, offset,
7162 *filename_ptr ? NULL : filename_ptr,
7163 functionname_ptr);
7165 return TRUE;
7168 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7169 &found, filename_ptr,
7170 functionname_ptr, line_ptr,
7171 &elf_tdata (abfd)->line_info))
7172 return FALSE;
7173 if (found && (*functionname_ptr || *line_ptr))
7174 return TRUE;
7176 if (symbols == NULL)
7177 return FALSE;
7179 if (! elf_find_function (abfd, section, symbols, offset,
7180 filename_ptr, functionname_ptr))
7181 return FALSE;
7183 *line_ptr = 0;
7184 return TRUE;
7187 /* Find the line for a symbol. */
7189 bfd_boolean
7190 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7191 const char **filename_ptr, unsigned int *line_ptr)
7193 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7194 filename_ptr, line_ptr, 0,
7195 &elf_tdata (abfd)->dwarf2_find_line_info);
7198 /* After a call to bfd_find_nearest_line, successive calls to
7199 bfd_find_inliner_info can be used to get source information about
7200 each level of function inlining that terminated at the address
7201 passed to bfd_find_nearest_line. Currently this is only supported
7202 for DWARF2 with appropriate DWARF3 extensions. */
7204 bfd_boolean
7205 _bfd_elf_find_inliner_info (bfd *abfd,
7206 const char **filename_ptr,
7207 const char **functionname_ptr,
7208 unsigned int *line_ptr)
7210 bfd_boolean found;
7211 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7212 functionname_ptr, line_ptr,
7213 & elf_tdata (abfd)->dwarf2_find_line_info);
7214 return found;
7218 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7220 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7221 int ret = bed->s->sizeof_ehdr;
7223 if (!info->relocatable)
7225 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7227 if (phdr_size == (bfd_size_type) -1)
7229 struct elf_segment_map *m;
7231 phdr_size = 0;
7232 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7233 phdr_size += bed->s->sizeof_phdr;
7235 if (phdr_size == 0)
7236 phdr_size = get_program_header_size (abfd, info);
7239 elf_tdata (abfd)->program_header_size = phdr_size;
7240 ret += phdr_size;
7243 return ret;
7246 bfd_boolean
7247 _bfd_elf_set_section_contents (bfd *abfd,
7248 sec_ptr section,
7249 const void *location,
7250 file_ptr offset,
7251 bfd_size_type count)
7253 Elf_Internal_Shdr *hdr;
7254 bfd_signed_vma pos;
7256 if (! abfd->output_has_begun
7257 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7258 return FALSE;
7260 hdr = &elf_section_data (section)->this_hdr;
7261 pos = hdr->sh_offset + offset;
7262 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7263 || bfd_bwrite (location, count, abfd) != count)
7264 return FALSE;
7266 return TRUE;
7269 void
7270 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7271 arelent *cache_ptr ATTRIBUTE_UNUSED,
7272 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7274 abort ();
7277 /* Try to convert a non-ELF reloc into an ELF one. */
7279 bfd_boolean
7280 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7282 /* Check whether we really have an ELF howto. */
7284 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7286 bfd_reloc_code_real_type code;
7287 reloc_howto_type *howto;
7289 /* Alien reloc: Try to determine its type to replace it with an
7290 equivalent ELF reloc. */
7292 if (areloc->howto->pc_relative)
7294 switch (areloc->howto->bitsize)
7296 case 8:
7297 code = BFD_RELOC_8_PCREL;
7298 break;
7299 case 12:
7300 code = BFD_RELOC_12_PCREL;
7301 break;
7302 case 16:
7303 code = BFD_RELOC_16_PCREL;
7304 break;
7305 case 24:
7306 code = BFD_RELOC_24_PCREL;
7307 break;
7308 case 32:
7309 code = BFD_RELOC_32_PCREL;
7310 break;
7311 case 64:
7312 code = BFD_RELOC_64_PCREL;
7313 break;
7314 default:
7315 goto fail;
7318 howto = bfd_reloc_type_lookup (abfd, code);
7320 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7322 if (howto->pcrel_offset)
7323 areloc->addend += areloc->address;
7324 else
7325 areloc->addend -= areloc->address; /* addend is unsigned!! */
7328 else
7330 switch (areloc->howto->bitsize)
7332 case 8:
7333 code = BFD_RELOC_8;
7334 break;
7335 case 14:
7336 code = BFD_RELOC_14;
7337 break;
7338 case 16:
7339 code = BFD_RELOC_16;
7340 break;
7341 case 26:
7342 code = BFD_RELOC_26;
7343 break;
7344 case 32:
7345 code = BFD_RELOC_32;
7346 break;
7347 case 64:
7348 code = BFD_RELOC_64;
7349 break;
7350 default:
7351 goto fail;
7354 howto = bfd_reloc_type_lookup (abfd, code);
7357 if (howto)
7358 areloc->howto = howto;
7359 else
7360 goto fail;
7363 return TRUE;
7365 fail:
7366 (*_bfd_error_handler)
7367 (_("%B: unsupported relocation type %s"),
7368 abfd, areloc->howto->name);
7369 bfd_set_error (bfd_error_bad_value);
7370 return FALSE;
7373 bfd_boolean
7374 _bfd_elf_close_and_cleanup (bfd *abfd)
7376 if (bfd_get_format (abfd) == bfd_object)
7378 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7379 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7380 _bfd_dwarf2_cleanup_debug_info (abfd);
7383 return _bfd_generic_close_and_cleanup (abfd);
7386 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7387 in the relocation's offset. Thus we cannot allow any sort of sanity
7388 range-checking to interfere. There is nothing else to do in processing
7389 this reloc. */
7391 bfd_reloc_status_type
7392 _bfd_elf_rel_vtable_reloc_fn
7393 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7394 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7395 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7396 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7398 return bfd_reloc_ok;
7401 /* Elf core file support. Much of this only works on native
7402 toolchains, since we rely on knowing the
7403 machine-dependent procfs structure in order to pick
7404 out details about the corefile. */
7406 #ifdef HAVE_SYS_PROCFS_H
7407 # include <sys/procfs.h>
7408 #endif
7410 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7412 static int
7413 elfcore_make_pid (bfd *abfd)
7415 return ((elf_tdata (abfd)->core_lwpid << 16)
7416 + (elf_tdata (abfd)->core_pid));
7419 /* If there isn't a section called NAME, make one, using
7420 data from SECT. Note, this function will generate a
7421 reference to NAME, so you shouldn't deallocate or
7422 overwrite it. */
7424 static bfd_boolean
7425 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7427 asection *sect2;
7429 if (bfd_get_section_by_name (abfd, name) != NULL)
7430 return TRUE;
7432 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7433 if (sect2 == NULL)
7434 return FALSE;
7436 sect2->size = sect->size;
7437 sect2->filepos = sect->filepos;
7438 sect2->alignment_power = sect->alignment_power;
7439 return TRUE;
7442 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7443 actually creates up to two pseudosections:
7444 - For the single-threaded case, a section named NAME, unless
7445 such a section already exists.
7446 - For the multi-threaded case, a section named "NAME/PID", where
7447 PID is elfcore_make_pid (abfd).
7448 Both pseudosections have identical contents. */
7449 bfd_boolean
7450 _bfd_elfcore_make_pseudosection (bfd *abfd,
7451 char *name,
7452 size_t size,
7453 ufile_ptr filepos)
7455 char buf[100];
7456 char *threaded_name;
7457 size_t len;
7458 asection *sect;
7460 /* Build the section name. */
7462 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7463 len = strlen (buf) + 1;
7464 threaded_name = bfd_alloc (abfd, len);
7465 if (threaded_name == NULL)
7466 return FALSE;
7467 memcpy (threaded_name, buf, len);
7469 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7470 SEC_HAS_CONTENTS);
7471 if (sect == NULL)
7472 return FALSE;
7473 sect->size = size;
7474 sect->filepos = filepos;
7475 sect->alignment_power = 2;
7477 return elfcore_maybe_make_sect (abfd, name, sect);
7480 /* prstatus_t exists on:
7481 solaris 2.5+
7482 linux 2.[01] + glibc
7483 unixware 4.2
7486 #if defined (HAVE_PRSTATUS_T)
7488 static bfd_boolean
7489 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7491 size_t size;
7492 int offset;
7494 if (note->descsz == sizeof (prstatus_t))
7496 prstatus_t prstat;
7498 size = sizeof (prstat.pr_reg);
7499 offset = offsetof (prstatus_t, pr_reg);
7500 memcpy (&prstat, note->descdata, sizeof (prstat));
7502 /* Do not overwrite the core signal if it
7503 has already been set by another thread. */
7504 if (elf_tdata (abfd)->core_signal == 0)
7505 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7506 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7508 /* pr_who exists on:
7509 solaris 2.5+
7510 unixware 4.2
7511 pr_who doesn't exist on:
7512 linux 2.[01]
7514 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7515 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7516 #endif
7518 #if defined (HAVE_PRSTATUS32_T)
7519 else if (note->descsz == sizeof (prstatus32_t))
7521 /* 64-bit host, 32-bit corefile */
7522 prstatus32_t prstat;
7524 size = sizeof (prstat.pr_reg);
7525 offset = offsetof (prstatus32_t, pr_reg);
7526 memcpy (&prstat, note->descdata, sizeof (prstat));
7528 /* Do not overwrite the core signal if it
7529 has already been set by another thread. */
7530 if (elf_tdata (abfd)->core_signal == 0)
7531 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7532 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7534 /* pr_who exists on:
7535 solaris 2.5+
7536 unixware 4.2
7537 pr_who doesn't exist on:
7538 linux 2.[01]
7540 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7541 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7542 #endif
7544 #endif /* HAVE_PRSTATUS32_T */
7545 else
7547 /* Fail - we don't know how to handle any other
7548 note size (ie. data object type). */
7549 return TRUE;
7552 /* Make a ".reg/999" section and a ".reg" section. */
7553 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7554 size, note->descpos + offset);
7556 #endif /* defined (HAVE_PRSTATUS_T) */
7558 /* Create a pseudosection containing the exact contents of NOTE. */
7559 static bfd_boolean
7560 elfcore_make_note_pseudosection (bfd *abfd,
7561 char *name,
7562 Elf_Internal_Note *note)
7564 return _bfd_elfcore_make_pseudosection (abfd, name,
7565 note->descsz, note->descpos);
7568 /* There isn't a consistent prfpregset_t across platforms,
7569 but it doesn't matter, because we don't have to pick this
7570 data structure apart. */
7572 static bfd_boolean
7573 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7575 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7578 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7579 type of NT_PRXFPREG. Just include the whole note's contents
7580 literally. */
7582 static bfd_boolean
7583 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7585 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7588 static bfd_boolean
7589 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7591 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7594 static bfd_boolean
7595 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7597 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7600 #if defined (HAVE_PRPSINFO_T)
7601 typedef prpsinfo_t elfcore_psinfo_t;
7602 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7603 typedef prpsinfo32_t elfcore_psinfo32_t;
7604 #endif
7605 #endif
7607 #if defined (HAVE_PSINFO_T)
7608 typedef psinfo_t elfcore_psinfo_t;
7609 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7610 typedef psinfo32_t elfcore_psinfo32_t;
7611 #endif
7612 #endif
7614 /* return a malloc'ed copy of a string at START which is at
7615 most MAX bytes long, possibly without a terminating '\0'.
7616 the copy will always have a terminating '\0'. */
7618 char *
7619 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7621 char *dups;
7622 char *end = memchr (start, '\0', max);
7623 size_t len;
7625 if (end == NULL)
7626 len = max;
7627 else
7628 len = end - start;
7630 dups = bfd_alloc (abfd, len + 1);
7631 if (dups == NULL)
7632 return NULL;
7634 memcpy (dups, start, len);
7635 dups[len] = '\0';
7637 return dups;
7640 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7641 static bfd_boolean
7642 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7644 if (note->descsz == sizeof (elfcore_psinfo_t))
7646 elfcore_psinfo_t psinfo;
7648 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7650 elf_tdata (abfd)->core_program
7651 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7652 sizeof (psinfo.pr_fname));
7654 elf_tdata (abfd)->core_command
7655 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7656 sizeof (psinfo.pr_psargs));
7658 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7659 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7661 /* 64-bit host, 32-bit corefile */
7662 elfcore_psinfo32_t psinfo;
7664 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7666 elf_tdata (abfd)->core_program
7667 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7668 sizeof (psinfo.pr_fname));
7670 elf_tdata (abfd)->core_command
7671 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7672 sizeof (psinfo.pr_psargs));
7674 #endif
7676 else
7678 /* Fail - we don't know how to handle any other
7679 note size (ie. data object type). */
7680 return TRUE;
7683 /* Note that for some reason, a spurious space is tacked
7684 onto the end of the args in some (at least one anyway)
7685 implementations, so strip it off if it exists. */
7688 char *command = elf_tdata (abfd)->core_command;
7689 int n = strlen (command);
7691 if (0 < n && command[n - 1] == ' ')
7692 command[n - 1] = '\0';
7695 return TRUE;
7697 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7699 #if defined (HAVE_PSTATUS_T)
7700 static bfd_boolean
7701 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7703 if (note->descsz == sizeof (pstatus_t)
7704 #if defined (HAVE_PXSTATUS_T)
7705 || note->descsz == sizeof (pxstatus_t)
7706 #endif
7709 pstatus_t pstat;
7711 memcpy (&pstat, note->descdata, sizeof (pstat));
7713 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7715 #if defined (HAVE_PSTATUS32_T)
7716 else if (note->descsz == sizeof (pstatus32_t))
7718 /* 64-bit host, 32-bit corefile */
7719 pstatus32_t pstat;
7721 memcpy (&pstat, note->descdata, sizeof (pstat));
7723 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7725 #endif
7726 /* Could grab some more details from the "representative"
7727 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7728 NT_LWPSTATUS note, presumably. */
7730 return TRUE;
7732 #endif /* defined (HAVE_PSTATUS_T) */
7734 #if defined (HAVE_LWPSTATUS_T)
7735 static bfd_boolean
7736 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7738 lwpstatus_t lwpstat;
7739 char buf[100];
7740 char *name;
7741 size_t len;
7742 asection *sect;
7744 if (note->descsz != sizeof (lwpstat)
7745 #if defined (HAVE_LWPXSTATUS_T)
7746 && note->descsz != sizeof (lwpxstatus_t)
7747 #endif
7749 return TRUE;
7751 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7753 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7754 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7756 /* Make a ".reg/999" section. */
7758 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7759 len = strlen (buf) + 1;
7760 name = bfd_alloc (abfd, len);
7761 if (name == NULL)
7762 return FALSE;
7763 memcpy (name, buf, len);
7765 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7766 if (sect == NULL)
7767 return FALSE;
7769 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7770 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7771 sect->filepos = note->descpos
7772 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7773 #endif
7775 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7776 sect->size = sizeof (lwpstat.pr_reg);
7777 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7778 #endif
7780 sect->alignment_power = 2;
7782 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7783 return FALSE;
7785 /* Make a ".reg2/999" section */
7787 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7788 len = strlen (buf) + 1;
7789 name = bfd_alloc (abfd, len);
7790 if (name == NULL)
7791 return FALSE;
7792 memcpy (name, buf, len);
7794 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7795 if (sect == NULL)
7796 return FALSE;
7798 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7799 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7800 sect->filepos = note->descpos
7801 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7802 #endif
7804 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7805 sect->size = sizeof (lwpstat.pr_fpreg);
7806 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7807 #endif
7809 sect->alignment_power = 2;
7811 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7813 #endif /* defined (HAVE_LWPSTATUS_T) */
7815 static bfd_boolean
7816 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7818 char buf[30];
7819 char *name;
7820 size_t len;
7821 asection *sect;
7822 int type;
7823 int is_active_thread;
7824 bfd_vma base_addr;
7826 if (note->descsz < 728)
7827 return TRUE;
7829 if (! CONST_STRNEQ (note->namedata, "win32"))
7830 return TRUE;
7832 type = bfd_get_32 (abfd, note->descdata);
7834 switch (type)
7836 case 1 /* NOTE_INFO_PROCESS */:
7837 /* FIXME: need to add ->core_command. */
7838 /* process_info.pid */
7839 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7840 /* process_info.signal */
7841 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7842 break;
7844 case 2 /* NOTE_INFO_THREAD */:
7845 /* Make a ".reg/999" section. */
7846 /* thread_info.tid */
7847 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7849 len = strlen (buf) + 1;
7850 name = bfd_alloc (abfd, len);
7851 if (name == NULL)
7852 return FALSE;
7854 memcpy (name, buf, len);
7856 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7857 if (sect == NULL)
7858 return FALSE;
7860 /* sizeof (thread_info.thread_context) */
7861 sect->size = 716;
7862 /* offsetof (thread_info.thread_context) */
7863 sect->filepos = note->descpos + 12;
7864 sect->alignment_power = 2;
7866 /* thread_info.is_active_thread */
7867 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7869 if (is_active_thread)
7870 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7871 return FALSE;
7872 break;
7874 case 3 /* NOTE_INFO_MODULE */:
7875 /* Make a ".module/xxxxxxxx" section. */
7876 /* module_info.base_address */
7877 base_addr = bfd_get_32 (abfd, note->descdata + 4);
7878 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
7880 len = strlen (buf) + 1;
7881 name = bfd_alloc (abfd, len);
7882 if (name == NULL)
7883 return FALSE;
7885 memcpy (name, buf, len);
7887 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7889 if (sect == NULL)
7890 return FALSE;
7892 sect->size = note->descsz;
7893 sect->filepos = note->descpos;
7894 sect->alignment_power = 2;
7895 break;
7897 default:
7898 return TRUE;
7901 return TRUE;
7904 static bfd_boolean
7905 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7907 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7909 switch (note->type)
7911 default:
7912 return TRUE;
7914 case NT_PRSTATUS:
7915 if (bed->elf_backend_grok_prstatus)
7916 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7917 return TRUE;
7918 #if defined (HAVE_PRSTATUS_T)
7919 return elfcore_grok_prstatus (abfd, note);
7920 #else
7921 return TRUE;
7922 #endif
7924 #if defined (HAVE_PSTATUS_T)
7925 case NT_PSTATUS:
7926 return elfcore_grok_pstatus (abfd, note);
7927 #endif
7929 #if defined (HAVE_LWPSTATUS_T)
7930 case NT_LWPSTATUS:
7931 return elfcore_grok_lwpstatus (abfd, note);
7932 #endif
7934 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7935 return elfcore_grok_prfpreg (abfd, note);
7937 case NT_WIN32PSTATUS:
7938 return elfcore_grok_win32pstatus (abfd, note);
7940 case NT_PRXFPREG: /* Linux SSE extension */
7941 if (note->namesz == 6
7942 && strcmp (note->namedata, "LINUX") == 0)
7943 return elfcore_grok_prxfpreg (abfd, note);
7944 else
7945 return TRUE;
7947 case NT_PPC_VMX:
7948 if (note->namesz == 6
7949 && strcmp (note->namedata, "LINUX") == 0)
7950 return elfcore_grok_ppc_vmx (abfd, note);
7951 else
7952 return TRUE;
7954 case NT_PPC_VSX:
7955 if (note->namesz == 6
7956 && strcmp (note->namedata, "LINUX") == 0)
7957 return elfcore_grok_ppc_vsx (abfd, note);
7958 else
7959 return TRUE;
7961 case NT_PRPSINFO:
7962 case NT_PSINFO:
7963 if (bed->elf_backend_grok_psinfo)
7964 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7965 return TRUE;
7966 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7967 return elfcore_grok_psinfo (abfd, note);
7968 #else
7969 return TRUE;
7970 #endif
7972 case NT_AUXV:
7974 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7975 SEC_HAS_CONTENTS);
7977 if (sect == NULL)
7978 return FALSE;
7979 sect->size = note->descsz;
7980 sect->filepos = note->descpos;
7981 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7983 return TRUE;
7988 static bfd_boolean
7989 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
7991 elf_tdata (abfd)->build_id_size = note->descsz;
7992 elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz);
7993 if (elf_tdata (abfd)->build_id == NULL)
7994 return FALSE;
7996 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
7998 return TRUE;
8001 static bfd_boolean
8002 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8004 switch (note->type)
8006 default:
8007 return TRUE;
8009 case NT_GNU_BUILD_ID:
8010 return elfobj_grok_gnu_build_id (abfd, note);
8014 static bfd_boolean
8015 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8017 char *cp;
8019 cp = strchr (note->namedata, '@');
8020 if (cp != NULL)
8022 *lwpidp = atoi(cp + 1);
8023 return TRUE;
8025 return FALSE;
8028 static bfd_boolean
8029 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8031 /* Signal number at offset 0x08. */
8032 elf_tdata (abfd)->core_signal
8033 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8035 /* Process ID at offset 0x50. */
8036 elf_tdata (abfd)->core_pid
8037 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8039 /* Command name at 0x7c (max 32 bytes, including nul). */
8040 elf_tdata (abfd)->core_command
8041 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8043 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8044 note);
8047 static bfd_boolean
8048 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8050 int lwp;
8052 if (elfcore_netbsd_get_lwpid (note, &lwp))
8053 elf_tdata (abfd)->core_lwpid = lwp;
8055 if (note->type == NT_NETBSDCORE_PROCINFO)
8057 /* NetBSD-specific core "procinfo". Note that we expect to
8058 find this note before any of the others, which is fine,
8059 since the kernel writes this note out first when it
8060 creates a core file. */
8062 return elfcore_grok_netbsd_procinfo (abfd, note);
8065 /* As of Jan 2002 there are no other machine-independent notes
8066 defined for NetBSD core files. If the note type is less
8067 than the start of the machine-dependent note types, we don't
8068 understand it. */
8070 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8071 return TRUE;
8074 switch (bfd_get_arch (abfd))
8076 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8077 PT_GETFPREGS == mach+2. */
8079 case bfd_arch_alpha:
8080 case bfd_arch_sparc:
8081 switch (note->type)
8083 case NT_NETBSDCORE_FIRSTMACH+0:
8084 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8086 case NT_NETBSDCORE_FIRSTMACH+2:
8087 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8089 default:
8090 return TRUE;
8093 /* On all other arch's, PT_GETREGS == mach+1 and
8094 PT_GETFPREGS == mach+3. */
8096 default:
8097 switch (note->type)
8099 case NT_NETBSDCORE_FIRSTMACH+1:
8100 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8102 case NT_NETBSDCORE_FIRSTMACH+3:
8103 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8105 default:
8106 return TRUE;
8109 /* NOTREACHED */
8112 static bfd_boolean
8113 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8115 /* Signal number at offset 0x08. */
8116 elf_tdata (abfd)->core_signal
8117 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8119 /* Process ID at offset 0x20. */
8120 elf_tdata (abfd)->core_pid
8121 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8123 /* Command name at 0x48 (max 32 bytes, including nul). */
8124 elf_tdata (abfd)->core_command
8125 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8127 return TRUE;
8130 static bfd_boolean
8131 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8133 if (note->type == NT_OPENBSD_PROCINFO)
8134 return elfcore_grok_openbsd_procinfo (abfd, note);
8136 if (note->type == NT_OPENBSD_REGS)
8137 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8139 if (note->type == NT_OPENBSD_FPREGS)
8140 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8142 if (note->type == NT_OPENBSD_XFPREGS)
8143 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8145 if (note->type == NT_OPENBSD_AUXV)
8147 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8148 SEC_HAS_CONTENTS);
8150 if (sect == NULL)
8151 return FALSE;
8152 sect->size = note->descsz;
8153 sect->filepos = note->descpos;
8154 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8156 return TRUE;
8159 if (note->type == NT_OPENBSD_WCOOKIE)
8161 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8162 SEC_HAS_CONTENTS);
8164 if (sect == NULL)
8165 return FALSE;
8166 sect->size = note->descsz;
8167 sect->filepos = note->descpos;
8168 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8170 return TRUE;
8173 return TRUE;
8176 static bfd_boolean
8177 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8179 void *ddata = note->descdata;
8180 char buf[100];
8181 char *name;
8182 asection *sect;
8183 short sig;
8184 unsigned flags;
8186 /* nto_procfs_status 'pid' field is at offset 0. */
8187 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8189 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8190 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8192 /* nto_procfs_status 'flags' field is at offset 8. */
8193 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8195 /* nto_procfs_status 'what' field is at offset 14. */
8196 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8198 elf_tdata (abfd)->core_signal = sig;
8199 elf_tdata (abfd)->core_lwpid = *tid;
8202 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8203 do not come from signals so we make sure we set the current
8204 thread just in case. */
8205 if (flags & 0x00000080)
8206 elf_tdata (abfd)->core_lwpid = *tid;
8208 /* Make a ".qnx_core_status/%d" section. */
8209 sprintf (buf, ".qnx_core_status/%ld", *tid);
8211 name = bfd_alloc (abfd, strlen (buf) + 1);
8212 if (name == NULL)
8213 return FALSE;
8214 strcpy (name, buf);
8216 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8217 if (sect == NULL)
8218 return FALSE;
8220 sect->size = note->descsz;
8221 sect->filepos = note->descpos;
8222 sect->alignment_power = 2;
8224 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8227 static bfd_boolean
8228 elfcore_grok_nto_regs (bfd *abfd,
8229 Elf_Internal_Note *note,
8230 long tid,
8231 char *base)
8233 char buf[100];
8234 char *name;
8235 asection *sect;
8237 /* Make a "(base)/%d" section. */
8238 sprintf (buf, "%s/%ld", base, tid);
8240 name = bfd_alloc (abfd, strlen (buf) + 1);
8241 if (name == NULL)
8242 return FALSE;
8243 strcpy (name, buf);
8245 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8246 if (sect == NULL)
8247 return FALSE;
8249 sect->size = note->descsz;
8250 sect->filepos = note->descpos;
8251 sect->alignment_power = 2;
8253 /* This is the current thread. */
8254 if (elf_tdata (abfd)->core_lwpid == tid)
8255 return elfcore_maybe_make_sect (abfd, base, sect);
8257 return TRUE;
8260 #define BFD_QNT_CORE_INFO 7
8261 #define BFD_QNT_CORE_STATUS 8
8262 #define BFD_QNT_CORE_GREG 9
8263 #define BFD_QNT_CORE_FPREG 10
8265 static bfd_boolean
8266 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8268 /* Every GREG section has a STATUS section before it. Store the
8269 tid from the previous call to pass down to the next gregs
8270 function. */
8271 static long tid = 1;
8273 switch (note->type)
8275 case BFD_QNT_CORE_INFO:
8276 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8277 case BFD_QNT_CORE_STATUS:
8278 return elfcore_grok_nto_status (abfd, note, &tid);
8279 case BFD_QNT_CORE_GREG:
8280 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8281 case BFD_QNT_CORE_FPREG:
8282 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8283 default:
8284 return TRUE;
8288 static bfd_boolean
8289 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8291 char *name;
8292 asection *sect;
8293 size_t len;
8295 /* Use note name as section name. */
8296 len = note->namesz;
8297 name = bfd_alloc (abfd, len);
8298 if (name == NULL)
8299 return FALSE;
8300 memcpy (name, note->namedata, len);
8301 name[len - 1] = '\0';
8303 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8304 if (sect == NULL)
8305 return FALSE;
8307 sect->size = note->descsz;
8308 sect->filepos = note->descpos;
8309 sect->alignment_power = 1;
8311 return TRUE;
8314 /* Function: elfcore_write_note
8316 Inputs:
8317 buffer to hold note, and current size of buffer
8318 name of note
8319 type of note
8320 data for note
8321 size of data for note
8323 Writes note to end of buffer. ELF64 notes are written exactly as
8324 for ELF32, despite the current (as of 2006) ELF gabi specifying
8325 that they ought to have 8-byte namesz and descsz field, and have
8326 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8328 Return:
8329 Pointer to realloc'd buffer, *BUFSIZ updated. */
8331 char *
8332 elfcore_write_note (bfd *abfd,
8333 char *buf,
8334 int *bufsiz,
8335 const char *name,
8336 int type,
8337 const void *input,
8338 int size)
8340 Elf_External_Note *xnp;
8341 size_t namesz;
8342 size_t newspace;
8343 char *dest;
8345 namesz = 0;
8346 if (name != NULL)
8347 namesz = strlen (name) + 1;
8349 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8351 buf = realloc (buf, *bufsiz + newspace);
8352 if (buf == NULL)
8353 return buf;
8354 dest = buf + *bufsiz;
8355 *bufsiz += newspace;
8356 xnp = (Elf_External_Note *) dest;
8357 H_PUT_32 (abfd, namesz, xnp->namesz);
8358 H_PUT_32 (abfd, size, xnp->descsz);
8359 H_PUT_32 (abfd, type, xnp->type);
8360 dest = xnp->name;
8361 if (name != NULL)
8363 memcpy (dest, name, namesz);
8364 dest += namesz;
8365 while (namesz & 3)
8367 *dest++ = '\0';
8368 ++namesz;
8371 memcpy (dest, input, size);
8372 dest += size;
8373 while (size & 3)
8375 *dest++ = '\0';
8376 ++size;
8378 return buf;
8381 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8382 char *
8383 elfcore_write_prpsinfo (bfd *abfd,
8384 char *buf,
8385 int *bufsiz,
8386 const char *fname,
8387 const char *psargs)
8389 const char *note_name = "CORE";
8390 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8392 if (bed->elf_backend_write_core_note != NULL)
8394 char *ret;
8395 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8396 NT_PRPSINFO, fname, psargs);
8397 if (ret != NULL)
8398 return ret;
8401 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8402 if (bed->s->elfclass == ELFCLASS32)
8404 #if defined (HAVE_PSINFO32_T)
8405 psinfo32_t data;
8406 int note_type = NT_PSINFO;
8407 #else
8408 prpsinfo32_t data;
8409 int note_type = NT_PRPSINFO;
8410 #endif
8412 memset (&data, 0, sizeof (data));
8413 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8414 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8415 return elfcore_write_note (abfd, buf, bufsiz,
8416 note_name, note_type, &data, sizeof (data));
8418 else
8419 #endif
8421 #if defined (HAVE_PSINFO_T)
8422 psinfo_t data;
8423 int note_type = NT_PSINFO;
8424 #else
8425 prpsinfo_t data;
8426 int note_type = NT_PRPSINFO;
8427 #endif
8429 memset (&data, 0, sizeof (data));
8430 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8431 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8432 return elfcore_write_note (abfd, buf, bufsiz,
8433 note_name, note_type, &data, sizeof (data));
8436 #endif /* PSINFO_T or PRPSINFO_T */
8438 #if defined (HAVE_PRSTATUS_T)
8439 char *
8440 elfcore_write_prstatus (bfd *abfd,
8441 char *buf,
8442 int *bufsiz,
8443 long pid,
8444 int cursig,
8445 const void *gregs)
8447 const char *note_name = "CORE";
8448 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8450 if (bed->elf_backend_write_core_note != NULL)
8452 char *ret;
8453 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8454 NT_PRSTATUS,
8455 pid, cursig, gregs);
8456 if (ret != NULL)
8457 return ret;
8460 #if defined (HAVE_PRSTATUS32_T)
8461 if (bed->s->elfclass == ELFCLASS32)
8463 prstatus32_t prstat;
8465 memset (&prstat, 0, sizeof (prstat));
8466 prstat.pr_pid = pid;
8467 prstat.pr_cursig = cursig;
8468 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8469 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8470 NT_PRSTATUS, &prstat, sizeof (prstat));
8472 else
8473 #endif
8475 prstatus_t prstat;
8477 memset (&prstat, 0, sizeof (prstat));
8478 prstat.pr_pid = pid;
8479 prstat.pr_cursig = cursig;
8480 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8481 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8482 NT_PRSTATUS, &prstat, sizeof (prstat));
8485 #endif /* HAVE_PRSTATUS_T */
8487 #if defined (HAVE_LWPSTATUS_T)
8488 char *
8489 elfcore_write_lwpstatus (bfd *abfd,
8490 char *buf,
8491 int *bufsiz,
8492 long pid,
8493 int cursig,
8494 const void *gregs)
8496 lwpstatus_t lwpstat;
8497 const char *note_name = "CORE";
8499 memset (&lwpstat, 0, sizeof (lwpstat));
8500 lwpstat.pr_lwpid = pid >> 16;
8501 lwpstat.pr_cursig = cursig;
8502 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8503 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8504 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8505 #if !defined(gregs)
8506 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8507 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8508 #else
8509 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8510 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8511 #endif
8512 #endif
8513 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8514 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8516 #endif /* HAVE_LWPSTATUS_T */
8518 #if defined (HAVE_PSTATUS_T)
8519 char *
8520 elfcore_write_pstatus (bfd *abfd,
8521 char *buf,
8522 int *bufsiz,
8523 long pid,
8524 int cursig ATTRIBUTE_UNUSED,
8525 const void *gregs ATTRIBUTE_UNUSED)
8527 const char *note_name = "CORE";
8528 #if defined (HAVE_PSTATUS32_T)
8529 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8531 if (bed->s->elfclass == ELFCLASS32)
8533 pstatus32_t pstat;
8535 memset (&pstat, 0, sizeof (pstat));
8536 pstat.pr_pid = pid & 0xffff;
8537 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8538 NT_PSTATUS, &pstat, sizeof (pstat));
8539 return buf;
8541 else
8542 #endif
8544 pstatus_t pstat;
8546 memset (&pstat, 0, sizeof (pstat));
8547 pstat.pr_pid = pid & 0xffff;
8548 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8549 NT_PSTATUS, &pstat, sizeof (pstat));
8550 return buf;
8553 #endif /* HAVE_PSTATUS_T */
8555 char *
8556 elfcore_write_prfpreg (bfd *abfd,
8557 char *buf,
8558 int *bufsiz,
8559 const void *fpregs,
8560 int size)
8562 const char *note_name = "CORE";
8563 return elfcore_write_note (abfd, buf, bufsiz,
8564 note_name, NT_FPREGSET, fpregs, size);
8567 char *
8568 elfcore_write_prxfpreg (bfd *abfd,
8569 char *buf,
8570 int *bufsiz,
8571 const void *xfpregs,
8572 int size)
8574 char *note_name = "LINUX";
8575 return elfcore_write_note (abfd, buf, bufsiz,
8576 note_name, NT_PRXFPREG, xfpregs, size);
8579 char *
8580 elfcore_write_ppc_vmx (bfd *abfd,
8581 char *buf,
8582 int *bufsiz,
8583 const void *ppc_vmx,
8584 int size)
8586 char *note_name = "LINUX";
8587 return elfcore_write_note (abfd, buf, bufsiz,
8588 note_name, NT_PPC_VMX, ppc_vmx, size);
8591 char *
8592 elfcore_write_ppc_vsx (bfd *abfd,
8593 char *buf,
8594 int *bufsiz,
8595 const void *ppc_vsx,
8596 int size)
8598 char *note_name = "LINUX";
8599 return elfcore_write_note (abfd, buf, bufsiz,
8600 note_name, NT_PPC_VSX, ppc_vsx, size);
8603 char *
8604 elfcore_write_register_note (bfd *abfd,
8605 char *buf,
8606 int *bufsiz,
8607 const char *section,
8608 const void *data,
8609 int size)
8611 if (strcmp (section, ".reg2") == 0)
8612 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8613 if (strcmp (section, ".reg-xfp") == 0)
8614 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8615 if (strcmp (section, ".reg-ppc-vmx") == 0)
8616 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8617 if (strcmp (section, ".reg-ppc-vsx") == 0)
8618 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
8619 return NULL;
8622 static bfd_boolean
8623 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8625 char *p;
8627 p = buf;
8628 while (p < buf + size)
8630 /* FIXME: bad alignment assumption. */
8631 Elf_External_Note *xnp = (Elf_External_Note *) p;
8632 Elf_Internal_Note in;
8634 if (offsetof (Elf_External_Note, name) > buf - p + size)
8635 return FALSE;
8637 in.type = H_GET_32 (abfd, xnp->type);
8639 in.namesz = H_GET_32 (abfd, xnp->namesz);
8640 in.namedata = xnp->name;
8641 if (in.namesz > buf - in.namedata + size)
8642 return FALSE;
8644 in.descsz = H_GET_32 (abfd, xnp->descsz);
8645 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8646 in.descpos = offset + (in.descdata - buf);
8647 if (in.descsz != 0
8648 && (in.descdata >= buf + size
8649 || in.descsz > buf - in.descdata + size))
8650 return FALSE;
8652 switch (bfd_get_format (abfd))
8654 default:
8655 return TRUE;
8657 case bfd_core:
8658 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8660 if (! elfcore_grok_netbsd_note (abfd, &in))
8661 return FALSE;
8663 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
8665 if (! elfcore_grok_openbsd_note (abfd, &in))
8666 return FALSE;
8668 else if (CONST_STRNEQ (in.namedata, "QNX"))
8670 if (! elfcore_grok_nto_note (abfd, &in))
8671 return FALSE;
8673 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8675 if (! elfcore_grok_spu_note (abfd, &in))
8676 return FALSE;
8678 else
8680 if (! elfcore_grok_note (abfd, &in))
8681 return FALSE;
8683 break;
8685 case bfd_object:
8686 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8688 if (! elfobj_grok_gnu_note (abfd, &in))
8689 return FALSE;
8691 break;
8694 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8697 return TRUE;
8700 static bfd_boolean
8701 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8703 char *buf;
8705 if (size <= 0)
8706 return TRUE;
8708 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8709 return FALSE;
8711 buf = bfd_malloc (size);
8712 if (buf == NULL)
8713 return FALSE;
8715 if (bfd_bread (buf, size, abfd) != size
8716 || !elf_parse_notes (abfd, buf, size, offset))
8718 free (buf);
8719 return FALSE;
8722 free (buf);
8723 return TRUE;
8726 /* Providing external access to the ELF program header table. */
8728 /* Return an upper bound on the number of bytes required to store a
8729 copy of ABFD's program header table entries. Return -1 if an error
8730 occurs; bfd_get_error will return an appropriate code. */
8732 long
8733 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8735 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8737 bfd_set_error (bfd_error_wrong_format);
8738 return -1;
8741 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8744 /* Copy ABFD's program header table entries to *PHDRS. The entries
8745 will be stored as an array of Elf_Internal_Phdr structures, as
8746 defined in include/elf/internal.h. To find out how large the
8747 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8749 Return the number of program header table entries read, or -1 if an
8750 error occurs; bfd_get_error will return an appropriate code. */
8753 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8755 int num_phdrs;
8757 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8759 bfd_set_error (bfd_error_wrong_format);
8760 return -1;
8763 num_phdrs = elf_elfheader (abfd)->e_phnum;
8764 memcpy (phdrs, elf_tdata (abfd)->phdr,
8765 num_phdrs * sizeof (Elf_Internal_Phdr));
8767 return num_phdrs;
8770 enum elf_reloc_type_class
8771 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8773 return reloc_class_normal;
8776 /* For RELA architectures, return the relocation value for a
8777 relocation against a local symbol. */
8779 bfd_vma
8780 _bfd_elf_rela_local_sym (bfd *abfd,
8781 Elf_Internal_Sym *sym,
8782 asection **psec,
8783 Elf_Internal_Rela *rel)
8785 asection *sec = *psec;
8786 bfd_vma relocation;
8788 relocation = (sec->output_section->vma
8789 + sec->output_offset
8790 + sym->st_value);
8791 if ((sec->flags & SEC_MERGE)
8792 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8793 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8795 rel->r_addend =
8796 _bfd_merged_section_offset (abfd, psec,
8797 elf_section_data (sec)->sec_info,
8798 sym->st_value + rel->r_addend);
8799 if (sec != *psec)
8801 /* If we have changed the section, and our original section is
8802 marked with SEC_EXCLUDE, it means that the original
8803 SEC_MERGE section has been completely subsumed in some
8804 other SEC_MERGE section. In this case, we need to leave
8805 some info around for --emit-relocs. */
8806 if ((sec->flags & SEC_EXCLUDE) != 0)
8807 sec->kept_section = *psec;
8808 sec = *psec;
8810 rel->r_addend -= relocation;
8811 rel->r_addend += sec->output_section->vma + sec->output_offset;
8813 return relocation;
8816 bfd_vma
8817 _bfd_elf_rel_local_sym (bfd *abfd,
8818 Elf_Internal_Sym *sym,
8819 asection **psec,
8820 bfd_vma addend)
8822 asection *sec = *psec;
8824 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8825 return sym->st_value + addend;
8827 return _bfd_merged_section_offset (abfd, psec,
8828 elf_section_data (sec)->sec_info,
8829 sym->st_value + addend);
8832 bfd_vma
8833 _bfd_elf_section_offset (bfd *abfd,
8834 struct bfd_link_info *info,
8835 asection *sec,
8836 bfd_vma offset)
8838 switch (sec->sec_info_type)
8840 case ELF_INFO_TYPE_STABS:
8841 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8842 offset);
8843 case ELF_INFO_TYPE_EH_FRAME:
8844 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8845 default:
8846 return offset;
8850 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8851 reconstruct an ELF file by reading the segments out of remote memory
8852 based on the ELF file header at EHDR_VMA and the ELF program headers it
8853 points to. If not null, *LOADBASEP is filled in with the difference
8854 between the VMAs from which the segments were read, and the VMAs the
8855 file headers (and hence BFD's idea of each section's VMA) put them at.
8857 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8858 remote memory at target address VMA into the local buffer at MYADDR; it
8859 should return zero on success or an `errno' code on failure. TEMPL must
8860 be a BFD for an ELF target with the word size and byte order found in
8861 the remote memory. */
8863 bfd *
8864 bfd_elf_bfd_from_remote_memory
8865 (bfd *templ,
8866 bfd_vma ehdr_vma,
8867 bfd_vma *loadbasep,
8868 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8870 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8871 (templ, ehdr_vma, loadbasep, target_read_memory);
8874 long
8875 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8876 long symcount ATTRIBUTE_UNUSED,
8877 asymbol **syms ATTRIBUTE_UNUSED,
8878 long dynsymcount,
8879 asymbol **dynsyms,
8880 asymbol **ret)
8882 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8883 asection *relplt;
8884 asymbol *s;
8885 const char *relplt_name;
8886 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8887 arelent *p;
8888 long count, i, n;
8889 size_t size;
8890 Elf_Internal_Shdr *hdr;
8891 char *names;
8892 asection *plt;
8894 *ret = NULL;
8896 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8897 return 0;
8899 if (dynsymcount <= 0)
8900 return 0;
8902 if (!bed->plt_sym_val)
8903 return 0;
8905 relplt_name = bed->relplt_name;
8906 if (relplt_name == NULL)
8907 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
8908 relplt = bfd_get_section_by_name (abfd, relplt_name);
8909 if (relplt == NULL)
8910 return 0;
8912 hdr = &elf_section_data (relplt)->this_hdr;
8913 if (hdr->sh_link != elf_dynsymtab (abfd)
8914 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8915 return 0;
8917 plt = bfd_get_section_by_name (abfd, ".plt");
8918 if (plt == NULL)
8919 return 0;
8921 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8922 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8923 return -1;
8925 count = relplt->size / hdr->sh_entsize;
8926 size = count * sizeof (asymbol);
8927 p = relplt->relocation;
8928 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8929 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8931 s = *ret = bfd_malloc (size);
8932 if (s == NULL)
8933 return -1;
8935 names = (char *) (s + count);
8936 p = relplt->relocation;
8937 n = 0;
8938 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8940 size_t len;
8941 bfd_vma addr;
8943 addr = bed->plt_sym_val (i, plt, p);
8944 if (addr == (bfd_vma) -1)
8945 continue;
8947 *s = **p->sym_ptr_ptr;
8948 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8949 we are defining a symbol, ensure one of them is set. */
8950 if ((s->flags & BSF_LOCAL) == 0)
8951 s->flags |= BSF_GLOBAL;
8952 s->flags |= BSF_SYNTHETIC;
8953 s->section = plt;
8954 s->value = addr - plt->vma;
8955 s->name = names;
8956 s->udata.p = NULL;
8957 len = strlen ((*p->sym_ptr_ptr)->name);
8958 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8959 names += len;
8960 memcpy (names, "@plt", sizeof ("@plt"));
8961 names += sizeof ("@plt");
8962 ++s, ++n;
8965 return n;
8968 /* It is only used by x86-64 so far. */
8969 asection _bfd_elf_large_com_section
8970 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
8971 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
8973 void
8974 _bfd_elf_set_osabi (bfd * abfd,
8975 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8977 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8979 i_ehdrp = elf_elfheader (abfd);
8981 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8983 /* To make things simpler for the loader on Linux systems we set the
8984 osabi field to ELFOSABI_LINUX if the binary contains symbols of
8985 the STT_GNU_IFUNC type. */
8986 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
8987 && elf_tdata (abfd)->has_ifunc_symbols)
8988 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
8992 /* Return TRUE for ELF symbol types that represent functions.
8993 This is the default version of this function, which is sufficient for
8994 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
8996 bfd_boolean
8997 _bfd_elf_is_function_type (unsigned int type)
8999 return (type == STT_FUNC
9000 || type == STT_GNU_IFUNC);