2009-12-04 Rafael Avila de Espindola <espindola@google.com>
[binutils.git] / bfd / elf.c
blob4a329294ce7c1645bbb4a881359cdd6b99316d9f
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_byte *) 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 NULL;
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 = (Elf_External_Sym_Shndx *)
411 bfd_malloc2 (symcount, 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 = (Elf_Internal_Sym *)
426 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
427 intsym_buf = alloc_intsym;
428 if (intsym_buf == NULL)
429 goto out;
432 /* Convert the symbols to internal form. */
433 isymend = intsym_buf + symcount;
434 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
435 shndx = extshndx_buf;
436 isym < isymend;
437 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
438 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
440 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
441 (*_bfd_error_handler) (_("%B symbol number %lu references "
442 "nonexistent SHT_SYMTAB_SHNDX section"),
443 ibfd, (unsigned long) symoffset);
444 if (alloc_intsym != NULL)
445 free (alloc_intsym);
446 intsym_buf = NULL;
447 goto out;
450 out:
451 if (alloc_ext != NULL)
452 free (alloc_ext);
453 if (alloc_extshndx != NULL)
454 free (alloc_extshndx);
456 return intsym_buf;
459 /* Look up a symbol name. */
460 const char *
461 bfd_elf_sym_name (bfd *abfd,
462 Elf_Internal_Shdr *symtab_hdr,
463 Elf_Internal_Sym *isym,
464 asection *sym_sec)
466 const char *name;
467 unsigned int iname = isym->st_name;
468 unsigned int shindex = symtab_hdr->sh_link;
470 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
471 /* Check for a bogus st_shndx to avoid crashing. */
472 && isym->st_shndx < elf_numsections (abfd))
474 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
475 shindex = elf_elfheader (abfd)->e_shstrndx;
478 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
479 if (name == NULL)
480 name = "(null)";
481 else if (sym_sec && *name == '\0')
482 name = bfd_section_name (abfd, sym_sec);
484 return name;
487 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
488 sections. The first element is the flags, the rest are section
489 pointers. */
491 typedef union elf_internal_group {
492 Elf_Internal_Shdr *shdr;
493 unsigned int flags;
494 } Elf_Internal_Group;
496 /* Return the name of the group signature symbol. Why isn't the
497 signature just a string? */
499 static const char *
500 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
502 Elf_Internal_Shdr *hdr;
503 unsigned char esym[sizeof (Elf64_External_Sym)];
504 Elf_External_Sym_Shndx eshndx;
505 Elf_Internal_Sym isym;
507 /* First we need to ensure the symbol table is available. Make sure
508 that it is a symbol table section. */
509 if (ghdr->sh_link >= elf_numsections (abfd))
510 return NULL;
511 hdr = elf_elfsections (abfd) [ghdr->sh_link];
512 if (hdr->sh_type != SHT_SYMTAB
513 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
514 return NULL;
516 /* Go read the symbol. */
517 hdr = &elf_tdata (abfd)->symtab_hdr;
518 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
519 &isym, esym, &eshndx) == NULL)
520 return NULL;
522 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
525 /* Set next_in_group list pointer, and group name for NEWSECT. */
527 static bfd_boolean
528 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
530 unsigned int num_group = elf_tdata (abfd)->num_group;
532 /* If num_group is zero, read in all SHT_GROUP sections. The count
533 is set to -1 if there are no SHT_GROUP sections. */
534 if (num_group == 0)
536 unsigned int i, shnum;
538 /* First count the number of groups. If we have a SHT_GROUP
539 section with just a flag word (ie. sh_size is 4), ignore it. */
540 shnum = elf_numsections (abfd);
541 num_group = 0;
543 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
544 ( (shdr)->sh_type == SHT_GROUP \
545 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
546 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
547 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
549 for (i = 0; i < shnum; i++)
551 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
553 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
554 num_group += 1;
557 if (num_group == 0)
559 num_group = (unsigned) -1;
560 elf_tdata (abfd)->num_group = num_group;
562 else
564 /* We keep a list of elf section headers for group sections,
565 so we can find them quickly. */
566 bfd_size_type amt;
568 elf_tdata (abfd)->num_group = num_group;
569 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
570 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
571 if (elf_tdata (abfd)->group_sect_ptr == NULL)
572 return FALSE;
574 num_group = 0;
575 for (i = 0; i < shnum; i++)
577 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
579 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
581 unsigned char *src;
582 Elf_Internal_Group *dest;
584 /* Add to list of sections. */
585 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
586 num_group += 1;
588 /* Read the raw contents. */
589 BFD_ASSERT (sizeof (*dest) >= 4);
590 amt = shdr->sh_size * sizeof (*dest) / 4;
591 shdr->contents = (unsigned char *)
592 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
593 /* PR binutils/4110: Handle corrupt group headers. */
594 if (shdr->contents == NULL)
596 _bfd_error_handler
597 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
598 bfd_set_error (bfd_error_bad_value);
599 return FALSE;
602 memset (shdr->contents, 0, amt);
604 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
605 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
606 != shdr->sh_size))
607 return FALSE;
609 /* Translate raw contents, a flag word followed by an
610 array of elf section indices all in target byte order,
611 to the flag word followed by an array of elf section
612 pointers. */
613 src = shdr->contents + shdr->sh_size;
614 dest = (Elf_Internal_Group *) (shdr->contents + amt);
615 while (1)
617 unsigned int idx;
619 src -= 4;
620 --dest;
621 idx = H_GET_32 (abfd, src);
622 if (src == shdr->contents)
624 dest->flags = idx;
625 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
626 shdr->bfd_section->flags
627 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
628 break;
630 if (idx >= shnum)
632 ((*_bfd_error_handler)
633 (_("%B: invalid SHT_GROUP entry"), abfd));
634 idx = 0;
636 dest->shdr = elf_elfsections (abfd)[idx];
643 if (num_group != (unsigned) -1)
645 unsigned int i;
647 for (i = 0; i < num_group; i++)
649 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
650 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
651 unsigned int n_elt = shdr->sh_size / 4;
653 /* Look through this group's sections to see if current
654 section is a member. */
655 while (--n_elt != 0)
656 if ((++idx)->shdr == hdr)
658 asection *s = NULL;
660 /* We are a member of this group. Go looking through
661 other members to see if any others are linked via
662 next_in_group. */
663 idx = (Elf_Internal_Group *) shdr->contents;
664 n_elt = shdr->sh_size / 4;
665 while (--n_elt != 0)
666 if ((s = (++idx)->shdr->bfd_section) != NULL
667 && elf_next_in_group (s) != NULL)
668 break;
669 if (n_elt != 0)
671 /* Snarf the group name from other member, and
672 insert current section in circular list. */
673 elf_group_name (newsect) = elf_group_name (s);
674 elf_next_in_group (newsect) = elf_next_in_group (s);
675 elf_next_in_group (s) = newsect;
677 else
679 const char *gname;
681 gname = group_signature (abfd, shdr);
682 if (gname == NULL)
683 return FALSE;
684 elf_group_name (newsect) = gname;
686 /* Start a circular list with one element. */
687 elf_next_in_group (newsect) = newsect;
690 /* If the group section has been created, point to the
691 new member. */
692 if (shdr->bfd_section != NULL)
693 elf_next_in_group (shdr->bfd_section) = newsect;
695 i = num_group - 1;
696 break;
701 if (elf_group_name (newsect) == NULL)
703 (*_bfd_error_handler) (_("%B: no group info for section %A"),
704 abfd, newsect);
706 return TRUE;
709 bfd_boolean
710 _bfd_elf_setup_sections (bfd *abfd)
712 unsigned int i;
713 unsigned int num_group = elf_tdata (abfd)->num_group;
714 bfd_boolean result = TRUE;
715 asection *s;
717 /* Process SHF_LINK_ORDER. */
718 for (s = abfd->sections; s != NULL; s = s->next)
720 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
721 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
723 unsigned int elfsec = this_hdr->sh_link;
724 /* FIXME: The old Intel compiler and old strip/objcopy may
725 not set the sh_link or sh_info fields. Hence we could
726 get the situation where elfsec is 0. */
727 if (elfsec == 0)
729 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
730 if (bed->link_order_error_handler)
731 bed->link_order_error_handler
732 (_("%B: warning: sh_link not set for section `%A'"),
733 abfd, s);
735 else
737 asection *link = NULL;
739 if (elfsec < elf_numsections (abfd))
741 this_hdr = elf_elfsections (abfd)[elfsec];
742 link = this_hdr->bfd_section;
745 /* PR 1991, 2008:
746 Some strip/objcopy may leave an incorrect value in
747 sh_link. We don't want to proceed. */
748 if (link == NULL)
750 (*_bfd_error_handler)
751 (_("%B: sh_link [%d] in section `%A' is incorrect"),
752 s->owner, s, elfsec);
753 result = FALSE;
756 elf_linked_to_section (s) = link;
761 /* Process section groups. */
762 if (num_group == (unsigned) -1)
763 return result;
765 for (i = 0; i < num_group; i++)
767 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
768 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
769 unsigned int n_elt = shdr->sh_size / 4;
771 while (--n_elt != 0)
772 if ((++idx)->shdr->bfd_section)
773 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
774 else if (idx->shdr->sh_type == SHT_RELA
775 || idx->shdr->sh_type == SHT_REL)
776 /* We won't include relocation sections in section groups in
777 output object files. We adjust the group section size here
778 so that relocatable link will work correctly when
779 relocation sections are in section group in input object
780 files. */
781 shdr->bfd_section->size -= 4;
782 else
784 /* There are some unknown sections in the group. */
785 (*_bfd_error_handler)
786 (_("%B: unknown [%d] section `%s' in group [%s]"),
787 abfd,
788 (unsigned int) idx->shdr->sh_type,
789 bfd_elf_string_from_elf_section (abfd,
790 (elf_elfheader (abfd)
791 ->e_shstrndx),
792 idx->shdr->sh_name),
793 shdr->bfd_section->name);
794 result = FALSE;
797 return result;
800 bfd_boolean
801 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
803 return elf_next_in_group (sec) != NULL;
806 /* Make a BFD section from an ELF section. We store a pointer to the
807 BFD section in the bfd_section field of the header. */
809 bfd_boolean
810 _bfd_elf_make_section_from_shdr (bfd *abfd,
811 Elf_Internal_Shdr *hdr,
812 const char *name,
813 int shindex)
815 asection *newsect;
816 flagword flags;
817 const struct elf_backend_data *bed;
819 if (hdr->bfd_section != NULL)
821 BFD_ASSERT (strcmp (name,
822 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
823 return TRUE;
826 newsect = bfd_make_section_anyway (abfd, name);
827 if (newsect == NULL)
828 return FALSE;
830 hdr->bfd_section = newsect;
831 elf_section_data (newsect)->this_hdr = *hdr;
832 elf_section_data (newsect)->this_idx = shindex;
834 /* Always use the real type/flags. */
835 elf_section_type (newsect) = hdr->sh_type;
836 elf_section_flags (newsect) = hdr->sh_flags;
838 newsect->filepos = hdr->sh_offset;
840 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
841 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
842 || ! bfd_set_section_alignment (abfd, newsect,
843 bfd_log2 (hdr->sh_addralign)))
844 return FALSE;
846 flags = SEC_NO_FLAGS;
847 if (hdr->sh_type != SHT_NOBITS)
848 flags |= SEC_HAS_CONTENTS;
849 if (hdr->sh_type == SHT_GROUP)
850 flags |= SEC_GROUP | SEC_EXCLUDE;
851 if ((hdr->sh_flags & SHF_ALLOC) != 0)
853 flags |= SEC_ALLOC;
854 if (hdr->sh_type != SHT_NOBITS)
855 flags |= SEC_LOAD;
857 if ((hdr->sh_flags & SHF_WRITE) == 0)
858 flags |= SEC_READONLY;
859 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
860 flags |= SEC_CODE;
861 else if ((flags & SEC_LOAD) != 0)
862 flags |= SEC_DATA;
863 if ((hdr->sh_flags & SHF_MERGE) != 0)
865 flags |= SEC_MERGE;
866 newsect->entsize = hdr->sh_entsize;
867 if ((hdr->sh_flags & SHF_STRINGS) != 0)
868 flags |= SEC_STRINGS;
870 if (hdr->sh_flags & SHF_GROUP)
871 if (!setup_group (abfd, hdr, newsect))
872 return FALSE;
873 if ((hdr->sh_flags & SHF_TLS) != 0)
874 flags |= SEC_THREAD_LOCAL;
876 if ((flags & SEC_ALLOC) == 0)
878 /* The debugging sections appear to be recognized only by name,
879 not any sort of flag. Their SEC_ALLOC bits are cleared. */
880 static const struct
882 const char *name;
883 int len;
884 } debug_sections [] =
886 { STRING_COMMA_LEN ("debug") }, /* 'd' */
887 { NULL, 0 }, /* 'e' */
888 { NULL, 0 }, /* 'f' */
889 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
890 { NULL, 0 }, /* 'h' */
891 { NULL, 0 }, /* 'i' */
892 { NULL, 0 }, /* 'j' */
893 { NULL, 0 }, /* 'k' */
894 { STRING_COMMA_LEN ("line") }, /* 'l' */
895 { NULL, 0 }, /* 'm' */
896 { NULL, 0 }, /* 'n' */
897 { NULL, 0 }, /* 'o' */
898 { NULL, 0 }, /* 'p' */
899 { NULL, 0 }, /* 'q' */
900 { NULL, 0 }, /* 'r' */
901 { STRING_COMMA_LEN ("stab") }, /* 's' */
902 { NULL, 0 }, /* 't' */
903 { NULL, 0 }, /* 'u' */
904 { NULL, 0 }, /* 'v' */
905 { NULL, 0 }, /* 'w' */
906 { NULL, 0 }, /* 'x' */
907 { NULL, 0 }, /* 'y' */
908 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
911 if (name [0] == '.')
913 int i = name [1] - 'd';
914 if (i >= 0
915 && i < (int) ARRAY_SIZE (debug_sections)
916 && debug_sections [i].name != NULL
917 && strncmp (&name [1], debug_sections [i].name,
918 debug_sections [i].len) == 0)
919 flags |= SEC_DEBUGGING;
923 /* As a GNU extension, if the name begins with .gnu.linkonce, we
924 only link a single copy of the section. This is used to support
925 g++. g++ will emit each template expansion in its own section.
926 The symbols will be defined as weak, so that multiple definitions
927 are permitted. The GNU linker extension is to actually discard
928 all but one of the sections. */
929 if (CONST_STRNEQ (name, ".gnu.linkonce")
930 && elf_next_in_group (newsect) == NULL)
931 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
933 bed = get_elf_backend_data (abfd);
934 if (bed->elf_backend_section_flags)
935 if (! bed->elf_backend_section_flags (&flags, hdr))
936 return FALSE;
938 if (! bfd_set_section_flags (abfd, newsect, flags))
939 return FALSE;
941 /* We do not parse the PT_NOTE segments as we are interested even in the
942 separate debug info files which may have the segments offsets corrupted.
943 PT_NOTEs from the core files are currently not parsed using BFD. */
944 if (hdr->sh_type == SHT_NOTE)
946 bfd_byte *contents;
948 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
949 return FALSE;
951 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
952 free (contents);
955 if ((flags & SEC_ALLOC) != 0)
957 Elf_Internal_Phdr *phdr;
958 unsigned int i, nload;
960 /* Some ELF linkers produce binaries with all the program header
961 p_paddr fields zero. If we have such a binary with more than
962 one PT_LOAD header, then leave the section lma equal to vma
963 so that we don't create sections with overlapping lma. */
964 phdr = elf_tdata (abfd)->phdr;
965 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
966 if (phdr->p_paddr != 0)
967 break;
968 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
969 ++nload;
970 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
971 return TRUE;
973 phdr = elf_tdata (abfd)->phdr;
974 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
976 if (phdr->p_type == PT_LOAD
977 && ELF_IS_SECTION_IN_SEGMENT (hdr, phdr))
979 if ((flags & SEC_LOAD) == 0)
980 newsect->lma = (phdr->p_paddr
981 + hdr->sh_addr - phdr->p_vaddr);
982 else
983 /* We used to use the same adjustment for SEC_LOAD
984 sections, but that doesn't work if the segment
985 is packed with code from multiple VMAs.
986 Instead we calculate the section LMA based on
987 the segment LMA. It is assumed that the
988 segment will contain sections with contiguous
989 LMAs, even if the VMAs are not. */
990 newsect->lma = (phdr->p_paddr
991 + hdr->sh_offset - phdr->p_offset);
993 /* With contiguous segments, we can't tell from file
994 offsets whether a section with zero size should
995 be placed at the end of one segment or the
996 beginning of the next. Decide based on vaddr. */
997 if (hdr->sh_addr >= phdr->p_vaddr
998 && (hdr->sh_addr + hdr->sh_size
999 <= phdr->p_vaddr + phdr->p_memsz))
1000 break;
1005 return TRUE;
1008 const char *const bfd_elf_section_type_names[] = {
1009 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1010 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1011 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1014 /* ELF relocs are against symbols. If we are producing relocatable
1015 output, and the reloc is against an external symbol, and nothing
1016 has given us any additional addend, the resulting reloc will also
1017 be against the same symbol. In such a case, we don't want to
1018 change anything about the way the reloc is handled, since it will
1019 all be done at final link time. Rather than put special case code
1020 into bfd_perform_relocation, all the reloc types use this howto
1021 function. It just short circuits the reloc if producing
1022 relocatable output against an external symbol. */
1024 bfd_reloc_status_type
1025 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1026 arelent *reloc_entry,
1027 asymbol *symbol,
1028 void *data ATTRIBUTE_UNUSED,
1029 asection *input_section,
1030 bfd *output_bfd,
1031 char **error_message ATTRIBUTE_UNUSED)
1033 if (output_bfd != NULL
1034 && (symbol->flags & BSF_SECTION_SYM) == 0
1035 && (! reloc_entry->howto->partial_inplace
1036 || reloc_entry->addend == 0))
1038 reloc_entry->address += input_section->output_offset;
1039 return bfd_reloc_ok;
1042 return bfd_reloc_continue;
1045 /* Copy the program header and other data from one object module to
1046 another. */
1048 bfd_boolean
1049 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1051 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1052 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1053 return TRUE;
1055 BFD_ASSERT (!elf_flags_init (obfd)
1056 || (elf_elfheader (obfd)->e_flags
1057 == elf_elfheader (ibfd)->e_flags));
1059 elf_gp (obfd) = elf_gp (ibfd);
1060 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1061 elf_flags_init (obfd) = TRUE;
1063 /* Copy object attributes. */
1064 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1066 return TRUE;
1069 static const char *
1070 get_segment_type (unsigned int p_type)
1072 const char *pt;
1073 switch (p_type)
1075 case PT_NULL: pt = "NULL"; break;
1076 case PT_LOAD: pt = "LOAD"; break;
1077 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1078 case PT_INTERP: pt = "INTERP"; break;
1079 case PT_NOTE: pt = "NOTE"; break;
1080 case PT_SHLIB: pt = "SHLIB"; break;
1081 case PT_PHDR: pt = "PHDR"; break;
1082 case PT_TLS: pt = "TLS"; break;
1083 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1084 case PT_GNU_STACK: pt = "STACK"; break;
1085 case PT_GNU_RELRO: pt = "RELRO"; break;
1086 default: pt = NULL; break;
1088 return pt;
1091 /* Print out the program headers. */
1093 bfd_boolean
1094 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1096 FILE *f = (FILE *) farg;
1097 Elf_Internal_Phdr *p;
1098 asection *s;
1099 bfd_byte *dynbuf = NULL;
1101 p = elf_tdata (abfd)->phdr;
1102 if (p != NULL)
1104 unsigned int i, c;
1106 fprintf (f, _("\nProgram Header:\n"));
1107 c = elf_elfheader (abfd)->e_phnum;
1108 for (i = 0; i < c; i++, p++)
1110 const char *pt = get_segment_type (p->p_type);
1111 char buf[20];
1113 if (pt == NULL)
1115 sprintf (buf, "0x%lx", p->p_type);
1116 pt = buf;
1118 fprintf (f, "%8s off 0x", pt);
1119 bfd_fprintf_vma (abfd, f, p->p_offset);
1120 fprintf (f, " vaddr 0x");
1121 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1122 fprintf (f, " paddr 0x");
1123 bfd_fprintf_vma (abfd, f, p->p_paddr);
1124 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1125 fprintf (f, " filesz 0x");
1126 bfd_fprintf_vma (abfd, f, p->p_filesz);
1127 fprintf (f, " memsz 0x");
1128 bfd_fprintf_vma (abfd, f, p->p_memsz);
1129 fprintf (f, " flags %c%c%c",
1130 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1131 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1132 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1133 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1134 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1135 fprintf (f, "\n");
1139 s = bfd_get_section_by_name (abfd, ".dynamic");
1140 if (s != NULL)
1142 unsigned int elfsec;
1143 unsigned long shlink;
1144 bfd_byte *extdyn, *extdynend;
1145 size_t extdynsize;
1146 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1148 fprintf (f, _("\nDynamic Section:\n"));
1150 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1151 goto error_return;
1153 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1154 if (elfsec == SHN_BAD)
1155 goto error_return;
1156 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1158 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1159 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1161 extdyn = dynbuf;
1162 extdynend = extdyn + s->size;
1163 for (; extdyn < extdynend; extdyn += extdynsize)
1165 Elf_Internal_Dyn dyn;
1166 const char *name = "";
1167 char ab[20];
1168 bfd_boolean stringp;
1169 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1171 (*swap_dyn_in) (abfd, extdyn, &dyn);
1173 if (dyn.d_tag == DT_NULL)
1174 break;
1176 stringp = FALSE;
1177 switch (dyn.d_tag)
1179 default:
1180 if (bed->elf_backend_get_target_dtag)
1181 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1183 if (!strcmp (name, ""))
1185 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1186 name = ab;
1188 break;
1190 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1191 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1192 case DT_PLTGOT: name = "PLTGOT"; break;
1193 case DT_HASH: name = "HASH"; break;
1194 case DT_STRTAB: name = "STRTAB"; break;
1195 case DT_SYMTAB: name = "SYMTAB"; break;
1196 case DT_RELA: name = "RELA"; break;
1197 case DT_RELASZ: name = "RELASZ"; break;
1198 case DT_RELAENT: name = "RELAENT"; break;
1199 case DT_STRSZ: name = "STRSZ"; break;
1200 case DT_SYMENT: name = "SYMENT"; break;
1201 case DT_INIT: name = "INIT"; break;
1202 case DT_FINI: name = "FINI"; break;
1203 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1204 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1205 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1206 case DT_REL: name = "REL"; break;
1207 case DT_RELSZ: name = "RELSZ"; break;
1208 case DT_RELENT: name = "RELENT"; break;
1209 case DT_PLTREL: name = "PLTREL"; break;
1210 case DT_DEBUG: name = "DEBUG"; break;
1211 case DT_TEXTREL: name = "TEXTREL"; break;
1212 case DT_JMPREL: name = "JMPREL"; break;
1213 case DT_BIND_NOW: name = "BIND_NOW"; break;
1214 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1215 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1216 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1217 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1218 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1219 case DT_FLAGS: name = "FLAGS"; break;
1220 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1221 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1222 case DT_CHECKSUM: name = "CHECKSUM"; break;
1223 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1224 case DT_MOVEENT: name = "MOVEENT"; break;
1225 case DT_MOVESZ: name = "MOVESZ"; break;
1226 case DT_FEATURE: name = "FEATURE"; break;
1227 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1228 case DT_SYMINSZ: name = "SYMINSZ"; break;
1229 case DT_SYMINENT: name = "SYMINENT"; break;
1230 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1231 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1232 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1233 case DT_PLTPAD: name = "PLTPAD"; break;
1234 case DT_MOVETAB: name = "MOVETAB"; break;
1235 case DT_SYMINFO: name = "SYMINFO"; break;
1236 case DT_RELACOUNT: name = "RELACOUNT"; break;
1237 case DT_RELCOUNT: name = "RELCOUNT"; break;
1238 case DT_FLAGS_1: name = "FLAGS_1"; break;
1239 case DT_VERSYM: name = "VERSYM"; break;
1240 case DT_VERDEF: name = "VERDEF"; break;
1241 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1242 case DT_VERNEED: name = "VERNEED"; break;
1243 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1244 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1245 case DT_USED: name = "USED"; break;
1246 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1247 case DT_GNU_HASH: name = "GNU_HASH"; break;
1250 fprintf (f, " %-20s ", name);
1251 if (! stringp)
1253 fprintf (f, "0x");
1254 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1256 else
1258 const char *string;
1259 unsigned int tagv = dyn.d_un.d_val;
1261 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1262 if (string == NULL)
1263 goto error_return;
1264 fprintf (f, "%s", string);
1266 fprintf (f, "\n");
1269 free (dynbuf);
1270 dynbuf = NULL;
1273 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1274 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1276 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1277 return FALSE;
1280 if (elf_dynverdef (abfd) != 0)
1282 Elf_Internal_Verdef *t;
1284 fprintf (f, _("\nVersion definitions:\n"));
1285 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1287 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1288 t->vd_flags, t->vd_hash,
1289 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1290 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1292 Elf_Internal_Verdaux *a;
1294 fprintf (f, "\t");
1295 for (a = t->vd_auxptr->vda_nextptr;
1296 a != NULL;
1297 a = a->vda_nextptr)
1298 fprintf (f, "%s ",
1299 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1300 fprintf (f, "\n");
1305 if (elf_dynverref (abfd) != 0)
1307 Elf_Internal_Verneed *t;
1309 fprintf (f, _("\nVersion References:\n"));
1310 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1312 Elf_Internal_Vernaux *a;
1314 fprintf (f, _(" required from %s:\n"),
1315 t->vn_filename ? t->vn_filename : "<corrupt>");
1316 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1317 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1318 a->vna_flags, a->vna_other,
1319 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1323 return TRUE;
1325 error_return:
1326 if (dynbuf != NULL)
1327 free (dynbuf);
1328 return FALSE;
1331 /* Display ELF-specific fields of a symbol. */
1333 void
1334 bfd_elf_print_symbol (bfd *abfd,
1335 void *filep,
1336 asymbol *symbol,
1337 bfd_print_symbol_type how)
1339 FILE *file = (FILE *) filep;
1340 switch (how)
1342 case bfd_print_symbol_name:
1343 fprintf (file, "%s", symbol->name);
1344 break;
1345 case bfd_print_symbol_more:
1346 fprintf (file, "elf ");
1347 bfd_fprintf_vma (abfd, file, symbol->value);
1348 fprintf (file, " %lx", (unsigned long) symbol->flags);
1349 break;
1350 case bfd_print_symbol_all:
1352 const char *section_name;
1353 const char *name = NULL;
1354 const struct elf_backend_data *bed;
1355 unsigned char st_other;
1356 bfd_vma val;
1358 section_name = symbol->section ? symbol->section->name : "(*none*)";
1360 bed = get_elf_backend_data (abfd);
1361 if (bed->elf_backend_print_symbol_all)
1362 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1364 if (name == NULL)
1366 name = symbol->name;
1367 bfd_print_symbol_vandf (abfd, file, symbol);
1370 fprintf (file, " %s\t", section_name);
1371 /* Print the "other" value for a symbol. For common symbols,
1372 we've already printed the size; now print the alignment.
1373 For other symbols, we have no specified alignment, and
1374 we've printed the address; now print the size. */
1375 if (symbol->section && bfd_is_com_section (symbol->section))
1376 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1377 else
1378 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1379 bfd_fprintf_vma (abfd, file, val);
1381 /* If we have version information, print it. */
1382 if (elf_tdata (abfd)->dynversym_section != 0
1383 && (elf_tdata (abfd)->dynverdef_section != 0
1384 || elf_tdata (abfd)->dynverref_section != 0))
1386 unsigned int vernum;
1387 const char *version_string;
1389 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1391 if (vernum == 0)
1392 version_string = "";
1393 else if (vernum == 1)
1394 version_string = "Base";
1395 else if (vernum <= elf_tdata (abfd)->cverdefs)
1396 version_string =
1397 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1398 else
1400 Elf_Internal_Verneed *t;
1402 version_string = "";
1403 for (t = elf_tdata (abfd)->verref;
1404 t != NULL;
1405 t = t->vn_nextref)
1407 Elf_Internal_Vernaux *a;
1409 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1411 if (a->vna_other == vernum)
1413 version_string = a->vna_nodename;
1414 break;
1420 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1421 fprintf (file, " %-11s", version_string);
1422 else
1424 int i;
1426 fprintf (file, " (%s)", version_string);
1427 for (i = 10 - strlen (version_string); i > 0; --i)
1428 putc (' ', file);
1432 /* If the st_other field is not zero, print it. */
1433 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1435 switch (st_other)
1437 case 0: break;
1438 case STV_INTERNAL: fprintf (file, " .internal"); break;
1439 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1440 case STV_PROTECTED: fprintf (file, " .protected"); break;
1441 default:
1442 /* Some other non-defined flags are also present, so print
1443 everything hex. */
1444 fprintf (file, " 0x%02x", (unsigned int) st_other);
1447 fprintf (file, " %s", name);
1449 break;
1453 /* Allocate an ELF string table--force the first byte to be zero. */
1455 struct bfd_strtab_hash *
1456 _bfd_elf_stringtab_init (void)
1458 struct bfd_strtab_hash *ret;
1460 ret = _bfd_stringtab_init ();
1461 if (ret != NULL)
1463 bfd_size_type loc;
1465 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1466 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1467 if (loc == (bfd_size_type) -1)
1469 _bfd_stringtab_free (ret);
1470 ret = NULL;
1473 return ret;
1476 /* ELF .o/exec file reading */
1478 /* Create a new bfd section from an ELF section header. */
1480 bfd_boolean
1481 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1483 Elf_Internal_Shdr *hdr;
1484 Elf_Internal_Ehdr *ehdr;
1485 const struct elf_backend_data *bed;
1486 const char *name;
1488 if (shindex >= elf_numsections (abfd))
1489 return FALSE;
1491 hdr = elf_elfsections (abfd)[shindex];
1492 ehdr = elf_elfheader (abfd);
1493 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1494 hdr->sh_name);
1495 if (name == NULL)
1496 return FALSE;
1498 bed = get_elf_backend_data (abfd);
1499 switch (hdr->sh_type)
1501 case SHT_NULL:
1502 /* Inactive section. Throw it away. */
1503 return TRUE;
1505 case SHT_PROGBITS: /* Normal section with contents. */
1506 case SHT_NOBITS: /* .bss section. */
1507 case SHT_HASH: /* .hash section. */
1508 case SHT_NOTE: /* .note section. */
1509 case SHT_INIT_ARRAY: /* .init_array section. */
1510 case SHT_FINI_ARRAY: /* .fini_array section. */
1511 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1512 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1513 case SHT_GNU_HASH: /* .gnu.hash section. */
1514 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1516 case SHT_DYNAMIC: /* Dynamic linking information. */
1517 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1518 return FALSE;
1519 if (hdr->sh_link > elf_numsections (abfd))
1521 /* PR 10478: Accept Solaris binaries with a sh_link
1522 field set to SHN_BEFORE or SHN_AFTER. */
1523 switch (bfd_get_arch (abfd))
1525 case bfd_arch_i386:
1526 case bfd_arch_sparc:
1527 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1528 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1529 break;
1530 /* Otherwise fall through. */
1531 default:
1532 return FALSE;
1535 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1536 return FALSE;
1537 else 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.
1733 Don't do it on executable nor shared library. */
1734 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1735 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1736 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1738 unsigned int scan;
1739 int found;
1741 found = 0;
1742 for (scan = 1; scan < num_sec; scan++)
1744 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1745 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1747 if (found != 0)
1749 found = 0;
1750 break;
1752 found = scan;
1755 if (found != 0)
1756 hdr->sh_link = found;
1759 /* Get the symbol table. */
1760 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1761 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1762 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1763 return FALSE;
1765 /* If this reloc section does not use the main symbol table we
1766 don't treat it as a reloc section. BFD can't adequately
1767 represent such a section, so at least for now, we don't
1768 try. We just present it as a normal section. We also
1769 can't use it as a reloc section if it points to the null
1770 section, an invalid section, another reloc section, or its
1771 sh_link points to the null section. */
1772 if (hdr->sh_link != elf_onesymtab (abfd)
1773 || hdr->sh_link == SHN_UNDEF
1774 || hdr->sh_info == SHN_UNDEF
1775 || hdr->sh_info >= num_sec
1776 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1777 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1778 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1779 shindex);
1781 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1782 return FALSE;
1783 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1784 if (target_sect == NULL)
1785 return FALSE;
1787 if ((target_sect->flags & SEC_RELOC) == 0
1788 || target_sect->reloc_count == 0)
1789 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1790 else
1792 bfd_size_type amt;
1793 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1794 amt = sizeof (*hdr2);
1795 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1796 if (hdr2 == NULL)
1797 return FALSE;
1798 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1800 *hdr2 = *hdr;
1801 elf_elfsections (abfd)[shindex] = hdr2;
1802 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1803 target_sect->flags |= SEC_RELOC;
1804 target_sect->relocation = NULL;
1805 target_sect->rel_filepos = hdr->sh_offset;
1806 /* In the section to which the relocations apply, mark whether
1807 its relocations are of the REL or RELA variety. */
1808 if (hdr->sh_size != 0)
1809 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1810 abfd->flags |= HAS_RELOC;
1811 return TRUE;
1814 case SHT_GNU_verdef:
1815 elf_dynverdef (abfd) = shindex;
1816 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1817 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1819 case SHT_GNU_versym:
1820 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1821 return FALSE;
1822 elf_dynversym (abfd) = shindex;
1823 elf_tdata (abfd)->dynversym_hdr = *hdr;
1824 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1826 case SHT_GNU_verneed:
1827 elf_dynverref (abfd) = shindex;
1828 elf_tdata (abfd)->dynverref_hdr = *hdr;
1829 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1831 case SHT_SHLIB:
1832 return TRUE;
1834 case SHT_GROUP:
1835 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1836 return FALSE;
1837 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1838 return FALSE;
1839 if (hdr->contents != NULL)
1841 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1842 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1843 asection *s;
1845 if (idx->flags & GRP_COMDAT)
1846 hdr->bfd_section->flags
1847 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1849 /* We try to keep the same section order as it comes in. */
1850 idx += n_elt;
1851 while (--n_elt != 0)
1853 --idx;
1855 if (idx->shdr != NULL
1856 && (s = idx->shdr->bfd_section) != NULL
1857 && elf_next_in_group (s) != NULL)
1859 elf_next_in_group (hdr->bfd_section) = s;
1860 break;
1864 break;
1866 default:
1867 /* Possibly an attributes section. */
1868 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1869 || hdr->sh_type == bed->obj_attrs_section_type)
1871 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1872 return FALSE;
1873 _bfd_elf_parse_attributes (abfd, hdr);
1874 return TRUE;
1877 /* Check for any processor-specific section types. */
1878 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1879 return TRUE;
1881 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1883 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1884 /* FIXME: How to properly handle allocated section reserved
1885 for applications? */
1886 (*_bfd_error_handler)
1887 (_("%B: don't know how to handle allocated, application "
1888 "specific section `%s' [0x%8x]"),
1889 abfd, name, hdr->sh_type);
1890 else
1891 /* Allow sections reserved for applications. */
1892 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1893 shindex);
1895 else if (hdr->sh_type >= SHT_LOPROC
1896 && hdr->sh_type <= SHT_HIPROC)
1897 /* FIXME: We should handle this section. */
1898 (*_bfd_error_handler)
1899 (_("%B: don't know how to handle processor specific section "
1900 "`%s' [0x%8x]"),
1901 abfd, name, hdr->sh_type);
1902 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1904 /* Unrecognised OS-specific sections. */
1905 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1906 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1907 required to correctly process the section and the file should
1908 be rejected with an error message. */
1909 (*_bfd_error_handler)
1910 (_("%B: don't know how to handle OS specific section "
1911 "`%s' [0x%8x]"),
1912 abfd, name, hdr->sh_type);
1913 else
1914 /* Otherwise it should be processed. */
1915 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1917 else
1918 /* FIXME: We should handle this section. */
1919 (*_bfd_error_handler)
1920 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1921 abfd, name, hdr->sh_type);
1923 return FALSE;
1926 return TRUE;
1929 /* Return the local symbol specified by ABFD, R_SYMNDX. */
1931 Elf_Internal_Sym *
1932 bfd_sym_from_r_symndx (struct sym_cache *cache,
1933 bfd *abfd,
1934 unsigned long r_symndx)
1936 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1938 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1940 Elf_Internal_Shdr *symtab_hdr;
1941 unsigned char esym[sizeof (Elf64_External_Sym)];
1942 Elf_External_Sym_Shndx eshndx;
1944 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1945 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1946 &cache->sym[ent], esym, &eshndx) == NULL)
1947 return NULL;
1949 if (cache->abfd != abfd)
1951 memset (cache->indx, -1, sizeof (cache->indx));
1952 cache->abfd = abfd;
1954 cache->indx[ent] = r_symndx;
1957 return &cache->sym[ent];
1960 /* Given an ELF section number, retrieve the corresponding BFD
1961 section. */
1963 asection *
1964 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
1966 if (index >= elf_numsections (abfd))
1967 return NULL;
1968 return elf_elfsections (abfd)[index]->bfd_section;
1971 static const struct bfd_elf_special_section special_sections_b[] =
1973 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1974 { NULL, 0, 0, 0, 0 }
1977 static const struct bfd_elf_special_section special_sections_c[] =
1979 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1980 { NULL, 0, 0, 0, 0 }
1983 static const struct bfd_elf_special_section special_sections_d[] =
1985 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1986 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1987 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
1988 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
1989 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
1990 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
1991 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
1992 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
1993 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
1994 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
1995 { NULL, 0, 0, 0, 0 }
1998 static const struct bfd_elf_special_section special_sections_f[] =
2000 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2001 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2002 { NULL, 0, 0, 0, 0 }
2005 static const struct bfd_elf_special_section special_sections_g[] =
2007 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2008 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2009 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2010 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2011 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2012 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2013 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2014 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2015 { NULL, 0, 0, 0, 0 }
2018 static const struct bfd_elf_special_section special_sections_h[] =
2020 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2021 { NULL, 0, 0, 0, 0 }
2024 static const struct bfd_elf_special_section special_sections_i[] =
2026 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2027 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2028 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2029 { NULL, 0, 0, 0, 0 }
2032 static const struct bfd_elf_special_section special_sections_l[] =
2034 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2035 { NULL, 0, 0, 0, 0 }
2038 static const struct bfd_elf_special_section special_sections_n[] =
2040 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2041 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2042 { NULL, 0, 0, 0, 0 }
2045 static const struct bfd_elf_special_section special_sections_p[] =
2047 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2048 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2049 { NULL, 0, 0, 0, 0 }
2052 static const struct bfd_elf_special_section special_sections_r[] =
2054 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2055 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2056 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2057 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2058 { NULL, 0, 0, 0, 0 }
2061 static const struct bfd_elf_special_section special_sections_s[] =
2063 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2064 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2065 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2066 /* See struct bfd_elf_special_section declaration for the semantics of
2067 this special case where .prefix_length != strlen (.prefix). */
2068 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2069 { NULL, 0, 0, 0, 0 }
2072 static const struct bfd_elf_special_section special_sections_t[] =
2074 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2075 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2076 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2077 { NULL, 0, 0, 0, 0 }
2080 static const struct bfd_elf_special_section special_sections_z[] =
2082 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2083 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2084 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2085 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2086 { NULL, 0, 0, 0, 0 }
2089 static const struct bfd_elf_special_section *special_sections[] =
2091 special_sections_b, /* 'b' */
2092 special_sections_c, /* 'c' */
2093 special_sections_d, /* 'd' */
2094 NULL, /* 'e' */
2095 special_sections_f, /* 'f' */
2096 special_sections_g, /* 'g' */
2097 special_sections_h, /* 'h' */
2098 special_sections_i, /* 'i' */
2099 NULL, /* 'j' */
2100 NULL, /* 'k' */
2101 special_sections_l, /* 'l' */
2102 NULL, /* 'm' */
2103 special_sections_n, /* 'n' */
2104 NULL, /* 'o' */
2105 special_sections_p, /* 'p' */
2106 NULL, /* 'q' */
2107 special_sections_r, /* 'r' */
2108 special_sections_s, /* 's' */
2109 special_sections_t, /* 't' */
2110 NULL, /* 'u' */
2111 NULL, /* 'v' */
2112 NULL, /* 'w' */
2113 NULL, /* 'x' */
2114 NULL, /* 'y' */
2115 special_sections_z /* 'z' */
2118 const struct bfd_elf_special_section *
2119 _bfd_elf_get_special_section (const char *name,
2120 const struct bfd_elf_special_section *spec,
2121 unsigned int rela)
2123 int i;
2124 int len;
2126 len = strlen (name);
2128 for (i = 0; spec[i].prefix != NULL; i++)
2130 int suffix_len;
2131 int prefix_len = spec[i].prefix_length;
2133 if (len < prefix_len)
2134 continue;
2135 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2136 continue;
2138 suffix_len = spec[i].suffix_length;
2139 if (suffix_len <= 0)
2141 if (name[prefix_len] != 0)
2143 if (suffix_len == 0)
2144 continue;
2145 if (name[prefix_len] != '.'
2146 && (suffix_len == -2
2147 || (rela && spec[i].type == SHT_REL)))
2148 continue;
2151 else
2153 if (len < prefix_len + suffix_len)
2154 continue;
2155 if (memcmp (name + len - suffix_len,
2156 spec[i].prefix + prefix_len,
2157 suffix_len) != 0)
2158 continue;
2160 return &spec[i];
2163 return NULL;
2166 const struct bfd_elf_special_section *
2167 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2169 int i;
2170 const struct bfd_elf_special_section *spec;
2171 const struct elf_backend_data *bed;
2173 /* See if this is one of the special sections. */
2174 if (sec->name == NULL)
2175 return NULL;
2177 bed = get_elf_backend_data (abfd);
2178 spec = bed->special_sections;
2179 if (spec)
2181 spec = _bfd_elf_get_special_section (sec->name,
2182 bed->special_sections,
2183 sec->use_rela_p);
2184 if (spec != NULL)
2185 return spec;
2188 if (sec->name[0] != '.')
2189 return NULL;
2191 i = sec->name[1] - 'b';
2192 if (i < 0 || i > 'z' - 'b')
2193 return NULL;
2195 spec = special_sections[i];
2197 if (spec == NULL)
2198 return NULL;
2200 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2203 bfd_boolean
2204 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2206 struct bfd_elf_section_data *sdata;
2207 const struct elf_backend_data *bed;
2208 const struct bfd_elf_special_section *ssect;
2210 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2211 if (sdata == NULL)
2213 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2214 sizeof (*sdata));
2215 if (sdata == NULL)
2216 return FALSE;
2217 sec->used_by_bfd = sdata;
2220 /* Indicate whether or not this section should use RELA relocations. */
2221 bed = get_elf_backend_data (abfd);
2222 sec->use_rela_p = bed->default_use_rela_p;
2224 /* When we read a file, we don't need to set ELF section type and
2225 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2226 anyway. We will set ELF section type and flags for all linker
2227 created sections. If user specifies BFD section flags, we will
2228 set ELF section type and flags based on BFD section flags in
2229 elf_fake_sections. */
2230 if ((!sec->flags && abfd->direction != read_direction)
2231 || (sec->flags & SEC_LINKER_CREATED) != 0)
2233 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2234 if (ssect != NULL)
2236 elf_section_type (sec) = ssect->type;
2237 elf_section_flags (sec) = ssect->attr;
2241 return _bfd_generic_new_section_hook (abfd, sec);
2244 /* Create a new bfd section from an ELF program header.
2246 Since program segments have no names, we generate a synthetic name
2247 of the form segment<NUM>, where NUM is generally the index in the
2248 program header table. For segments that are split (see below) we
2249 generate the names segment<NUM>a and segment<NUM>b.
2251 Note that some program segments may have a file size that is different than
2252 (less than) the memory size. All this means is that at execution the
2253 system must allocate the amount of memory specified by the memory size,
2254 but only initialize it with the first "file size" bytes read from the
2255 file. This would occur for example, with program segments consisting
2256 of combined data+bss.
2258 To handle the above situation, this routine generates TWO bfd sections
2259 for the single program segment. The first has the length specified by
2260 the file size of the segment, and the second has the length specified
2261 by the difference between the two sizes. In effect, the segment is split
2262 into its initialized and uninitialized parts.
2266 bfd_boolean
2267 _bfd_elf_make_section_from_phdr (bfd *abfd,
2268 Elf_Internal_Phdr *hdr,
2269 int index,
2270 const char *type_name)
2272 asection *newsect;
2273 char *name;
2274 char namebuf[64];
2275 size_t len;
2276 int split;
2278 split = ((hdr->p_memsz > 0)
2279 && (hdr->p_filesz > 0)
2280 && (hdr->p_memsz > hdr->p_filesz));
2282 if (hdr->p_filesz > 0)
2284 sprintf (namebuf, "%s%d%s", type_name, index, split ? "a" : "");
2285 len = strlen (namebuf) + 1;
2286 name = (char *) bfd_alloc (abfd, len);
2287 if (!name)
2288 return FALSE;
2289 memcpy (name, namebuf, len);
2290 newsect = bfd_make_section (abfd, name);
2291 if (newsect == NULL)
2292 return FALSE;
2293 newsect->vma = hdr->p_vaddr;
2294 newsect->lma = hdr->p_paddr;
2295 newsect->size = hdr->p_filesz;
2296 newsect->filepos = hdr->p_offset;
2297 newsect->flags |= SEC_HAS_CONTENTS;
2298 newsect->alignment_power = bfd_log2 (hdr->p_align);
2299 if (hdr->p_type == PT_LOAD)
2301 newsect->flags |= SEC_ALLOC;
2302 newsect->flags |= SEC_LOAD;
2303 if (hdr->p_flags & PF_X)
2305 /* FIXME: all we known is that it has execute PERMISSION,
2306 may be data. */
2307 newsect->flags |= SEC_CODE;
2310 if (!(hdr->p_flags & PF_W))
2312 newsect->flags |= SEC_READONLY;
2316 if (hdr->p_memsz > hdr->p_filesz)
2318 bfd_vma align;
2320 sprintf (namebuf, "%s%d%s", type_name, index, split ? "b" : "");
2321 len = strlen (namebuf) + 1;
2322 name = (char *) bfd_alloc (abfd, len);
2323 if (!name)
2324 return FALSE;
2325 memcpy (name, namebuf, len);
2326 newsect = bfd_make_section (abfd, name);
2327 if (newsect == NULL)
2328 return FALSE;
2329 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2330 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2331 newsect->size = hdr->p_memsz - hdr->p_filesz;
2332 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2333 align = newsect->vma & -newsect->vma;
2334 if (align == 0 || align > hdr->p_align)
2335 align = hdr->p_align;
2336 newsect->alignment_power = bfd_log2 (align);
2337 if (hdr->p_type == PT_LOAD)
2339 /* Hack for gdb. Segments that have not been modified do
2340 not have their contents written to a core file, on the
2341 assumption that a debugger can find the contents in the
2342 executable. We flag this case by setting the fake
2343 section size to zero. Note that "real" bss sections will
2344 always have their contents dumped to the core file. */
2345 if (bfd_get_format (abfd) == bfd_core)
2346 newsect->size = 0;
2347 newsect->flags |= SEC_ALLOC;
2348 if (hdr->p_flags & PF_X)
2349 newsect->flags |= SEC_CODE;
2351 if (!(hdr->p_flags & PF_W))
2352 newsect->flags |= SEC_READONLY;
2355 return TRUE;
2358 bfd_boolean
2359 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2361 const struct elf_backend_data *bed;
2363 switch (hdr->p_type)
2365 case PT_NULL:
2366 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2368 case PT_LOAD:
2369 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2371 case PT_DYNAMIC:
2372 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2374 case PT_INTERP:
2375 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2377 case PT_NOTE:
2378 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2379 return FALSE;
2380 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2381 return FALSE;
2382 return TRUE;
2384 case PT_SHLIB:
2385 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2387 case PT_PHDR:
2388 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2390 case PT_GNU_EH_FRAME:
2391 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2392 "eh_frame_hdr");
2394 case PT_GNU_STACK:
2395 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2397 case PT_GNU_RELRO:
2398 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2400 default:
2401 /* Check for any processor-specific program segment types. */
2402 bed = get_elf_backend_data (abfd);
2403 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2407 /* Initialize REL_HDR, the section-header for new section, containing
2408 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2409 relocations; otherwise, we use REL relocations. */
2411 bfd_boolean
2412 _bfd_elf_init_reloc_shdr (bfd *abfd,
2413 Elf_Internal_Shdr *rel_hdr,
2414 asection *asect,
2415 bfd_boolean use_rela_p)
2417 char *name;
2418 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2419 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2421 name = (char *) bfd_alloc (abfd, amt);
2422 if (name == NULL)
2423 return FALSE;
2424 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2425 rel_hdr->sh_name =
2426 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2427 FALSE);
2428 if (rel_hdr->sh_name == (unsigned int) -1)
2429 return FALSE;
2430 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2431 rel_hdr->sh_entsize = (use_rela_p
2432 ? bed->s->sizeof_rela
2433 : bed->s->sizeof_rel);
2434 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2435 rel_hdr->sh_flags = 0;
2436 rel_hdr->sh_addr = 0;
2437 rel_hdr->sh_size = 0;
2438 rel_hdr->sh_offset = 0;
2440 return TRUE;
2443 /* Return the default section type based on the passed in section flags. */
2446 bfd_elf_get_default_section_type (flagword flags)
2448 if ((flags & SEC_ALLOC) != 0
2449 && ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0
2450 || (flags & SEC_NEVER_LOAD) != 0))
2451 return SHT_NOBITS;
2452 return SHT_PROGBITS;
2455 /* Set up an ELF internal section header for a section. */
2457 static void
2458 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2460 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2461 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2462 Elf_Internal_Shdr *this_hdr;
2463 unsigned int sh_type;
2465 if (*failedptr)
2467 /* We already failed; just get out of the bfd_map_over_sections
2468 loop. */
2469 return;
2472 this_hdr = &elf_section_data (asect)->this_hdr;
2474 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2475 asect->name, FALSE);
2476 if (this_hdr->sh_name == (unsigned int) -1)
2478 *failedptr = TRUE;
2479 return;
2482 /* Don't clear sh_flags. Assembler may set additional bits. */
2484 if ((asect->flags & SEC_ALLOC) != 0
2485 || asect->user_set_vma)
2486 this_hdr->sh_addr = asect->vma;
2487 else
2488 this_hdr->sh_addr = 0;
2490 this_hdr->sh_offset = 0;
2491 this_hdr->sh_size = asect->size;
2492 this_hdr->sh_link = 0;
2493 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2494 /* The sh_entsize and sh_info fields may have been set already by
2495 copy_private_section_data. */
2497 this_hdr->bfd_section = asect;
2498 this_hdr->contents = NULL;
2500 /* If the section type is unspecified, we set it based on
2501 asect->flags. */
2502 if ((asect->flags & SEC_GROUP) != 0)
2503 sh_type = SHT_GROUP;
2504 else
2505 sh_type = bfd_elf_get_default_section_type (asect->flags);
2507 if (this_hdr->sh_type == SHT_NULL)
2508 this_hdr->sh_type = sh_type;
2509 else if (this_hdr->sh_type == SHT_NOBITS
2510 && sh_type == SHT_PROGBITS
2511 && (asect->flags & SEC_ALLOC) != 0)
2513 /* Warn if we are changing a NOBITS section to PROGBITS, but
2514 allow the link to proceed. This can happen when users link
2515 non-bss input sections to bss output sections, or emit data
2516 to a bss output section via a linker script. */
2517 (*_bfd_error_handler)
2518 (_("warning: section `%A' type changed to PROGBITS"), asect);
2519 this_hdr->sh_type = sh_type;
2522 switch (this_hdr->sh_type)
2524 default:
2525 break;
2527 case SHT_STRTAB:
2528 case SHT_INIT_ARRAY:
2529 case SHT_FINI_ARRAY:
2530 case SHT_PREINIT_ARRAY:
2531 case SHT_NOTE:
2532 case SHT_NOBITS:
2533 case SHT_PROGBITS:
2534 break;
2536 case SHT_HASH:
2537 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2538 break;
2540 case SHT_DYNSYM:
2541 this_hdr->sh_entsize = bed->s->sizeof_sym;
2542 break;
2544 case SHT_DYNAMIC:
2545 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2546 break;
2548 case SHT_RELA:
2549 if (get_elf_backend_data (abfd)->may_use_rela_p)
2550 this_hdr->sh_entsize = bed->s->sizeof_rela;
2551 break;
2553 case SHT_REL:
2554 if (get_elf_backend_data (abfd)->may_use_rel_p)
2555 this_hdr->sh_entsize = bed->s->sizeof_rel;
2556 break;
2558 case SHT_GNU_versym:
2559 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2560 break;
2562 case SHT_GNU_verdef:
2563 this_hdr->sh_entsize = 0;
2564 /* objcopy or strip will copy over sh_info, but may not set
2565 cverdefs. The linker will set cverdefs, but sh_info will be
2566 zero. */
2567 if (this_hdr->sh_info == 0)
2568 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2569 else
2570 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2571 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2572 break;
2574 case SHT_GNU_verneed:
2575 this_hdr->sh_entsize = 0;
2576 /* objcopy or strip will copy over sh_info, but may not set
2577 cverrefs. The linker will set cverrefs, but sh_info will be
2578 zero. */
2579 if (this_hdr->sh_info == 0)
2580 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2581 else
2582 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2583 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2584 break;
2586 case SHT_GROUP:
2587 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2588 break;
2590 case SHT_GNU_HASH:
2591 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2592 break;
2595 if ((asect->flags & SEC_ALLOC) != 0)
2596 this_hdr->sh_flags |= SHF_ALLOC;
2597 if ((asect->flags & SEC_READONLY) == 0)
2598 this_hdr->sh_flags |= SHF_WRITE;
2599 if ((asect->flags & SEC_CODE) != 0)
2600 this_hdr->sh_flags |= SHF_EXECINSTR;
2601 if ((asect->flags & SEC_MERGE) != 0)
2603 this_hdr->sh_flags |= SHF_MERGE;
2604 this_hdr->sh_entsize = asect->entsize;
2605 if ((asect->flags & SEC_STRINGS) != 0)
2606 this_hdr->sh_flags |= SHF_STRINGS;
2608 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2609 this_hdr->sh_flags |= SHF_GROUP;
2610 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2612 this_hdr->sh_flags |= SHF_TLS;
2613 if (asect->size == 0
2614 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2616 struct bfd_link_order *o = asect->map_tail.link_order;
2618 this_hdr->sh_size = 0;
2619 if (o != NULL)
2621 this_hdr->sh_size = o->offset + o->size;
2622 if (this_hdr->sh_size != 0)
2623 this_hdr->sh_type = SHT_NOBITS;
2628 /* Check for processor-specific section types. */
2629 sh_type = this_hdr->sh_type;
2630 if (bed->elf_backend_fake_sections
2631 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2632 *failedptr = TRUE;
2634 if (sh_type == SHT_NOBITS && asect->size != 0)
2636 /* Don't change the header type from NOBITS if we are being
2637 called for objcopy --only-keep-debug. */
2638 this_hdr->sh_type = sh_type;
2641 /* If the section has relocs, set up a section header for the
2642 SHT_REL[A] section. If two relocation sections are required for
2643 this section, it is up to the processor-specific back-end to
2644 create the other. */
2645 if ((asect->flags & SEC_RELOC) != 0
2646 && !_bfd_elf_init_reloc_shdr (abfd,
2647 &elf_section_data (asect)->rel_hdr,
2648 asect,
2649 asect->use_rela_p))
2650 *failedptr = TRUE;
2653 /* Fill in the contents of a SHT_GROUP section. Called from
2654 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2655 when ELF targets use the generic linker, ld. Called for ld -r
2656 from bfd_elf_final_link. */
2658 void
2659 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2661 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2662 asection *elt, *first;
2663 unsigned char *loc;
2664 bfd_boolean gas;
2666 /* Ignore linker created group section. See elfNN_ia64_object_p in
2667 elfxx-ia64.c. */
2668 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2669 || *failedptr)
2670 return;
2672 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2674 unsigned long symindx = 0;
2676 /* elf_group_id will have been set up by objcopy and the
2677 generic linker. */
2678 if (elf_group_id (sec) != NULL)
2679 symindx = elf_group_id (sec)->udata.i;
2681 if (symindx == 0)
2683 /* If called from the assembler, swap_out_syms will have set up
2684 elf_section_syms. */
2685 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2686 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2688 elf_section_data (sec)->this_hdr.sh_info = symindx;
2690 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2692 /* The ELF backend linker sets sh_info to -2 when the group
2693 signature symbol is global, and thus the index can't be
2694 set until all local symbols are output. */
2695 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2696 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2697 unsigned long symndx = sec_data->this_hdr.sh_info;
2698 unsigned long extsymoff = 0;
2699 struct elf_link_hash_entry *h;
2701 if (!elf_bad_symtab (igroup->owner))
2703 Elf_Internal_Shdr *symtab_hdr;
2705 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2706 extsymoff = symtab_hdr->sh_info;
2708 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2709 while (h->root.type == bfd_link_hash_indirect
2710 || h->root.type == bfd_link_hash_warning)
2711 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2713 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2716 /* The contents won't be allocated for "ld -r" or objcopy. */
2717 gas = TRUE;
2718 if (sec->contents == NULL)
2720 gas = FALSE;
2721 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2723 /* Arrange for the section to be written out. */
2724 elf_section_data (sec)->this_hdr.contents = sec->contents;
2725 if (sec->contents == NULL)
2727 *failedptr = TRUE;
2728 return;
2732 loc = sec->contents + sec->size;
2734 /* Get the pointer to the first section in the group that gas
2735 squirreled away here. objcopy arranges for this to be set to the
2736 start of the input section group. */
2737 first = elt = elf_next_in_group (sec);
2739 /* First element is a flag word. Rest of section is elf section
2740 indices for all the sections of the group. Write them backwards
2741 just to keep the group in the same order as given in .section
2742 directives, not that it matters. */
2743 while (elt != NULL)
2745 asection *s;
2746 unsigned int idx;
2748 s = elt;
2749 if (! elf_discarded_section (s))
2751 loc -= 4;
2752 if (!gas)
2753 s = s->output_section;
2754 idx = 0;
2755 if (s != NULL)
2756 idx = elf_section_data (s)->this_idx;
2757 H_PUT_32 (abfd, idx, loc);
2759 elt = elf_next_in_group (elt);
2760 if (elt == first)
2761 break;
2764 if ((loc -= 4) != sec->contents)
2765 abort ();
2767 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2770 /* Assign all ELF section numbers. The dummy first section is handled here
2771 too. The link/info pointers for the standard section types are filled
2772 in here too, while we're at it. */
2774 static bfd_boolean
2775 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2777 struct elf_obj_tdata *t = elf_tdata (abfd);
2778 asection *sec;
2779 unsigned int section_number, secn;
2780 Elf_Internal_Shdr **i_shdrp;
2781 struct bfd_elf_section_data *d;
2782 bfd_boolean need_symtab;
2784 section_number = 1;
2786 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2788 /* SHT_GROUP sections are in relocatable files only. */
2789 if (link_info == NULL || link_info->relocatable)
2791 /* Put SHT_GROUP sections first. */
2792 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2794 d = elf_section_data (sec);
2796 if (d->this_hdr.sh_type == SHT_GROUP)
2798 if (sec->flags & SEC_LINKER_CREATED)
2800 /* Remove the linker created SHT_GROUP sections. */
2801 bfd_section_list_remove (abfd, sec);
2802 abfd->section_count--;
2804 else
2805 d->this_idx = section_number++;
2810 for (sec = abfd->sections; sec; sec = sec->next)
2812 d = elf_section_data (sec);
2814 if (d->this_hdr.sh_type != SHT_GROUP)
2815 d->this_idx = section_number++;
2816 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2817 if ((sec->flags & SEC_RELOC) == 0)
2818 d->rel_idx = 0;
2819 else
2821 d->rel_idx = section_number++;
2822 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2825 if (d->rel_hdr2)
2827 d->rel_idx2 = section_number++;
2828 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2830 else
2831 d->rel_idx2 = 0;
2834 t->shstrtab_section = section_number++;
2835 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2836 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2838 need_symtab = (bfd_get_symcount (abfd) > 0
2839 || (link_info == NULL
2840 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
2841 == HAS_RELOC)));
2842 if (need_symtab)
2844 t->symtab_section = section_number++;
2845 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2846 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2848 t->symtab_shndx_section = section_number++;
2849 t->symtab_shndx_hdr.sh_name
2850 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2851 ".symtab_shndx", FALSE);
2852 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2853 return FALSE;
2855 t->strtab_section = section_number++;
2856 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2859 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2860 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2862 elf_numsections (abfd) = section_number;
2863 elf_elfheader (abfd)->e_shnum = section_number;
2865 /* Set up the list of section header pointers, in agreement with the
2866 indices. */
2867 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
2868 sizeof (Elf_Internal_Shdr *));
2869 if (i_shdrp == NULL)
2870 return FALSE;
2872 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
2873 sizeof (Elf_Internal_Shdr));
2874 if (i_shdrp[0] == NULL)
2876 bfd_release (abfd, i_shdrp);
2877 return FALSE;
2880 elf_elfsections (abfd) = i_shdrp;
2882 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2883 if (need_symtab)
2885 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2886 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2888 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2889 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2891 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2892 t->symtab_hdr.sh_link = t->strtab_section;
2895 for (sec = abfd->sections; sec; sec = sec->next)
2897 struct bfd_elf_section_data *d = elf_section_data (sec);
2898 asection *s;
2899 const char *name;
2901 i_shdrp[d->this_idx] = &d->this_hdr;
2902 if (d->rel_idx != 0)
2903 i_shdrp[d->rel_idx] = &d->rel_hdr;
2904 if (d->rel_idx2 != 0)
2905 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2907 /* Fill in the sh_link and sh_info fields while we're at it. */
2909 /* sh_link of a reloc section is the section index of the symbol
2910 table. sh_info is the section index of the section to which
2911 the relocation entries apply. */
2912 if (d->rel_idx != 0)
2914 d->rel_hdr.sh_link = t->symtab_section;
2915 d->rel_hdr.sh_info = d->this_idx;
2917 if (d->rel_idx2 != 0)
2919 d->rel_hdr2->sh_link = t->symtab_section;
2920 d->rel_hdr2->sh_info = d->this_idx;
2923 /* We need to set up sh_link for SHF_LINK_ORDER. */
2924 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2926 s = elf_linked_to_section (sec);
2927 if (s)
2929 /* elf_linked_to_section points to the input section. */
2930 if (link_info != NULL)
2932 /* Check discarded linkonce section. */
2933 if (elf_discarded_section (s))
2935 asection *kept;
2936 (*_bfd_error_handler)
2937 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2938 abfd, d->this_hdr.bfd_section,
2939 s, s->owner);
2940 /* Point to the kept section if it has the same
2941 size as the discarded one. */
2942 kept = _bfd_elf_check_kept_section (s, link_info);
2943 if (kept == NULL)
2945 bfd_set_error (bfd_error_bad_value);
2946 return FALSE;
2948 s = kept;
2951 s = s->output_section;
2952 BFD_ASSERT (s != NULL);
2954 else
2956 /* Handle objcopy. */
2957 if (s->output_section == NULL)
2959 (*_bfd_error_handler)
2960 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2961 abfd, d->this_hdr.bfd_section, s, s->owner);
2962 bfd_set_error (bfd_error_bad_value);
2963 return FALSE;
2965 s = s->output_section;
2967 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2969 else
2971 /* PR 290:
2972 The Intel C compiler generates SHT_IA_64_UNWIND with
2973 SHF_LINK_ORDER. But it doesn't set the sh_link or
2974 sh_info fields. Hence we could get the situation
2975 where s is NULL. */
2976 const struct elf_backend_data *bed
2977 = get_elf_backend_data (abfd);
2978 if (bed->link_order_error_handler)
2979 bed->link_order_error_handler
2980 (_("%B: warning: sh_link not set for section `%A'"),
2981 abfd, sec);
2985 switch (d->this_hdr.sh_type)
2987 case SHT_REL:
2988 case SHT_RELA:
2989 /* A reloc section which we are treating as a normal BFD
2990 section. sh_link is the section index of the symbol
2991 table. sh_info is the section index of the section to
2992 which the relocation entries apply. We assume that an
2993 allocated reloc section uses the dynamic symbol table.
2994 FIXME: How can we be sure? */
2995 s = bfd_get_section_by_name (abfd, ".dynsym");
2996 if (s != NULL)
2997 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2999 /* We look up the section the relocs apply to by name. */
3000 name = sec->name;
3001 if (d->this_hdr.sh_type == SHT_REL)
3002 name += 4;
3003 else
3004 name += 5;
3005 s = bfd_get_section_by_name (abfd, name);
3006 if (s != NULL)
3007 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3008 break;
3010 case SHT_STRTAB:
3011 /* We assume that a section named .stab*str is a stabs
3012 string section. We look for a section with the same name
3013 but without the trailing ``str'', and set its sh_link
3014 field to point to this section. */
3015 if (CONST_STRNEQ (sec->name, ".stab")
3016 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3018 size_t len;
3019 char *alc;
3021 len = strlen (sec->name);
3022 alc = (char *) bfd_malloc (len - 2);
3023 if (alc == NULL)
3024 return FALSE;
3025 memcpy (alc, sec->name, len - 3);
3026 alc[len - 3] = '\0';
3027 s = bfd_get_section_by_name (abfd, alc);
3028 free (alc);
3029 if (s != NULL)
3031 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3033 /* This is a .stab section. */
3034 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3035 elf_section_data (s)->this_hdr.sh_entsize
3036 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3039 break;
3041 case SHT_DYNAMIC:
3042 case SHT_DYNSYM:
3043 case SHT_GNU_verneed:
3044 case SHT_GNU_verdef:
3045 /* sh_link is the section header index of the string table
3046 used for the dynamic entries, or the symbol table, or the
3047 version strings. */
3048 s = bfd_get_section_by_name (abfd, ".dynstr");
3049 if (s != NULL)
3050 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3051 break;
3053 case SHT_GNU_LIBLIST:
3054 /* sh_link is the section header index of the prelink library
3055 list used for the dynamic entries, or the symbol table, or
3056 the version strings. */
3057 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3058 ? ".dynstr" : ".gnu.libstr");
3059 if (s != NULL)
3060 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3061 break;
3063 case SHT_HASH:
3064 case SHT_GNU_HASH:
3065 case SHT_GNU_versym:
3066 /* sh_link is the section header index of the symbol table
3067 this hash table or version table is for. */
3068 s = bfd_get_section_by_name (abfd, ".dynsym");
3069 if (s != NULL)
3070 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3071 break;
3073 case SHT_GROUP:
3074 d->this_hdr.sh_link = t->symtab_section;
3078 for (secn = 1; secn < section_number; ++secn)
3079 if (i_shdrp[secn] == NULL)
3080 i_shdrp[secn] = i_shdrp[0];
3081 else
3082 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3083 i_shdrp[secn]->sh_name);
3084 return TRUE;
3087 /* Map symbol from it's internal number to the external number, moving
3088 all local symbols to be at the head of the list. */
3090 static bfd_boolean
3091 sym_is_global (bfd *abfd, asymbol *sym)
3093 /* If the backend has a special mapping, use it. */
3094 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3095 if (bed->elf_backend_sym_is_global)
3096 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3098 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3099 || bfd_is_und_section (bfd_get_section (sym))
3100 || bfd_is_com_section (bfd_get_section (sym)));
3103 /* Don't output section symbols for sections that are not going to be
3104 output. */
3106 static bfd_boolean
3107 ignore_section_sym (bfd *abfd, asymbol *sym)
3109 return ((sym->flags & BSF_SECTION_SYM) != 0
3110 && !(sym->section->owner == abfd
3111 || (sym->section->output_section->owner == abfd
3112 && sym->section->output_offset == 0)));
3115 static bfd_boolean
3116 elf_map_symbols (bfd *abfd)
3118 unsigned int symcount = bfd_get_symcount (abfd);
3119 asymbol **syms = bfd_get_outsymbols (abfd);
3120 asymbol **sect_syms;
3121 unsigned int num_locals = 0;
3122 unsigned int num_globals = 0;
3123 unsigned int num_locals2 = 0;
3124 unsigned int num_globals2 = 0;
3125 int max_index = 0;
3126 unsigned int idx;
3127 asection *asect;
3128 asymbol **new_syms;
3130 #ifdef DEBUG
3131 fprintf (stderr, "elf_map_symbols\n");
3132 fflush (stderr);
3133 #endif
3135 for (asect = abfd->sections; asect; asect = asect->next)
3137 if (max_index < asect->index)
3138 max_index = asect->index;
3141 max_index++;
3142 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3143 if (sect_syms == NULL)
3144 return FALSE;
3145 elf_section_syms (abfd) = sect_syms;
3146 elf_num_section_syms (abfd) = max_index;
3148 /* Init sect_syms entries for any section symbols we have already
3149 decided to output. */
3150 for (idx = 0; idx < symcount; idx++)
3152 asymbol *sym = syms[idx];
3154 if ((sym->flags & BSF_SECTION_SYM) != 0
3155 && sym->value == 0
3156 && !ignore_section_sym (abfd, sym))
3158 asection *sec = sym->section;
3160 if (sec->owner != abfd)
3161 sec = sec->output_section;
3163 sect_syms[sec->index] = syms[idx];
3167 /* Classify all of the symbols. */
3168 for (idx = 0; idx < symcount; idx++)
3170 if (ignore_section_sym (abfd, syms[idx]))
3171 continue;
3172 if (!sym_is_global (abfd, syms[idx]))
3173 num_locals++;
3174 else
3175 num_globals++;
3178 /* We will be adding a section symbol for each normal BFD section. Most
3179 sections will already have a section symbol in outsymbols, but
3180 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3181 at least in that case. */
3182 for (asect = abfd->sections; asect; asect = asect->next)
3184 if (sect_syms[asect->index] == NULL)
3186 if (!sym_is_global (abfd, asect->symbol))
3187 num_locals++;
3188 else
3189 num_globals++;
3193 /* Now sort the symbols so the local symbols are first. */
3194 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3195 sizeof (asymbol *));
3197 if (new_syms == NULL)
3198 return FALSE;
3200 for (idx = 0; idx < symcount; idx++)
3202 asymbol *sym = syms[idx];
3203 unsigned int i;
3205 if (ignore_section_sym (abfd, sym))
3206 continue;
3207 if (!sym_is_global (abfd, sym))
3208 i = num_locals2++;
3209 else
3210 i = num_locals + num_globals2++;
3211 new_syms[i] = sym;
3212 sym->udata.i = i + 1;
3214 for (asect = abfd->sections; asect; asect = asect->next)
3216 if (sect_syms[asect->index] == NULL)
3218 asymbol *sym = asect->symbol;
3219 unsigned int i;
3221 sect_syms[asect->index] = sym;
3222 if (!sym_is_global (abfd, sym))
3223 i = num_locals2++;
3224 else
3225 i = num_locals + num_globals2++;
3226 new_syms[i] = sym;
3227 sym->udata.i = i + 1;
3231 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3233 elf_num_locals (abfd) = num_locals;
3234 elf_num_globals (abfd) = num_globals;
3235 return TRUE;
3238 /* Align to the maximum file alignment that could be required for any
3239 ELF data structure. */
3241 static inline file_ptr
3242 align_file_position (file_ptr off, int align)
3244 return (off + align - 1) & ~(align - 1);
3247 /* Assign a file position to a section, optionally aligning to the
3248 required section alignment. */
3250 file_ptr
3251 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3252 file_ptr offset,
3253 bfd_boolean align)
3255 if (align && i_shdrp->sh_addralign > 1)
3256 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3257 i_shdrp->sh_offset = offset;
3258 if (i_shdrp->bfd_section != NULL)
3259 i_shdrp->bfd_section->filepos = offset;
3260 if (i_shdrp->sh_type != SHT_NOBITS)
3261 offset += i_shdrp->sh_size;
3262 return offset;
3265 /* Compute the file positions we are going to put the sections at, and
3266 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3267 is not NULL, this is being called by the ELF backend linker. */
3269 bfd_boolean
3270 _bfd_elf_compute_section_file_positions (bfd *abfd,
3271 struct bfd_link_info *link_info)
3273 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3274 bfd_boolean failed;
3275 struct bfd_strtab_hash *strtab = NULL;
3276 Elf_Internal_Shdr *shstrtab_hdr;
3277 bfd_boolean need_symtab;
3279 if (abfd->output_has_begun)
3280 return TRUE;
3282 /* Do any elf backend specific processing first. */
3283 if (bed->elf_backend_begin_write_processing)
3284 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3286 if (! prep_headers (abfd))
3287 return FALSE;
3289 /* Post process the headers if necessary. */
3290 if (bed->elf_backend_post_process_headers)
3291 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3293 failed = FALSE;
3294 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3295 if (failed)
3296 return FALSE;
3298 if (!assign_section_numbers (abfd, link_info))
3299 return FALSE;
3301 /* The backend linker builds symbol table information itself. */
3302 need_symtab = (link_info == NULL
3303 && (bfd_get_symcount (abfd) > 0
3304 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3305 == HAS_RELOC)));
3306 if (need_symtab)
3308 /* Non-zero if doing a relocatable link. */
3309 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3311 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3312 return FALSE;
3315 if (link_info == NULL)
3317 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3318 if (failed)
3319 return FALSE;
3322 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3323 /* sh_name was set in prep_headers. */
3324 shstrtab_hdr->sh_type = SHT_STRTAB;
3325 shstrtab_hdr->sh_flags = 0;
3326 shstrtab_hdr->sh_addr = 0;
3327 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3328 shstrtab_hdr->sh_entsize = 0;
3329 shstrtab_hdr->sh_link = 0;
3330 shstrtab_hdr->sh_info = 0;
3331 /* sh_offset is set in assign_file_positions_except_relocs. */
3332 shstrtab_hdr->sh_addralign = 1;
3334 if (!assign_file_positions_except_relocs (abfd, link_info))
3335 return FALSE;
3337 if (need_symtab)
3339 file_ptr off;
3340 Elf_Internal_Shdr *hdr;
3342 off = elf_tdata (abfd)->next_file_pos;
3344 hdr = &elf_tdata (abfd)->symtab_hdr;
3345 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3347 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3348 if (hdr->sh_size != 0)
3349 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3351 hdr = &elf_tdata (abfd)->strtab_hdr;
3352 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3354 elf_tdata (abfd)->next_file_pos = off;
3356 /* Now that we know where the .strtab section goes, write it
3357 out. */
3358 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3359 || ! _bfd_stringtab_emit (abfd, strtab))
3360 return FALSE;
3361 _bfd_stringtab_free (strtab);
3364 abfd->output_has_begun = TRUE;
3366 return TRUE;
3369 /* Make an initial estimate of the size of the program header. If we
3370 get the number wrong here, we'll redo section placement. */
3372 static bfd_size_type
3373 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3375 size_t segs;
3376 asection *s;
3377 const struct elf_backend_data *bed;
3379 /* Assume we will need exactly two PT_LOAD segments: one for text
3380 and one for data. */
3381 segs = 2;
3383 s = bfd_get_section_by_name (abfd, ".interp");
3384 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3386 /* If we have a loadable interpreter section, we need a
3387 PT_INTERP segment. In this case, assume we also need a
3388 PT_PHDR segment, although that may not be true for all
3389 targets. */
3390 segs += 2;
3393 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3395 /* We need a PT_DYNAMIC segment. */
3396 ++segs;
3399 if (info != NULL && info->relro)
3401 /* We need a PT_GNU_RELRO segment. */
3402 ++segs;
3405 if (elf_tdata (abfd)->eh_frame_hdr)
3407 /* We need a PT_GNU_EH_FRAME segment. */
3408 ++segs;
3411 if (elf_tdata (abfd)->stack_flags)
3413 /* We need a PT_GNU_STACK segment. */
3414 ++segs;
3417 for (s = abfd->sections; s != NULL; s = s->next)
3419 if ((s->flags & SEC_LOAD) != 0
3420 && CONST_STRNEQ (s->name, ".note"))
3422 /* We need a PT_NOTE segment. */
3423 ++segs;
3424 /* Try to create just one PT_NOTE segment
3425 for all adjacent loadable .note* sections.
3426 gABI requires that within a PT_NOTE segment
3427 (and also inside of each SHT_NOTE section)
3428 each note is padded to a multiple of 4 size,
3429 so we check whether the sections are correctly
3430 aligned. */
3431 if (s->alignment_power == 2)
3432 while (s->next != NULL
3433 && s->next->alignment_power == 2
3434 && (s->next->flags & SEC_LOAD) != 0
3435 && CONST_STRNEQ (s->next->name, ".note"))
3436 s = s->next;
3440 for (s = abfd->sections; s != NULL; s = s->next)
3442 if (s->flags & SEC_THREAD_LOCAL)
3444 /* We need a PT_TLS segment. */
3445 ++segs;
3446 break;
3450 /* Let the backend count up any program headers it might need. */
3451 bed = get_elf_backend_data (abfd);
3452 if (bed->elf_backend_additional_program_headers)
3454 int a;
3456 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3457 if (a == -1)
3458 abort ();
3459 segs += a;
3462 return segs * bed->s->sizeof_phdr;
3465 /* Find the segment that contains the output_section of section. */
3467 Elf_Internal_Phdr *
3468 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3470 struct elf_segment_map *m;
3471 Elf_Internal_Phdr *p;
3473 for (m = elf_tdata (abfd)->segment_map,
3474 p = elf_tdata (abfd)->phdr;
3475 m != NULL;
3476 m = m->next, p++)
3478 int i;
3480 for (i = m->count - 1; i >= 0; i--)
3481 if (m->sections[i] == section)
3482 return p;
3485 return NULL;
3488 /* Create a mapping from a set of sections to a program segment. */
3490 static struct elf_segment_map *
3491 make_mapping (bfd *abfd,
3492 asection **sections,
3493 unsigned int from,
3494 unsigned int to,
3495 bfd_boolean phdr)
3497 struct elf_segment_map *m;
3498 unsigned int i;
3499 asection **hdrpp;
3500 bfd_size_type amt;
3502 amt = sizeof (struct elf_segment_map);
3503 amt += (to - from - 1) * sizeof (asection *);
3504 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3505 if (m == NULL)
3506 return NULL;
3507 m->next = NULL;
3508 m->p_type = PT_LOAD;
3509 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3510 m->sections[i - from] = *hdrpp;
3511 m->count = to - from;
3513 if (from == 0 && phdr)
3515 /* Include the headers in the first PT_LOAD segment. */
3516 m->includes_filehdr = 1;
3517 m->includes_phdrs = 1;
3520 return m;
3523 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3524 on failure. */
3526 struct elf_segment_map *
3527 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3529 struct elf_segment_map *m;
3531 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3532 sizeof (struct elf_segment_map));
3533 if (m == NULL)
3534 return NULL;
3535 m->next = NULL;
3536 m->p_type = PT_DYNAMIC;
3537 m->count = 1;
3538 m->sections[0] = dynsec;
3540 return m;
3543 /* Possibly add or remove segments from the segment map. */
3545 static bfd_boolean
3546 elf_modify_segment_map (bfd *abfd,
3547 struct bfd_link_info *info,
3548 bfd_boolean remove_empty_load)
3550 struct elf_segment_map **m;
3551 const struct elf_backend_data *bed;
3553 /* The placement algorithm assumes that non allocated sections are
3554 not in PT_LOAD segments. We ensure this here by removing such
3555 sections from the segment map. We also remove excluded
3556 sections. Finally, any PT_LOAD segment without sections is
3557 removed. */
3558 m = &elf_tdata (abfd)->segment_map;
3559 while (*m)
3561 unsigned int i, new_count;
3563 for (new_count = 0, i = 0; i < (*m)->count; i++)
3565 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3566 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3567 || (*m)->p_type != PT_LOAD))
3569 (*m)->sections[new_count] = (*m)->sections[i];
3570 new_count++;
3573 (*m)->count = new_count;
3575 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3576 *m = (*m)->next;
3577 else
3578 m = &(*m)->next;
3581 bed = get_elf_backend_data (abfd);
3582 if (bed->elf_backend_modify_segment_map != NULL)
3584 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3585 return FALSE;
3588 return TRUE;
3591 /* Set up a mapping from BFD sections to program segments. */
3593 bfd_boolean
3594 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3596 unsigned int count;
3597 struct elf_segment_map *m;
3598 asection **sections = NULL;
3599 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3600 bfd_boolean no_user_phdrs;
3602 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3603 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3605 asection *s;
3606 unsigned int i;
3607 struct elf_segment_map *mfirst;
3608 struct elf_segment_map **pm;
3609 asection *last_hdr;
3610 bfd_vma last_size;
3611 unsigned int phdr_index;
3612 bfd_vma maxpagesize;
3613 asection **hdrpp;
3614 bfd_boolean phdr_in_segment = TRUE;
3615 bfd_boolean writable;
3616 int tls_count = 0;
3617 asection *first_tls = NULL;
3618 asection *dynsec, *eh_frame_hdr;
3619 bfd_size_type amt;
3621 /* Select the allocated sections, and sort them. */
3623 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3624 sizeof (asection *));
3625 if (sections == NULL)
3626 goto error_return;
3628 i = 0;
3629 for (s = abfd->sections; s != NULL; s = s->next)
3631 if ((s->flags & SEC_ALLOC) != 0)
3633 sections[i] = s;
3634 ++i;
3637 BFD_ASSERT (i <= bfd_count_sections (abfd));
3638 count = i;
3640 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3642 /* Build the mapping. */
3644 mfirst = NULL;
3645 pm = &mfirst;
3647 /* If we have a .interp section, then create a PT_PHDR segment for
3648 the program headers and a PT_INTERP segment for the .interp
3649 section. */
3650 s = bfd_get_section_by_name (abfd, ".interp");
3651 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3653 amt = sizeof (struct elf_segment_map);
3654 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3655 if (m == NULL)
3656 goto error_return;
3657 m->next = NULL;
3658 m->p_type = PT_PHDR;
3659 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3660 m->p_flags = PF_R | PF_X;
3661 m->p_flags_valid = 1;
3662 m->includes_phdrs = 1;
3664 *pm = m;
3665 pm = &m->next;
3667 amt = sizeof (struct elf_segment_map);
3668 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3669 if (m == NULL)
3670 goto error_return;
3671 m->next = NULL;
3672 m->p_type = PT_INTERP;
3673 m->count = 1;
3674 m->sections[0] = s;
3676 *pm = m;
3677 pm = &m->next;
3680 /* Look through the sections. We put sections in the same program
3681 segment when the start of the second section can be placed within
3682 a few bytes of the end of the first section. */
3683 last_hdr = NULL;
3684 last_size = 0;
3685 phdr_index = 0;
3686 maxpagesize = bed->maxpagesize;
3687 writable = FALSE;
3688 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3689 if (dynsec != NULL
3690 && (dynsec->flags & SEC_LOAD) == 0)
3691 dynsec = NULL;
3693 /* Deal with -Ttext or something similar such that the first section
3694 is not adjacent to the program headers. This is an
3695 approximation, since at this point we don't know exactly how many
3696 program headers we will need. */
3697 if (count > 0)
3699 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3701 if (phdr_size == (bfd_size_type) -1)
3702 phdr_size = get_program_header_size (abfd, info);
3703 if ((abfd->flags & D_PAGED) == 0
3704 || sections[0]->lma < phdr_size
3705 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3706 phdr_in_segment = FALSE;
3709 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3711 asection *hdr;
3712 bfd_boolean new_segment;
3714 hdr = *hdrpp;
3716 /* See if this section and the last one will fit in the same
3717 segment. */
3719 if (last_hdr == NULL)
3721 /* If we don't have a segment yet, then we don't need a new
3722 one (we build the last one after this loop). */
3723 new_segment = FALSE;
3725 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3727 /* If this section has a different relation between the
3728 virtual address and the load address, then we need a new
3729 segment. */
3730 new_segment = TRUE;
3732 /* In the next test we have to be careful when last_hdr->lma is close
3733 to the end of the address space. If the aligned address wraps
3734 around to the start of the address space, then there are no more
3735 pages left in memory and it is OK to assume that the current
3736 section can be included in the current segment. */
3737 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3738 > last_hdr->lma)
3739 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3740 <= hdr->lma))
3742 /* If putting this section in this segment would force us to
3743 skip a page in the segment, then we need a new segment. */
3744 new_segment = TRUE;
3746 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3747 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3749 /* We don't want to put a loadable section after a
3750 nonloadable section in the same segment.
3751 Consider .tbss sections as loadable for this purpose. */
3752 new_segment = TRUE;
3754 else if ((abfd->flags & D_PAGED) == 0)
3756 /* If the file is not demand paged, which means that we
3757 don't require the sections to be correctly aligned in the
3758 file, then there is no other reason for a new segment. */
3759 new_segment = FALSE;
3761 else if (! writable
3762 && (hdr->flags & SEC_READONLY) == 0
3763 && (((last_hdr->lma + last_size - 1)
3764 & ~(maxpagesize - 1))
3765 != (hdr->lma & ~(maxpagesize - 1))))
3767 /* We don't want to put a writable section in a read only
3768 segment, unless they are on the same page in memory
3769 anyhow. We already know that the last section does not
3770 bring us past the current section on the page, so the
3771 only case in which the new section is not on the same
3772 page as the previous section is when the previous section
3773 ends precisely on a page boundary. */
3774 new_segment = TRUE;
3776 else
3778 /* Otherwise, we can use the same segment. */
3779 new_segment = FALSE;
3782 /* Allow interested parties a chance to override our decision. */
3783 if (last_hdr != NULL
3784 && info != NULL
3785 && info->callbacks->override_segment_assignment != NULL)
3786 new_segment
3787 = info->callbacks->override_segment_assignment (info, abfd, hdr,
3788 last_hdr,
3789 new_segment);
3791 if (! new_segment)
3793 if ((hdr->flags & SEC_READONLY) == 0)
3794 writable = TRUE;
3795 last_hdr = hdr;
3796 /* .tbss sections effectively have zero size. */
3797 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3798 != SEC_THREAD_LOCAL)
3799 last_size = hdr->size;
3800 else
3801 last_size = 0;
3802 continue;
3805 /* We need a new program segment. We must create a new program
3806 header holding all the sections from phdr_index until hdr. */
3808 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3809 if (m == NULL)
3810 goto error_return;
3812 *pm = m;
3813 pm = &m->next;
3815 if ((hdr->flags & SEC_READONLY) == 0)
3816 writable = TRUE;
3817 else
3818 writable = FALSE;
3820 last_hdr = hdr;
3821 /* .tbss sections effectively have zero size. */
3822 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3823 last_size = hdr->size;
3824 else
3825 last_size = 0;
3826 phdr_index = i;
3827 phdr_in_segment = FALSE;
3830 /* Create a final PT_LOAD program segment. */
3831 if (last_hdr != NULL)
3833 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3834 if (m == NULL)
3835 goto error_return;
3837 *pm = m;
3838 pm = &m->next;
3841 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3842 if (dynsec != NULL)
3844 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3845 if (m == NULL)
3846 goto error_return;
3847 *pm = m;
3848 pm = &m->next;
3851 /* For each batch of consecutive loadable .note sections,
3852 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3853 because if we link together nonloadable .note sections and
3854 loadable .note sections, we will generate two .note sections
3855 in the output file. FIXME: Using names for section types is
3856 bogus anyhow. */
3857 for (s = abfd->sections; s != NULL; s = s->next)
3859 if ((s->flags & SEC_LOAD) != 0
3860 && CONST_STRNEQ (s->name, ".note"))
3862 asection *s2;
3863 unsigned count = 1;
3864 amt = sizeof (struct elf_segment_map);
3865 if (s->alignment_power == 2)
3866 for (s2 = s; s2->next != NULL; s2 = s2->next)
3868 if (s2->next->alignment_power == 2
3869 && (s2->next->flags & SEC_LOAD) != 0
3870 && CONST_STRNEQ (s2->next->name, ".note")
3871 && align_power (s2->vma + s2->size, 2)
3872 == s2->next->vma)
3873 count++;
3874 else
3875 break;
3877 amt += (count - 1) * sizeof (asection *);
3878 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3879 if (m == NULL)
3880 goto error_return;
3881 m->next = NULL;
3882 m->p_type = PT_NOTE;
3883 m->count = count;
3884 while (count > 1)
3886 m->sections[m->count - count--] = s;
3887 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3888 s = s->next;
3890 m->sections[m->count - 1] = s;
3891 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3892 *pm = m;
3893 pm = &m->next;
3895 if (s->flags & SEC_THREAD_LOCAL)
3897 if (! tls_count)
3898 first_tls = s;
3899 tls_count++;
3903 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3904 if (tls_count > 0)
3906 int i;
3908 amt = sizeof (struct elf_segment_map);
3909 amt += (tls_count - 1) * sizeof (asection *);
3910 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3911 if (m == NULL)
3912 goto error_return;
3913 m->next = NULL;
3914 m->p_type = PT_TLS;
3915 m->count = tls_count;
3916 /* Mandated PF_R. */
3917 m->p_flags = PF_R;
3918 m->p_flags_valid = 1;
3919 for (i = 0; i < tls_count; ++i)
3921 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3922 m->sections[i] = first_tls;
3923 first_tls = first_tls->next;
3926 *pm = m;
3927 pm = &m->next;
3930 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3931 segment. */
3932 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3933 if (eh_frame_hdr != NULL
3934 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3936 amt = sizeof (struct elf_segment_map);
3937 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3938 if (m == NULL)
3939 goto error_return;
3940 m->next = NULL;
3941 m->p_type = PT_GNU_EH_FRAME;
3942 m->count = 1;
3943 m->sections[0] = eh_frame_hdr->output_section;
3945 *pm = m;
3946 pm = &m->next;
3949 if (elf_tdata (abfd)->stack_flags)
3951 amt = sizeof (struct elf_segment_map);
3952 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3953 if (m == NULL)
3954 goto error_return;
3955 m->next = NULL;
3956 m->p_type = PT_GNU_STACK;
3957 m->p_flags = elf_tdata (abfd)->stack_flags;
3958 m->p_flags_valid = 1;
3960 *pm = m;
3961 pm = &m->next;
3964 if (info != NULL && info->relro)
3966 for (m = mfirst; m != NULL; m = m->next)
3968 if (m->p_type == PT_LOAD)
3970 asection *last = m->sections[m->count - 1];
3971 bfd_vma vaddr = m->sections[0]->vma;
3972 bfd_vma filesz = last->vma - vaddr + last->size;
3974 if (vaddr < info->relro_end
3975 && vaddr >= info->relro_start
3976 && (vaddr + filesz) >= info->relro_end)
3977 break;
3981 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3982 if (m != NULL)
3984 amt = sizeof (struct elf_segment_map);
3985 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3986 if (m == NULL)
3987 goto error_return;
3988 m->next = NULL;
3989 m->p_type = PT_GNU_RELRO;
3990 m->p_flags = PF_R;
3991 m->p_flags_valid = 1;
3993 *pm = m;
3994 pm = &m->next;
3998 free (sections);
3999 elf_tdata (abfd)->segment_map = mfirst;
4002 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4003 return FALSE;
4005 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4006 ++count;
4007 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
4009 return TRUE;
4011 error_return:
4012 if (sections != NULL)
4013 free (sections);
4014 return FALSE;
4017 /* Sort sections by address. */
4019 static int
4020 elf_sort_sections (const void *arg1, const void *arg2)
4022 const asection *sec1 = *(const asection **) arg1;
4023 const asection *sec2 = *(const asection **) arg2;
4024 bfd_size_type size1, size2;
4026 /* Sort by LMA first, since this is the address used to
4027 place the section into a segment. */
4028 if (sec1->lma < sec2->lma)
4029 return -1;
4030 else if (sec1->lma > sec2->lma)
4031 return 1;
4033 /* Then sort by VMA. Normally the LMA and the VMA will be
4034 the same, and this will do nothing. */
4035 if (sec1->vma < sec2->vma)
4036 return -1;
4037 else if (sec1->vma > sec2->vma)
4038 return 1;
4040 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4042 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4044 if (TOEND (sec1))
4046 if (TOEND (sec2))
4048 /* If the indicies are the same, do not return 0
4049 here, but continue to try the next comparison. */
4050 if (sec1->target_index - sec2->target_index != 0)
4051 return sec1->target_index - sec2->target_index;
4053 else
4054 return 1;
4056 else if (TOEND (sec2))
4057 return -1;
4059 #undef TOEND
4061 /* Sort by size, to put zero sized sections
4062 before others at the same address. */
4064 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4065 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4067 if (size1 < size2)
4068 return -1;
4069 if (size1 > size2)
4070 return 1;
4072 return sec1->target_index - sec2->target_index;
4075 /* Ian Lance Taylor writes:
4077 We shouldn't be using % with a negative signed number. That's just
4078 not good. We have to make sure either that the number is not
4079 negative, or that the number has an unsigned type. When the types
4080 are all the same size they wind up as unsigned. When file_ptr is a
4081 larger signed type, the arithmetic winds up as signed long long,
4082 which is wrong.
4084 What we're trying to say here is something like ``increase OFF by
4085 the least amount that will cause it to be equal to the VMA modulo
4086 the page size.'' */
4087 /* In other words, something like:
4089 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4090 off_offset = off % bed->maxpagesize;
4091 if (vma_offset < off_offset)
4092 adjustment = vma_offset + bed->maxpagesize - off_offset;
4093 else
4094 adjustment = vma_offset - off_offset;
4096 which can can be collapsed into the expression below. */
4098 static file_ptr
4099 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4101 return ((vma - off) % maxpagesize);
4104 static void
4105 print_segment_map (const struct elf_segment_map *m)
4107 unsigned int j;
4108 const char *pt = get_segment_type (m->p_type);
4109 char buf[32];
4111 if (pt == NULL)
4113 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4114 sprintf (buf, "LOPROC+%7.7x",
4115 (unsigned int) (m->p_type - PT_LOPROC));
4116 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4117 sprintf (buf, "LOOS+%7.7x",
4118 (unsigned int) (m->p_type - PT_LOOS));
4119 else
4120 snprintf (buf, sizeof (buf), "%8.8x",
4121 (unsigned int) m->p_type);
4122 pt = buf;
4124 fprintf (stderr, "%s:", pt);
4125 for (j = 0; j < m->count; j++)
4126 fprintf (stderr, " %s", m->sections [j]->name);
4127 putc ('\n',stderr);
4130 /* Assign file positions to the sections based on the mapping from
4131 sections to segments. This function also sets up some fields in
4132 the file header. */
4134 static bfd_boolean
4135 assign_file_positions_for_load_sections (bfd *abfd,
4136 struct bfd_link_info *link_info)
4138 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4139 struct elf_segment_map *m;
4140 Elf_Internal_Phdr *phdrs;
4141 Elf_Internal_Phdr *p;
4142 file_ptr off;
4143 bfd_size_type maxpagesize;
4144 unsigned int alloc;
4145 unsigned int i, j;
4146 bfd_vma header_pad = 0;
4148 if (link_info == NULL
4149 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4150 return FALSE;
4152 alloc = 0;
4153 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4155 ++alloc;
4156 if (m->header_size)
4157 header_pad = m->header_size;
4160 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4161 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4162 elf_elfheader (abfd)->e_phnum = alloc;
4164 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4165 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4166 else
4167 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4168 >= alloc * bed->s->sizeof_phdr);
4170 if (alloc == 0)
4172 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4173 return TRUE;
4176 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4177 see assign_file_positions_except_relocs, so make sure we have
4178 that amount allocated, with trailing space cleared.
4179 The variable alloc contains the computed need, while elf_tdata
4180 (abfd)->program_header_size contains the size used for the
4181 layout.
4182 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4183 where the layout is forced to according to a larger size in the
4184 last iterations for the testcase ld-elf/header. */
4185 BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr
4186 == 0);
4187 phdrs = (Elf_Internal_Phdr *)
4188 bfd_zalloc2 (abfd,
4189 (elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr),
4190 sizeof (Elf_Internal_Phdr));
4191 elf_tdata (abfd)->phdr = phdrs;
4192 if (phdrs == NULL)
4193 return FALSE;
4195 maxpagesize = 1;
4196 if ((abfd->flags & D_PAGED) != 0)
4197 maxpagesize = bed->maxpagesize;
4199 off = bed->s->sizeof_ehdr;
4200 off += alloc * bed->s->sizeof_phdr;
4201 if (header_pad < (bfd_vma) off)
4202 header_pad = 0;
4203 else
4204 header_pad -= off;
4205 off += header_pad;
4207 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4208 m != NULL;
4209 m = m->next, p++, j++)
4211 asection **secpp;
4212 bfd_vma off_adjust;
4213 bfd_boolean no_contents;
4215 /* If elf_segment_map is not from map_sections_to_segments, the
4216 sections may not be correctly ordered. NOTE: sorting should
4217 not be done to the PT_NOTE section of a corefile, which may
4218 contain several pseudo-sections artificially created by bfd.
4219 Sorting these pseudo-sections breaks things badly. */
4220 if (m->count > 1
4221 && !(elf_elfheader (abfd)->e_type == ET_CORE
4222 && m->p_type == PT_NOTE))
4223 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4224 elf_sort_sections);
4226 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4227 number of sections with contents contributing to both p_filesz
4228 and p_memsz, followed by a number of sections with no contents
4229 that just contribute to p_memsz. In this loop, OFF tracks next
4230 available file offset for PT_LOAD and PT_NOTE segments. */
4231 p->p_type = m->p_type;
4232 p->p_flags = m->p_flags;
4234 if (m->count == 0)
4235 p->p_vaddr = 0;
4236 else
4237 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4239 if (m->p_paddr_valid)
4240 p->p_paddr = m->p_paddr;
4241 else if (m->count == 0)
4242 p->p_paddr = 0;
4243 else
4244 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4246 if (p->p_type == PT_LOAD
4247 && (abfd->flags & D_PAGED) != 0)
4249 /* p_align in demand paged PT_LOAD segments effectively stores
4250 the maximum page size. When copying an executable with
4251 objcopy, we set m->p_align from the input file. Use this
4252 value for maxpagesize rather than bed->maxpagesize, which
4253 may be different. Note that we use maxpagesize for PT_TLS
4254 segment alignment later in this function, so we are relying
4255 on at least one PT_LOAD segment appearing before a PT_TLS
4256 segment. */
4257 if (m->p_align_valid)
4258 maxpagesize = m->p_align;
4260 p->p_align = maxpagesize;
4262 else if (m->p_align_valid)
4263 p->p_align = m->p_align;
4264 else if (m->count == 0)
4265 p->p_align = 1 << bed->s->log_file_align;
4266 else
4267 p->p_align = 0;
4269 no_contents = FALSE;
4270 off_adjust = 0;
4271 if (p->p_type == PT_LOAD
4272 && m->count > 0)
4274 bfd_size_type align;
4275 unsigned int align_power = 0;
4277 if (m->p_align_valid)
4278 align = p->p_align;
4279 else
4281 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4283 unsigned int secalign;
4285 secalign = bfd_get_section_alignment (abfd, *secpp);
4286 if (secalign > align_power)
4287 align_power = secalign;
4289 align = (bfd_size_type) 1 << align_power;
4290 if (align < maxpagesize)
4291 align = maxpagesize;
4294 for (i = 0; i < m->count; i++)
4295 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4296 /* If we aren't making room for this section, then
4297 it must be SHT_NOBITS regardless of what we've
4298 set via struct bfd_elf_special_section. */
4299 elf_section_type (m->sections[i]) = SHT_NOBITS;
4301 /* Find out whether this segment contains any loadable
4302 sections. */
4303 no_contents = TRUE;
4304 for (i = 0; i < m->count; i++)
4305 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4307 no_contents = FALSE;
4308 break;
4311 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4312 off += off_adjust;
4313 if (no_contents)
4315 /* We shouldn't need to align the segment on disk since
4316 the segment doesn't need file space, but the gABI
4317 arguably requires the alignment and glibc ld.so
4318 checks it. So to comply with the alignment
4319 requirement but not waste file space, we adjust
4320 p_offset for just this segment. (OFF_ADJUST is
4321 subtracted from OFF later.) This may put p_offset
4322 past the end of file, but that shouldn't matter. */
4324 else
4325 off_adjust = 0;
4327 /* Make sure the .dynamic section is the first section in the
4328 PT_DYNAMIC segment. */
4329 else if (p->p_type == PT_DYNAMIC
4330 && m->count > 1
4331 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4333 _bfd_error_handler
4334 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4335 abfd);
4336 bfd_set_error (bfd_error_bad_value);
4337 return FALSE;
4339 /* Set the note section type to SHT_NOTE. */
4340 else if (p->p_type == PT_NOTE)
4341 for (i = 0; i < m->count; i++)
4342 elf_section_type (m->sections[i]) = SHT_NOTE;
4344 p->p_offset = 0;
4345 p->p_filesz = 0;
4346 p->p_memsz = 0;
4348 if (m->includes_filehdr)
4350 if (!m->p_flags_valid)
4351 p->p_flags |= PF_R;
4352 p->p_filesz = bed->s->sizeof_ehdr;
4353 p->p_memsz = bed->s->sizeof_ehdr;
4354 if (m->count > 0)
4356 BFD_ASSERT (p->p_type == PT_LOAD);
4358 if (p->p_vaddr < (bfd_vma) off)
4360 (*_bfd_error_handler)
4361 (_("%B: Not enough room for program headers, try linking with -N"),
4362 abfd);
4363 bfd_set_error (bfd_error_bad_value);
4364 return FALSE;
4367 p->p_vaddr -= off;
4368 if (!m->p_paddr_valid)
4369 p->p_paddr -= off;
4373 if (m->includes_phdrs)
4375 if (!m->p_flags_valid)
4376 p->p_flags |= PF_R;
4378 if (!m->includes_filehdr)
4380 p->p_offset = bed->s->sizeof_ehdr;
4382 if (m->count > 0)
4384 BFD_ASSERT (p->p_type == PT_LOAD);
4385 p->p_vaddr -= off - p->p_offset;
4386 if (!m->p_paddr_valid)
4387 p->p_paddr -= off - p->p_offset;
4391 p->p_filesz += alloc * bed->s->sizeof_phdr;
4392 p->p_memsz += alloc * bed->s->sizeof_phdr;
4393 if (m->count)
4395 p->p_filesz += header_pad;
4396 p->p_memsz += header_pad;
4400 if (p->p_type == PT_LOAD
4401 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4403 if (!m->includes_filehdr && !m->includes_phdrs)
4404 p->p_offset = off;
4405 else
4407 file_ptr adjust;
4409 adjust = off - (p->p_offset + p->p_filesz);
4410 if (!no_contents)
4411 p->p_filesz += adjust;
4412 p->p_memsz += adjust;
4416 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4417 maps. Set filepos for sections in PT_LOAD segments, and in
4418 core files, for sections in PT_NOTE segments.
4419 assign_file_positions_for_non_load_sections will set filepos
4420 for other sections and update p_filesz for other segments. */
4421 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4423 asection *sec;
4424 bfd_size_type align;
4425 Elf_Internal_Shdr *this_hdr;
4427 sec = *secpp;
4428 this_hdr = &elf_section_data (sec)->this_hdr;
4429 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4431 if ((p->p_type == PT_LOAD
4432 || p->p_type == PT_TLS)
4433 && (this_hdr->sh_type != SHT_NOBITS
4434 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4435 && ((this_hdr->sh_flags & SHF_TLS) == 0
4436 || p->p_type == PT_TLS))))
4438 bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
4440 if (adjust < 0)
4442 (*_bfd_error_handler)
4443 (_("%B: section %A vma 0x%lx overlaps previous sections"),
4444 abfd, sec, (unsigned long) sec->vma);
4445 adjust = 0;
4447 p->p_memsz += adjust;
4449 if (this_hdr->sh_type != SHT_NOBITS)
4451 off += adjust;
4452 p->p_filesz += adjust;
4456 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4458 /* The section at i == 0 is the one that actually contains
4459 everything. */
4460 if (i == 0)
4462 this_hdr->sh_offset = sec->filepos = off;
4463 off += this_hdr->sh_size;
4464 p->p_filesz = this_hdr->sh_size;
4465 p->p_memsz = 0;
4466 p->p_align = 1;
4468 else
4470 /* The rest are fake sections that shouldn't be written. */
4471 sec->filepos = 0;
4472 sec->size = 0;
4473 sec->flags = 0;
4474 continue;
4477 else
4479 if (p->p_type == PT_LOAD)
4481 this_hdr->sh_offset = sec->filepos = off;
4482 if (this_hdr->sh_type != SHT_NOBITS)
4483 off += this_hdr->sh_size;
4486 if (this_hdr->sh_type != SHT_NOBITS)
4488 p->p_filesz += this_hdr->sh_size;
4489 /* A load section without SHF_ALLOC is something like
4490 a note section in a PT_NOTE segment. These take
4491 file space but are not loaded into memory. */
4492 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4493 p->p_memsz += this_hdr->sh_size;
4495 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4497 if (p->p_type == PT_TLS)
4498 p->p_memsz += this_hdr->sh_size;
4500 /* .tbss is special. It doesn't contribute to p_memsz of
4501 normal segments. */
4502 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4503 p->p_memsz += this_hdr->sh_size;
4506 if (align > p->p_align
4507 && !m->p_align_valid
4508 && (p->p_type != PT_LOAD
4509 || (abfd->flags & D_PAGED) == 0))
4510 p->p_align = align;
4513 if (!m->p_flags_valid)
4515 p->p_flags |= PF_R;
4516 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4517 p->p_flags |= PF_X;
4518 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4519 p->p_flags |= PF_W;
4522 off -= off_adjust;
4524 /* Check that all sections are in a PT_LOAD segment.
4525 Don't check funky gdb generated core files. */
4526 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4527 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4529 Elf_Internal_Shdr *this_hdr;
4530 asection *sec;
4532 sec = *secpp;
4533 this_hdr = &(elf_section_data(sec)->this_hdr);
4534 if (this_hdr->sh_size != 0
4535 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4537 (*_bfd_error_handler)
4538 (_("%B: section `%A' can't be allocated in segment %d"),
4539 abfd, sec, j);
4540 print_segment_map (m);
4541 bfd_set_error (bfd_error_bad_value);
4542 return FALSE;
4547 elf_tdata (abfd)->next_file_pos = off;
4548 return TRUE;
4551 /* Assign file positions for the other sections. */
4553 static bfd_boolean
4554 assign_file_positions_for_non_load_sections (bfd *abfd,
4555 struct bfd_link_info *link_info)
4557 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4558 Elf_Internal_Shdr **i_shdrpp;
4559 Elf_Internal_Shdr **hdrpp;
4560 Elf_Internal_Phdr *phdrs;
4561 Elf_Internal_Phdr *p;
4562 struct elf_segment_map *m;
4563 bfd_vma filehdr_vaddr, filehdr_paddr;
4564 bfd_vma phdrs_vaddr, phdrs_paddr;
4565 file_ptr off;
4566 unsigned int num_sec;
4567 unsigned int i;
4568 unsigned int count;
4570 i_shdrpp = elf_elfsections (abfd);
4571 num_sec = elf_numsections (abfd);
4572 off = elf_tdata (abfd)->next_file_pos;
4573 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4575 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4576 Elf_Internal_Shdr *hdr;
4578 hdr = *hdrpp;
4579 if (hdr->bfd_section != NULL
4580 && (hdr->bfd_section->filepos != 0
4581 || (hdr->sh_type == SHT_NOBITS
4582 && hdr->contents == NULL)))
4583 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4584 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4586 if (hdr->sh_size != 0)
4587 ((*_bfd_error_handler)
4588 (_("%B: warning: allocated section `%s' not in segment"),
4589 abfd,
4590 (hdr->bfd_section == NULL
4591 ? "*unknown*"
4592 : hdr->bfd_section->name)));
4593 /* We don't need to page align empty sections. */
4594 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4595 off += vma_page_aligned_bias (hdr->sh_addr, off,
4596 bed->maxpagesize);
4597 else
4598 off += vma_page_aligned_bias (hdr->sh_addr, off,
4599 hdr->sh_addralign);
4600 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4601 FALSE);
4603 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4604 && hdr->bfd_section == NULL)
4605 || hdr == i_shdrpp[tdata->symtab_section]
4606 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4607 || hdr == i_shdrpp[tdata->strtab_section])
4608 hdr->sh_offset = -1;
4609 else
4610 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4613 /* Now that we have set the section file positions, we can set up
4614 the file positions for the non PT_LOAD segments. */
4615 count = 0;
4616 filehdr_vaddr = 0;
4617 filehdr_paddr = 0;
4618 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4619 phdrs_paddr = 0;
4620 phdrs = elf_tdata (abfd)->phdr;
4621 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4622 m != NULL;
4623 m = m->next, p++)
4625 ++count;
4626 if (p->p_type != PT_LOAD)
4627 continue;
4629 if (m->includes_filehdr)
4631 filehdr_vaddr = p->p_vaddr;
4632 filehdr_paddr = p->p_paddr;
4634 if (m->includes_phdrs)
4636 phdrs_vaddr = p->p_vaddr;
4637 phdrs_paddr = p->p_paddr;
4638 if (m->includes_filehdr)
4640 phdrs_vaddr += bed->s->sizeof_ehdr;
4641 phdrs_paddr += bed->s->sizeof_ehdr;
4646 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4647 m != NULL;
4648 m = m->next, p++)
4650 if (p->p_type == PT_GNU_RELRO)
4652 const Elf_Internal_Phdr *lp;
4654 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4656 if (link_info != NULL)
4658 /* During linking the range of the RELRO segment is passed
4659 in link_info. */
4660 for (lp = phdrs; lp < phdrs + count; ++lp)
4662 if (lp->p_type == PT_LOAD
4663 && lp->p_vaddr >= link_info->relro_start
4664 && lp->p_vaddr < link_info->relro_end
4665 && lp->p_vaddr + lp->p_filesz >= link_info->relro_end)
4666 break;
4669 else
4671 /* Otherwise we are copying an executable or shared
4672 library, but we need to use the same linker logic. */
4673 for (lp = phdrs; lp < phdrs + count; ++lp)
4675 if (lp->p_type == PT_LOAD
4676 && lp->p_paddr == p->p_paddr)
4677 break;
4681 if (lp < phdrs + count)
4683 p->p_vaddr = lp->p_vaddr;
4684 p->p_paddr = lp->p_paddr;
4685 p->p_offset = lp->p_offset;
4686 if (link_info != NULL)
4687 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4688 else if (m->p_size_valid)
4689 p->p_filesz = m->p_size;
4690 else
4691 abort ();
4692 p->p_memsz = p->p_filesz;
4693 p->p_align = 1;
4694 p->p_flags = (lp->p_flags & ~PF_W);
4696 else
4698 memset (p, 0, sizeof *p);
4699 p->p_type = PT_NULL;
4702 else if (m->count != 0)
4704 if (p->p_type != PT_LOAD
4705 && (p->p_type != PT_NOTE
4706 || bfd_get_format (abfd) != bfd_core))
4708 Elf_Internal_Shdr *hdr;
4709 asection *sect;
4711 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4713 sect = m->sections[m->count - 1];
4714 hdr = &elf_section_data (sect)->this_hdr;
4715 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4716 if (hdr->sh_type != SHT_NOBITS)
4717 p->p_filesz += hdr->sh_size;
4718 p->p_offset = m->sections[0]->filepos;
4721 else if (m->includes_filehdr)
4723 p->p_vaddr = filehdr_vaddr;
4724 if (! m->p_paddr_valid)
4725 p->p_paddr = filehdr_paddr;
4727 else if (m->includes_phdrs)
4729 p->p_vaddr = phdrs_vaddr;
4730 if (! m->p_paddr_valid)
4731 p->p_paddr = phdrs_paddr;
4735 elf_tdata (abfd)->next_file_pos = off;
4737 return TRUE;
4740 /* Work out the file positions of all the sections. This is called by
4741 _bfd_elf_compute_section_file_positions. All the section sizes and
4742 VMAs must be known before this is called.
4744 Reloc sections come in two flavours: Those processed specially as
4745 "side-channel" data attached to a section to which they apply, and
4746 those that bfd doesn't process as relocations. The latter sort are
4747 stored in a normal bfd section by bfd_section_from_shdr. We don't
4748 consider the former sort here, unless they form part of the loadable
4749 image. Reloc sections not assigned here will be handled later by
4750 assign_file_positions_for_relocs.
4752 We also don't set the positions of the .symtab and .strtab here. */
4754 static bfd_boolean
4755 assign_file_positions_except_relocs (bfd *abfd,
4756 struct bfd_link_info *link_info)
4758 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4759 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4760 file_ptr off;
4761 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4763 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4764 && bfd_get_format (abfd) != bfd_core)
4766 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4767 unsigned int num_sec = elf_numsections (abfd);
4768 Elf_Internal_Shdr **hdrpp;
4769 unsigned int i;
4771 /* Start after the ELF header. */
4772 off = i_ehdrp->e_ehsize;
4774 /* We are not creating an executable, which means that we are
4775 not creating a program header, and that the actual order of
4776 the sections in the file is unimportant. */
4777 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4779 Elf_Internal_Shdr *hdr;
4781 hdr = *hdrpp;
4782 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4783 && hdr->bfd_section == NULL)
4784 || i == tdata->symtab_section
4785 || i == tdata->symtab_shndx_section
4786 || i == tdata->strtab_section)
4788 hdr->sh_offset = -1;
4790 else
4791 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4794 else
4796 unsigned int alloc;
4798 /* Assign file positions for the loaded sections based on the
4799 assignment of sections to segments. */
4800 if (!assign_file_positions_for_load_sections (abfd, link_info))
4801 return FALSE;
4803 /* And for non-load sections. */
4804 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4805 return FALSE;
4807 if (bed->elf_backend_modify_program_headers != NULL)
4809 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4810 return FALSE;
4813 /* Write out the program headers. */
4814 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4815 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4816 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4817 return FALSE;
4819 off = tdata->next_file_pos;
4822 /* Place the section headers. */
4823 off = align_file_position (off, 1 << bed->s->log_file_align);
4824 i_ehdrp->e_shoff = off;
4825 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4827 tdata->next_file_pos = off;
4829 return TRUE;
4832 static bfd_boolean
4833 prep_headers (bfd *abfd)
4835 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4836 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4837 struct elf_strtab_hash *shstrtab;
4838 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4840 i_ehdrp = elf_elfheader (abfd);
4842 shstrtab = _bfd_elf_strtab_init ();
4843 if (shstrtab == NULL)
4844 return FALSE;
4846 elf_shstrtab (abfd) = shstrtab;
4848 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4849 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4850 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4851 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4853 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4854 i_ehdrp->e_ident[EI_DATA] =
4855 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4856 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4858 if ((abfd->flags & DYNAMIC) != 0)
4859 i_ehdrp->e_type = ET_DYN;
4860 else if ((abfd->flags & EXEC_P) != 0)
4861 i_ehdrp->e_type = ET_EXEC;
4862 else if (bfd_get_format (abfd) == bfd_core)
4863 i_ehdrp->e_type = ET_CORE;
4864 else
4865 i_ehdrp->e_type = ET_REL;
4867 switch (bfd_get_arch (abfd))
4869 case bfd_arch_unknown:
4870 i_ehdrp->e_machine = EM_NONE;
4871 break;
4873 /* There used to be a long list of cases here, each one setting
4874 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4875 in the corresponding bfd definition. To avoid duplication,
4876 the switch was removed. Machines that need special handling
4877 can generally do it in elf_backend_final_write_processing(),
4878 unless they need the information earlier than the final write.
4879 Such need can generally be supplied by replacing the tests for
4880 e_machine with the conditions used to determine it. */
4881 default:
4882 i_ehdrp->e_machine = bed->elf_machine_code;
4885 i_ehdrp->e_version = bed->s->ev_current;
4886 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4888 /* No program header, for now. */
4889 i_ehdrp->e_phoff = 0;
4890 i_ehdrp->e_phentsize = 0;
4891 i_ehdrp->e_phnum = 0;
4893 /* Each bfd section is section header entry. */
4894 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4895 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4897 /* If we're building an executable, we'll need a program header table. */
4898 if (abfd->flags & EXEC_P)
4899 /* It all happens later. */
4901 else
4903 i_ehdrp->e_phentsize = 0;
4904 i_phdrp = 0;
4905 i_ehdrp->e_phoff = 0;
4908 elf_tdata (abfd)->symtab_hdr.sh_name =
4909 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4910 elf_tdata (abfd)->strtab_hdr.sh_name =
4911 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4912 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4913 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4914 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4915 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4916 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4917 return FALSE;
4919 return TRUE;
4922 /* Assign file positions for all the reloc sections which are not part
4923 of the loadable file image. */
4925 void
4926 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4928 file_ptr off;
4929 unsigned int i, num_sec;
4930 Elf_Internal_Shdr **shdrpp;
4932 off = elf_tdata (abfd)->next_file_pos;
4934 num_sec = elf_numsections (abfd);
4935 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4937 Elf_Internal_Shdr *shdrp;
4939 shdrp = *shdrpp;
4940 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4941 && shdrp->sh_offset == -1)
4942 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4945 elf_tdata (abfd)->next_file_pos = off;
4948 bfd_boolean
4949 _bfd_elf_write_object_contents (bfd *abfd)
4951 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4952 Elf_Internal_Ehdr *i_ehdrp;
4953 Elf_Internal_Shdr **i_shdrp;
4954 bfd_boolean failed;
4955 unsigned int count, num_sec;
4957 if (! abfd->output_has_begun
4958 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4959 return FALSE;
4961 i_shdrp = elf_elfsections (abfd);
4962 i_ehdrp = elf_elfheader (abfd);
4964 failed = FALSE;
4965 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4966 if (failed)
4967 return FALSE;
4969 _bfd_elf_assign_file_positions_for_relocs (abfd);
4971 /* After writing the headers, we need to write the sections too... */
4972 num_sec = elf_numsections (abfd);
4973 for (count = 1; count < num_sec; count++)
4975 if (bed->elf_backend_section_processing)
4976 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4977 if (i_shdrp[count]->contents)
4979 bfd_size_type amt = i_shdrp[count]->sh_size;
4981 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4982 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4983 return FALSE;
4987 /* Write out the section header names. */
4988 if (elf_shstrtab (abfd) != NULL
4989 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4990 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4991 return FALSE;
4993 if (bed->elf_backend_final_write_processing)
4994 (*bed->elf_backend_final_write_processing) (abfd,
4995 elf_tdata (abfd)->linker);
4997 if (!bed->s->write_shdrs_and_ehdr (abfd))
4998 return FALSE;
5000 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5001 if (elf_tdata (abfd)->after_write_object_contents)
5002 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
5004 return TRUE;
5007 bfd_boolean
5008 _bfd_elf_write_corefile_contents (bfd *abfd)
5010 /* Hopefully this can be done just like an object file. */
5011 return _bfd_elf_write_object_contents (abfd);
5014 /* Given a section, search the header to find them. */
5016 unsigned int
5017 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5019 const struct elf_backend_data *bed;
5020 unsigned int index;
5022 if (elf_section_data (asect) != NULL
5023 && elf_section_data (asect)->this_idx != 0)
5024 return elf_section_data (asect)->this_idx;
5026 if (bfd_is_abs_section (asect))
5027 index = SHN_ABS;
5028 else if (bfd_is_com_section (asect))
5029 index = SHN_COMMON;
5030 else if (bfd_is_und_section (asect))
5031 index = SHN_UNDEF;
5032 else
5033 index = SHN_BAD;
5035 bed = get_elf_backend_data (abfd);
5036 if (bed->elf_backend_section_from_bfd_section)
5038 int retval = index;
5040 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5041 return retval;
5044 if (index == SHN_BAD)
5045 bfd_set_error (bfd_error_nonrepresentable_section);
5047 return index;
5050 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5051 on error. */
5054 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5056 asymbol *asym_ptr = *asym_ptr_ptr;
5057 int idx;
5058 flagword flags = asym_ptr->flags;
5060 /* When gas creates relocations against local labels, it creates its
5061 own symbol for the section, but does put the symbol into the
5062 symbol chain, so udata is 0. When the linker is generating
5063 relocatable output, this section symbol may be for one of the
5064 input sections rather than the output section. */
5065 if (asym_ptr->udata.i == 0
5066 && (flags & BSF_SECTION_SYM)
5067 && asym_ptr->section)
5069 asection *sec;
5070 int indx;
5072 sec = asym_ptr->section;
5073 if (sec->owner != abfd && sec->output_section != NULL)
5074 sec = sec->output_section;
5075 if (sec->owner == abfd
5076 && (indx = sec->index) < elf_num_section_syms (abfd)
5077 && elf_section_syms (abfd)[indx] != NULL)
5078 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5081 idx = asym_ptr->udata.i;
5083 if (idx == 0)
5085 /* This case can occur when using --strip-symbol on a symbol
5086 which is used in a relocation entry. */
5087 (*_bfd_error_handler)
5088 (_("%B: symbol `%s' required but not present"),
5089 abfd, bfd_asymbol_name (asym_ptr));
5090 bfd_set_error (bfd_error_no_symbols);
5091 return -1;
5094 #if DEBUG & 4
5096 fprintf (stderr,
5097 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5098 (long) asym_ptr, asym_ptr->name, idx, flags,
5099 elf_symbol_flags (flags));
5100 fflush (stderr);
5102 #endif
5104 return idx;
5107 /* Rewrite program header information. */
5109 static bfd_boolean
5110 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5112 Elf_Internal_Ehdr *iehdr;
5113 struct elf_segment_map *map;
5114 struct elf_segment_map *map_first;
5115 struct elf_segment_map **pointer_to_map;
5116 Elf_Internal_Phdr *segment;
5117 asection *section;
5118 unsigned int i;
5119 unsigned int num_segments;
5120 bfd_boolean phdr_included = FALSE;
5121 bfd_boolean p_paddr_valid;
5122 bfd_vma maxpagesize;
5123 struct elf_segment_map *phdr_adjust_seg = NULL;
5124 unsigned int phdr_adjust_num = 0;
5125 const struct elf_backend_data *bed;
5127 bed = get_elf_backend_data (ibfd);
5128 iehdr = elf_elfheader (ibfd);
5130 map_first = NULL;
5131 pointer_to_map = &map_first;
5133 num_segments = elf_elfheader (ibfd)->e_phnum;
5134 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5136 /* Returns the end address of the segment + 1. */
5137 #define SEGMENT_END(segment, start) \
5138 (start + (segment->p_memsz > segment->p_filesz \
5139 ? segment->p_memsz : segment->p_filesz))
5141 #define SECTION_SIZE(section, segment) \
5142 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5143 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5144 ? section->size : 0)
5146 /* Returns TRUE if the given section is contained within
5147 the given segment. VMA addresses are compared. */
5148 #define IS_CONTAINED_BY_VMA(section, segment) \
5149 (section->vma >= segment->p_vaddr \
5150 && (section->vma + SECTION_SIZE (section, segment) \
5151 <= (SEGMENT_END (segment, segment->p_vaddr))))
5153 /* Returns TRUE if the given section is contained within
5154 the given segment. LMA addresses are compared. */
5155 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5156 (section->lma >= base \
5157 && (section->lma + SECTION_SIZE (section, segment) \
5158 <= SEGMENT_END (segment, base)))
5160 /* Handle PT_NOTE segment. */
5161 #define IS_NOTE(p, s) \
5162 (p->p_type == PT_NOTE \
5163 && elf_section_type (s) == SHT_NOTE \
5164 && (bfd_vma) s->filepos >= p->p_offset \
5165 && ((bfd_vma) s->filepos + s->size \
5166 <= p->p_offset + p->p_filesz))
5168 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5169 etc. */
5170 #define IS_COREFILE_NOTE(p, s) \
5171 (IS_NOTE (p, s) \
5172 && bfd_get_format (ibfd) == bfd_core \
5173 && s->vma == 0 \
5174 && s->lma == 0)
5176 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5177 linker, which generates a PT_INTERP section with p_vaddr and
5178 p_memsz set to 0. */
5179 #define IS_SOLARIS_PT_INTERP(p, s) \
5180 (p->p_vaddr == 0 \
5181 && p->p_paddr == 0 \
5182 && p->p_memsz == 0 \
5183 && p->p_filesz > 0 \
5184 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5185 && s->size > 0 \
5186 && (bfd_vma) s->filepos >= p->p_offset \
5187 && ((bfd_vma) s->filepos + s->size \
5188 <= p->p_offset + p->p_filesz))
5190 /* Decide if the given section should be included in the given segment.
5191 A section will be included if:
5192 1. It is within the address space of the segment -- we use the LMA
5193 if that is set for the segment and the VMA otherwise,
5194 2. It is an allocated section or a NOTE section in a PT_NOTE
5195 segment.
5196 3. There is an output section associated with it,
5197 4. The section has not already been allocated to a previous segment.
5198 5. PT_GNU_STACK segments do not include any sections.
5199 6. PT_TLS segment includes only SHF_TLS sections.
5200 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5201 8. PT_DYNAMIC should not contain empty sections at the beginning
5202 (with the possible exception of .dynamic). */
5203 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5204 ((((segment->p_paddr \
5205 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5206 : IS_CONTAINED_BY_VMA (section, segment)) \
5207 && (section->flags & SEC_ALLOC) != 0) \
5208 || IS_NOTE (segment, section)) \
5209 && segment->p_type != PT_GNU_STACK \
5210 && (segment->p_type != PT_TLS \
5211 || (section->flags & SEC_THREAD_LOCAL)) \
5212 && (segment->p_type == PT_LOAD \
5213 || segment->p_type == PT_TLS \
5214 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5215 && (segment->p_type != PT_DYNAMIC \
5216 || SECTION_SIZE (section, segment) > 0 \
5217 || (segment->p_paddr \
5218 ? segment->p_paddr != section->lma \
5219 : segment->p_vaddr != section->vma) \
5220 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5221 == 0)) \
5222 && !section->segment_mark)
5224 /* If the output section of a section in the input segment is NULL,
5225 it is removed from the corresponding output segment. */
5226 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5227 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5228 && section->output_section != NULL)
5230 /* Returns TRUE iff seg1 starts after the end of seg2. */
5231 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5232 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5234 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5235 their VMA address ranges and their LMA address ranges overlap.
5236 It is possible to have overlapping VMA ranges without overlapping LMA
5237 ranges. RedBoot images for example can have both .data and .bss mapped
5238 to the same VMA range, but with the .data section mapped to a different
5239 LMA. */
5240 #define SEGMENT_OVERLAPS(seg1, seg2) \
5241 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5242 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5243 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5244 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5246 /* Initialise the segment mark field. */
5247 for (section = ibfd->sections; section != NULL; section = section->next)
5248 section->segment_mark = FALSE;
5250 /* The Solaris linker creates program headers in which all the
5251 p_paddr fields are zero. When we try to objcopy or strip such a
5252 file, we get confused. Check for this case, and if we find it
5253 don't set the p_paddr_valid fields. */
5254 p_paddr_valid = FALSE;
5255 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5256 i < num_segments;
5257 i++, segment++)
5258 if (segment->p_paddr != 0)
5260 p_paddr_valid = TRUE;
5261 break;
5264 /* Scan through the segments specified in the program header
5265 of the input BFD. For this first scan we look for overlaps
5266 in the loadable segments. These can be created by weird
5267 parameters to objcopy. Also, fix some solaris weirdness. */
5268 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5269 i < num_segments;
5270 i++, segment++)
5272 unsigned int j;
5273 Elf_Internal_Phdr *segment2;
5275 if (segment->p_type == PT_INTERP)
5276 for (section = ibfd->sections; section; section = section->next)
5277 if (IS_SOLARIS_PT_INTERP (segment, section))
5279 /* Mininal change so that the normal section to segment
5280 assignment code will work. */
5281 segment->p_vaddr = section->vma;
5282 break;
5285 if (segment->p_type != PT_LOAD)
5287 /* Remove PT_GNU_RELRO segment. */
5288 if (segment->p_type == PT_GNU_RELRO)
5289 segment->p_type = PT_NULL;
5290 continue;
5293 /* Determine if this segment overlaps any previous segments. */
5294 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5296 bfd_signed_vma extra_length;
5298 if (segment2->p_type != PT_LOAD
5299 || !SEGMENT_OVERLAPS (segment, segment2))
5300 continue;
5302 /* Merge the two segments together. */
5303 if (segment2->p_vaddr < segment->p_vaddr)
5305 /* Extend SEGMENT2 to include SEGMENT and then delete
5306 SEGMENT. */
5307 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5308 - SEGMENT_END (segment2, segment2->p_vaddr));
5310 if (extra_length > 0)
5312 segment2->p_memsz += extra_length;
5313 segment2->p_filesz += extra_length;
5316 segment->p_type = PT_NULL;
5318 /* Since we have deleted P we must restart the outer loop. */
5319 i = 0;
5320 segment = elf_tdata (ibfd)->phdr;
5321 break;
5323 else
5325 /* Extend SEGMENT to include SEGMENT2 and then delete
5326 SEGMENT2. */
5327 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5328 - SEGMENT_END (segment, segment->p_vaddr));
5330 if (extra_length > 0)
5332 segment->p_memsz += extra_length;
5333 segment->p_filesz += extra_length;
5336 segment2->p_type = PT_NULL;
5341 /* The second scan attempts to assign sections to segments. */
5342 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5343 i < num_segments;
5344 i++, segment++)
5346 unsigned int section_count;
5347 asection **sections;
5348 asection *output_section;
5349 unsigned int isec;
5350 bfd_vma matching_lma;
5351 bfd_vma suggested_lma;
5352 unsigned int j;
5353 bfd_size_type amt;
5354 asection *first_section;
5355 bfd_boolean first_matching_lma;
5356 bfd_boolean first_suggested_lma;
5358 if (segment->p_type == PT_NULL)
5359 continue;
5361 first_section = NULL;
5362 /* Compute how many sections might be placed into this segment. */
5363 for (section = ibfd->sections, section_count = 0;
5364 section != NULL;
5365 section = section->next)
5367 /* Find the first section in the input segment, which may be
5368 removed from the corresponding output segment. */
5369 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5371 if (first_section == NULL)
5372 first_section = section;
5373 if (section->output_section != NULL)
5374 ++section_count;
5378 /* Allocate a segment map big enough to contain
5379 all of the sections we have selected. */
5380 amt = sizeof (struct elf_segment_map);
5381 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5382 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5383 if (map == NULL)
5384 return FALSE;
5386 /* Initialise the fields of the segment map. Default to
5387 using the physical address of the segment in the input BFD. */
5388 map->next = NULL;
5389 map->p_type = segment->p_type;
5390 map->p_flags = segment->p_flags;
5391 map->p_flags_valid = 1;
5393 /* If the first section in the input segment is removed, there is
5394 no need to preserve segment physical address in the corresponding
5395 output segment. */
5396 if (!first_section || first_section->output_section != NULL)
5398 map->p_paddr = segment->p_paddr;
5399 map->p_paddr_valid = p_paddr_valid;
5402 /* Determine if this segment contains the ELF file header
5403 and if it contains the program headers themselves. */
5404 map->includes_filehdr = (segment->p_offset == 0
5405 && segment->p_filesz >= iehdr->e_ehsize);
5406 map->includes_phdrs = 0;
5408 if (!phdr_included || segment->p_type != PT_LOAD)
5410 map->includes_phdrs =
5411 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5412 && (segment->p_offset + segment->p_filesz
5413 >= ((bfd_vma) iehdr->e_phoff
5414 + iehdr->e_phnum * iehdr->e_phentsize)));
5416 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5417 phdr_included = TRUE;
5420 if (section_count == 0)
5422 /* Special segments, such as the PT_PHDR segment, may contain
5423 no sections, but ordinary, loadable segments should contain
5424 something. They are allowed by the ELF spec however, so only
5425 a warning is produced. */
5426 if (segment->p_type == PT_LOAD)
5427 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5428 " detected, is this intentional ?\n"),
5429 ibfd);
5431 map->count = 0;
5432 *pointer_to_map = map;
5433 pointer_to_map = &map->next;
5435 continue;
5438 /* Now scan the sections in the input BFD again and attempt
5439 to add their corresponding output sections to the segment map.
5440 The problem here is how to handle an output section which has
5441 been moved (ie had its LMA changed). There are four possibilities:
5443 1. None of the sections have been moved.
5444 In this case we can continue to use the segment LMA from the
5445 input BFD.
5447 2. All of the sections have been moved by the same amount.
5448 In this case we can change the segment's LMA to match the LMA
5449 of the first section.
5451 3. Some of the sections have been moved, others have not.
5452 In this case those sections which have not been moved can be
5453 placed in the current segment which will have to have its size,
5454 and possibly its LMA changed, and a new segment or segments will
5455 have to be created to contain the other sections.
5457 4. The sections have been moved, but not by the same amount.
5458 In this case we can change the segment's LMA to match the LMA
5459 of the first section and we will have to create a new segment
5460 or segments to contain the other sections.
5462 In order to save time, we allocate an array to hold the section
5463 pointers that we are interested in. As these sections get assigned
5464 to a segment, they are removed from this array. */
5466 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5467 if (sections == NULL)
5468 return FALSE;
5470 /* Step One: Scan for segment vs section LMA conflicts.
5471 Also add the sections to the section array allocated above.
5472 Also add the sections to the current segment. In the common
5473 case, where the sections have not been moved, this means that
5474 we have completely filled the segment, and there is nothing
5475 more to do. */
5476 isec = 0;
5477 matching_lma = 0;
5478 suggested_lma = 0;
5479 first_matching_lma = TRUE;
5480 first_suggested_lma = TRUE;
5482 for (section = ibfd->sections;
5483 section != NULL;
5484 section = section->next)
5485 if (section == first_section)
5486 break;
5488 for (j = 0; section != NULL; section = section->next)
5490 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5492 output_section = section->output_section;
5494 sections[j++] = section;
5496 /* The Solaris native linker always sets p_paddr to 0.
5497 We try to catch that case here, and set it to the
5498 correct value. Note - some backends require that
5499 p_paddr be left as zero. */
5500 if (!p_paddr_valid
5501 && segment->p_vaddr != 0
5502 && !bed->want_p_paddr_set_to_zero
5503 && isec == 0
5504 && output_section->lma != 0
5505 && output_section->vma == (segment->p_vaddr
5506 + (map->includes_filehdr
5507 ? iehdr->e_ehsize
5508 : 0)
5509 + (map->includes_phdrs
5510 ? (iehdr->e_phnum
5511 * iehdr->e_phentsize)
5512 : 0)))
5513 map->p_paddr = segment->p_vaddr;
5515 /* Match up the physical address of the segment with the
5516 LMA address of the output section. */
5517 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5518 || IS_COREFILE_NOTE (segment, section)
5519 || (bed->want_p_paddr_set_to_zero
5520 && IS_CONTAINED_BY_VMA (output_section, segment)))
5522 if (first_matching_lma || output_section->lma < matching_lma)
5524 matching_lma = output_section->lma;
5525 first_matching_lma = FALSE;
5528 /* We assume that if the section fits within the segment
5529 then it does not overlap any other section within that
5530 segment. */
5531 map->sections[isec++] = output_section;
5533 else if (first_suggested_lma)
5535 suggested_lma = output_section->lma;
5536 first_suggested_lma = FALSE;
5539 if (j == section_count)
5540 break;
5544 BFD_ASSERT (j == section_count);
5546 /* Step Two: Adjust the physical address of the current segment,
5547 if necessary. */
5548 if (isec == section_count)
5550 /* All of the sections fitted within the segment as currently
5551 specified. This is the default case. Add the segment to
5552 the list of built segments and carry on to process the next
5553 program header in the input BFD. */
5554 map->count = section_count;
5555 *pointer_to_map = map;
5556 pointer_to_map = &map->next;
5558 if (p_paddr_valid
5559 && !bed->want_p_paddr_set_to_zero
5560 && matching_lma != map->p_paddr
5561 && !map->includes_filehdr
5562 && !map->includes_phdrs)
5563 /* There is some padding before the first section in the
5564 segment. So, we must account for that in the output
5565 segment's vma. */
5566 map->p_vaddr_offset = matching_lma - map->p_paddr;
5568 free (sections);
5569 continue;
5571 else
5573 if (!first_matching_lma)
5575 /* At least one section fits inside the current segment.
5576 Keep it, but modify its physical address to match the
5577 LMA of the first section that fitted. */
5578 map->p_paddr = matching_lma;
5580 else
5582 /* None of the sections fitted inside the current segment.
5583 Change the current segment's physical address to match
5584 the LMA of the first section. */
5585 map->p_paddr = suggested_lma;
5588 /* Offset the segment physical address from the lma
5589 to allow for space taken up by elf headers. */
5590 if (map->includes_filehdr)
5592 if (map->p_paddr >= iehdr->e_ehsize)
5593 map->p_paddr -= iehdr->e_ehsize;
5594 else
5596 map->includes_filehdr = FALSE;
5597 map->includes_phdrs = FALSE;
5601 if (map->includes_phdrs)
5603 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5605 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5607 /* iehdr->e_phnum is just an estimate of the number
5608 of program headers that we will need. Make a note
5609 here of the number we used and the segment we chose
5610 to hold these headers, so that we can adjust the
5611 offset when we know the correct value. */
5612 phdr_adjust_num = iehdr->e_phnum;
5613 phdr_adjust_seg = map;
5615 else
5616 map->includes_phdrs = FALSE;
5620 /* Step Three: Loop over the sections again, this time assigning
5621 those that fit to the current segment and removing them from the
5622 sections array; but making sure not to leave large gaps. Once all
5623 possible sections have been assigned to the current segment it is
5624 added to the list of built segments and if sections still remain
5625 to be assigned, a new segment is constructed before repeating
5626 the loop. */
5627 isec = 0;
5630 map->count = 0;
5631 suggested_lma = 0;
5632 first_suggested_lma = TRUE;
5634 /* Fill the current segment with sections that fit. */
5635 for (j = 0; j < section_count; j++)
5637 section = sections[j];
5639 if (section == NULL)
5640 continue;
5642 output_section = section->output_section;
5644 BFD_ASSERT (output_section != NULL);
5646 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5647 || IS_COREFILE_NOTE (segment, section))
5649 if (map->count == 0)
5651 /* If the first section in a segment does not start at
5652 the beginning of the segment, then something is
5653 wrong. */
5654 if (output_section->lma
5655 != (map->p_paddr
5656 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5657 + (map->includes_phdrs
5658 ? iehdr->e_phnum * iehdr->e_phentsize
5659 : 0)))
5660 abort ();
5662 else
5664 asection *prev_sec;
5666 prev_sec = map->sections[map->count - 1];
5668 /* If the gap between the end of the previous section
5669 and the start of this section is more than
5670 maxpagesize then we need to start a new segment. */
5671 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5672 maxpagesize)
5673 < BFD_ALIGN (output_section->lma, maxpagesize))
5674 || (prev_sec->lma + prev_sec->size
5675 > output_section->lma))
5677 if (first_suggested_lma)
5679 suggested_lma = output_section->lma;
5680 first_suggested_lma = FALSE;
5683 continue;
5687 map->sections[map->count++] = output_section;
5688 ++isec;
5689 sections[j] = NULL;
5690 section->segment_mark = TRUE;
5692 else if (first_suggested_lma)
5694 suggested_lma = output_section->lma;
5695 first_suggested_lma = FALSE;
5699 BFD_ASSERT (map->count > 0);
5701 /* Add the current segment to the list of built segments. */
5702 *pointer_to_map = map;
5703 pointer_to_map = &map->next;
5705 if (isec < section_count)
5707 /* We still have not allocated all of the sections to
5708 segments. Create a new segment here, initialise it
5709 and carry on looping. */
5710 amt = sizeof (struct elf_segment_map);
5711 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5712 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5713 if (map == NULL)
5715 free (sections);
5716 return FALSE;
5719 /* Initialise the fields of the segment map. Set the physical
5720 physical address to the LMA of the first section that has
5721 not yet been assigned. */
5722 map->next = NULL;
5723 map->p_type = segment->p_type;
5724 map->p_flags = segment->p_flags;
5725 map->p_flags_valid = 1;
5726 map->p_paddr = suggested_lma;
5727 map->p_paddr_valid = p_paddr_valid;
5728 map->includes_filehdr = 0;
5729 map->includes_phdrs = 0;
5732 while (isec < section_count);
5734 free (sections);
5737 elf_tdata (obfd)->segment_map = map_first;
5739 /* If we had to estimate the number of program headers that were
5740 going to be needed, then check our estimate now and adjust
5741 the offset if necessary. */
5742 if (phdr_adjust_seg != NULL)
5744 unsigned int count;
5746 for (count = 0, map = map_first; map != NULL; map = map->next)
5747 count++;
5749 if (count > phdr_adjust_num)
5750 phdr_adjust_seg->p_paddr
5751 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5754 #undef SEGMENT_END
5755 #undef SECTION_SIZE
5756 #undef IS_CONTAINED_BY_VMA
5757 #undef IS_CONTAINED_BY_LMA
5758 #undef IS_NOTE
5759 #undef IS_COREFILE_NOTE
5760 #undef IS_SOLARIS_PT_INTERP
5761 #undef IS_SECTION_IN_INPUT_SEGMENT
5762 #undef INCLUDE_SECTION_IN_SEGMENT
5763 #undef SEGMENT_AFTER_SEGMENT
5764 #undef SEGMENT_OVERLAPS
5765 return TRUE;
5768 /* Copy ELF program header information. */
5770 static bfd_boolean
5771 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5773 Elf_Internal_Ehdr *iehdr;
5774 struct elf_segment_map *map;
5775 struct elf_segment_map *map_first;
5776 struct elf_segment_map **pointer_to_map;
5777 Elf_Internal_Phdr *segment;
5778 unsigned int i;
5779 unsigned int num_segments;
5780 bfd_boolean phdr_included = FALSE;
5781 bfd_boolean p_paddr_valid;
5783 iehdr = elf_elfheader (ibfd);
5785 map_first = NULL;
5786 pointer_to_map = &map_first;
5788 /* If all the segment p_paddr fields are zero, don't set
5789 map->p_paddr_valid. */
5790 p_paddr_valid = FALSE;
5791 num_segments = elf_elfheader (ibfd)->e_phnum;
5792 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5793 i < num_segments;
5794 i++, segment++)
5795 if (segment->p_paddr != 0)
5797 p_paddr_valid = TRUE;
5798 break;
5801 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5802 i < num_segments;
5803 i++, segment++)
5805 asection *section;
5806 unsigned int section_count;
5807 bfd_size_type amt;
5808 Elf_Internal_Shdr *this_hdr;
5809 asection *first_section = NULL;
5810 asection *lowest_section = NULL;
5812 /* Compute how many sections are in this segment. */
5813 for (section = ibfd->sections, section_count = 0;
5814 section != NULL;
5815 section = section->next)
5817 this_hdr = &(elf_section_data(section)->this_hdr);
5818 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5820 if (!first_section)
5821 first_section = lowest_section = section;
5822 if (section->lma < lowest_section->lma)
5823 lowest_section = section;
5824 section_count++;
5828 /* Allocate a segment map big enough to contain
5829 all of the sections we have selected. */
5830 amt = sizeof (struct elf_segment_map);
5831 if (section_count != 0)
5832 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5833 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5834 if (map == NULL)
5835 return FALSE;
5837 /* Initialize the fields of the output segment map with the
5838 input segment. */
5839 map->next = NULL;
5840 map->p_type = segment->p_type;
5841 map->p_flags = segment->p_flags;
5842 map->p_flags_valid = 1;
5843 map->p_paddr = segment->p_paddr;
5844 map->p_paddr_valid = p_paddr_valid;
5845 map->p_align = segment->p_align;
5846 map->p_align_valid = 1;
5847 map->p_vaddr_offset = 0;
5849 if (map->p_type == PT_GNU_RELRO)
5851 /* The PT_GNU_RELRO segment may contain the first a few
5852 bytes in the .got.plt section even if the whole .got.plt
5853 section isn't in the PT_GNU_RELRO segment. We won't
5854 change the size of the PT_GNU_RELRO segment. */
5855 map->p_size = segment->p_memsz;
5856 map->p_size_valid = 1;
5859 /* Determine if this segment contains the ELF file header
5860 and if it contains the program headers themselves. */
5861 map->includes_filehdr = (segment->p_offset == 0
5862 && segment->p_filesz >= iehdr->e_ehsize);
5864 map->includes_phdrs = 0;
5865 if (! phdr_included || segment->p_type != PT_LOAD)
5867 map->includes_phdrs =
5868 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5869 && (segment->p_offset + segment->p_filesz
5870 >= ((bfd_vma) iehdr->e_phoff
5871 + iehdr->e_phnum * iehdr->e_phentsize)));
5873 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5874 phdr_included = TRUE;
5877 if (map->includes_filehdr && first_section)
5878 /* We need to keep the space used by the headers fixed. */
5879 map->header_size = first_section->vma - segment->p_vaddr;
5881 if (!map->includes_phdrs
5882 && !map->includes_filehdr
5883 && map->p_paddr_valid)
5884 /* There is some other padding before the first section. */
5885 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5886 - segment->p_paddr);
5888 if (section_count != 0)
5890 unsigned int isec = 0;
5892 for (section = first_section;
5893 section != NULL;
5894 section = section->next)
5896 this_hdr = &(elf_section_data(section)->this_hdr);
5897 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5899 map->sections[isec++] = section->output_section;
5900 if (isec == section_count)
5901 break;
5906 map->count = section_count;
5907 *pointer_to_map = map;
5908 pointer_to_map = &map->next;
5911 elf_tdata (obfd)->segment_map = map_first;
5912 return TRUE;
5915 /* Copy private BFD data. This copies or rewrites ELF program header
5916 information. */
5918 static bfd_boolean
5919 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5921 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5922 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5923 return TRUE;
5925 if (elf_tdata (ibfd)->phdr == NULL)
5926 return TRUE;
5928 if (ibfd->xvec == obfd->xvec)
5930 /* Check to see if any sections in the input BFD
5931 covered by ELF program header have changed. */
5932 Elf_Internal_Phdr *segment;
5933 asection *section, *osec;
5934 unsigned int i, num_segments;
5935 Elf_Internal_Shdr *this_hdr;
5936 const struct elf_backend_data *bed;
5938 bed = get_elf_backend_data (ibfd);
5940 /* Regenerate the segment map if p_paddr is set to 0. */
5941 if (bed->want_p_paddr_set_to_zero)
5942 goto rewrite;
5944 /* Initialize the segment mark field. */
5945 for (section = obfd->sections; section != NULL;
5946 section = section->next)
5947 section->segment_mark = FALSE;
5949 num_segments = elf_elfheader (ibfd)->e_phnum;
5950 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5951 i < num_segments;
5952 i++, segment++)
5954 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5955 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5956 which severly confuses things, so always regenerate the segment
5957 map in this case. */
5958 if (segment->p_paddr == 0
5959 && segment->p_memsz == 0
5960 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5961 goto rewrite;
5963 for (section = ibfd->sections;
5964 section != NULL; section = section->next)
5966 /* We mark the output section so that we know it comes
5967 from the input BFD. */
5968 osec = section->output_section;
5969 if (osec)
5970 osec->segment_mark = TRUE;
5972 /* Check if this section is covered by the segment. */
5973 this_hdr = &(elf_section_data(section)->this_hdr);
5974 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5976 /* FIXME: Check if its output section is changed or
5977 removed. What else do we need to check? */
5978 if (osec == NULL
5979 || section->flags != osec->flags
5980 || section->lma != osec->lma
5981 || section->vma != osec->vma
5982 || section->size != osec->size
5983 || section->rawsize != osec->rawsize
5984 || section->alignment_power != osec->alignment_power)
5985 goto rewrite;
5990 /* Check to see if any output section do not come from the
5991 input BFD. */
5992 for (section = obfd->sections; section != NULL;
5993 section = section->next)
5995 if (section->segment_mark == FALSE)
5996 goto rewrite;
5997 else
5998 section->segment_mark = FALSE;
6001 return copy_elf_program_header (ibfd, obfd);
6004 rewrite:
6005 return rewrite_elf_program_header (ibfd, obfd);
6008 /* Initialize private output section information from input section. */
6010 bfd_boolean
6011 _bfd_elf_init_private_section_data (bfd *ibfd,
6012 asection *isec,
6013 bfd *obfd,
6014 asection *osec,
6015 struct bfd_link_info *link_info)
6018 Elf_Internal_Shdr *ihdr, *ohdr;
6019 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
6021 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6022 || obfd->xvec->flavour != bfd_target_elf_flavour)
6023 return TRUE;
6025 /* Don't copy the output ELF section type from input if the
6026 output BFD section flags have been set to something different.
6027 elf_fake_sections will set ELF section type based on BFD
6028 section flags. */
6029 if (elf_section_type (osec) == SHT_NULL
6030 && (osec->flags == isec->flags || !osec->flags))
6031 elf_section_type (osec) = elf_section_type (isec);
6033 /* FIXME: Is this correct for all OS/PROC specific flags? */
6034 elf_section_flags (osec) |= (elf_section_flags (isec)
6035 & (SHF_MASKOS | SHF_MASKPROC));
6037 /* Set things up for objcopy and relocatable link. The output
6038 SHT_GROUP section will have its elf_next_in_group pointing back
6039 to the input group members. Ignore linker created group section.
6040 See elfNN_ia64_object_p in elfxx-ia64.c. */
6041 if (need_group)
6043 if (elf_sec_group (isec) == NULL
6044 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6046 if (elf_section_flags (isec) & SHF_GROUP)
6047 elf_section_flags (osec) |= SHF_GROUP;
6048 elf_next_in_group (osec) = elf_next_in_group (isec);
6049 elf_section_data (osec)->group = elf_section_data (isec)->group;
6053 ihdr = &elf_section_data (isec)->this_hdr;
6055 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6056 don't use the output section of the linked-to section since it
6057 may be NULL at this point. */
6058 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6060 ohdr = &elf_section_data (osec)->this_hdr;
6061 ohdr->sh_flags |= SHF_LINK_ORDER;
6062 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6065 osec->use_rela_p = isec->use_rela_p;
6067 return TRUE;
6070 /* Copy private section information. This copies over the entsize
6071 field, and sometimes the info field. */
6073 bfd_boolean
6074 _bfd_elf_copy_private_section_data (bfd *ibfd,
6075 asection *isec,
6076 bfd *obfd,
6077 asection *osec)
6079 Elf_Internal_Shdr *ihdr, *ohdr;
6081 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6082 || obfd->xvec->flavour != bfd_target_elf_flavour)
6083 return TRUE;
6085 ihdr = &elf_section_data (isec)->this_hdr;
6086 ohdr = &elf_section_data (osec)->this_hdr;
6088 ohdr->sh_entsize = ihdr->sh_entsize;
6090 if (ihdr->sh_type == SHT_SYMTAB
6091 || ihdr->sh_type == SHT_DYNSYM
6092 || ihdr->sh_type == SHT_GNU_verneed
6093 || ihdr->sh_type == SHT_GNU_verdef)
6094 ohdr->sh_info = ihdr->sh_info;
6096 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6097 NULL);
6100 /* Copy private header information. */
6102 bfd_boolean
6103 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6105 asection *isec;
6107 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6108 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6109 return TRUE;
6111 /* Copy over private BFD data if it has not already been copied.
6112 This must be done here, rather than in the copy_private_bfd_data
6113 entry point, because the latter is called after the section
6114 contents have been set, which means that the program headers have
6115 already been worked out. */
6116 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6118 if (! copy_private_bfd_data (ibfd, obfd))
6119 return FALSE;
6122 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6123 but this might be wrong if we deleted the group section. */
6124 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6125 if (elf_section_type (isec) == SHT_GROUP
6126 && isec->output_section == NULL)
6128 asection *first = elf_next_in_group (isec);
6129 asection *s = first;
6130 while (s != NULL)
6132 if (s->output_section != NULL)
6134 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6135 elf_group_name (s->output_section) = NULL;
6137 s = elf_next_in_group (s);
6138 if (s == first)
6139 break;
6143 return TRUE;
6146 /* Copy private symbol information. If this symbol is in a section
6147 which we did not map into a BFD section, try to map the section
6148 index correctly. We use special macro definitions for the mapped
6149 section indices; these definitions are interpreted by the
6150 swap_out_syms function. */
6152 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6153 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6154 #define MAP_STRTAB (SHN_HIOS + 3)
6155 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6156 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6158 bfd_boolean
6159 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6160 asymbol *isymarg,
6161 bfd *obfd,
6162 asymbol *osymarg)
6164 elf_symbol_type *isym, *osym;
6166 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6167 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6168 return TRUE;
6170 isym = elf_symbol_from (ibfd, isymarg);
6171 osym = elf_symbol_from (obfd, osymarg);
6173 if (isym != NULL
6174 && isym->internal_elf_sym.st_shndx != 0
6175 && osym != NULL
6176 && bfd_is_abs_section (isym->symbol.section))
6178 unsigned int shndx;
6180 shndx = isym->internal_elf_sym.st_shndx;
6181 if (shndx == elf_onesymtab (ibfd))
6182 shndx = MAP_ONESYMTAB;
6183 else if (shndx == elf_dynsymtab (ibfd))
6184 shndx = MAP_DYNSYMTAB;
6185 else if (shndx == elf_tdata (ibfd)->strtab_section)
6186 shndx = MAP_STRTAB;
6187 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6188 shndx = MAP_SHSTRTAB;
6189 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6190 shndx = MAP_SYM_SHNDX;
6191 osym->internal_elf_sym.st_shndx = shndx;
6194 return TRUE;
6197 /* Swap out the symbols. */
6199 static bfd_boolean
6200 swap_out_syms (bfd *abfd,
6201 struct bfd_strtab_hash **sttp,
6202 int relocatable_p)
6204 const struct elf_backend_data *bed;
6205 int symcount;
6206 asymbol **syms;
6207 struct bfd_strtab_hash *stt;
6208 Elf_Internal_Shdr *symtab_hdr;
6209 Elf_Internal_Shdr *symtab_shndx_hdr;
6210 Elf_Internal_Shdr *symstrtab_hdr;
6211 bfd_byte *outbound_syms;
6212 bfd_byte *outbound_shndx;
6213 int idx;
6214 bfd_size_type amt;
6215 bfd_boolean name_local_sections;
6217 if (!elf_map_symbols (abfd))
6218 return FALSE;
6220 /* Dump out the symtabs. */
6221 stt = _bfd_elf_stringtab_init ();
6222 if (stt == NULL)
6223 return FALSE;
6225 bed = get_elf_backend_data (abfd);
6226 symcount = bfd_get_symcount (abfd);
6227 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6228 symtab_hdr->sh_type = SHT_SYMTAB;
6229 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6230 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6231 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6232 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6234 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6235 symstrtab_hdr->sh_type = SHT_STRTAB;
6237 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6238 bed->s->sizeof_sym);
6239 if (outbound_syms == NULL)
6241 _bfd_stringtab_free (stt);
6242 return FALSE;
6244 symtab_hdr->contents = outbound_syms;
6246 outbound_shndx = NULL;
6247 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6248 if (symtab_shndx_hdr->sh_name != 0)
6250 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6251 outbound_shndx = (bfd_byte *)
6252 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6253 if (outbound_shndx == NULL)
6255 _bfd_stringtab_free (stt);
6256 return FALSE;
6259 symtab_shndx_hdr->contents = outbound_shndx;
6260 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6261 symtab_shndx_hdr->sh_size = amt;
6262 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6263 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6266 /* Now generate the data (for "contents"). */
6268 /* Fill in zeroth symbol and swap it out. */
6269 Elf_Internal_Sym sym;
6270 sym.st_name = 0;
6271 sym.st_value = 0;
6272 sym.st_size = 0;
6273 sym.st_info = 0;
6274 sym.st_other = 0;
6275 sym.st_shndx = SHN_UNDEF;
6276 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6277 outbound_syms += bed->s->sizeof_sym;
6278 if (outbound_shndx != NULL)
6279 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6282 name_local_sections
6283 = (bed->elf_backend_name_local_section_symbols
6284 && bed->elf_backend_name_local_section_symbols (abfd));
6286 syms = bfd_get_outsymbols (abfd);
6287 for (idx = 0; idx < symcount; idx++)
6289 Elf_Internal_Sym sym;
6290 bfd_vma value = syms[idx]->value;
6291 elf_symbol_type *type_ptr;
6292 flagword flags = syms[idx]->flags;
6293 int type;
6295 if (!name_local_sections
6296 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6298 /* Local section symbols have no name. */
6299 sym.st_name = 0;
6301 else
6303 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6304 syms[idx]->name,
6305 TRUE, FALSE);
6306 if (sym.st_name == (unsigned long) -1)
6308 _bfd_stringtab_free (stt);
6309 return FALSE;
6313 type_ptr = elf_symbol_from (abfd, syms[idx]);
6315 if ((flags & BSF_SECTION_SYM) == 0
6316 && bfd_is_com_section (syms[idx]->section))
6318 /* ELF common symbols put the alignment into the `value' field,
6319 and the size into the `size' field. This is backwards from
6320 how BFD handles it, so reverse it here. */
6321 sym.st_size = value;
6322 if (type_ptr == NULL
6323 || type_ptr->internal_elf_sym.st_value == 0)
6324 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6325 else
6326 sym.st_value = type_ptr->internal_elf_sym.st_value;
6327 sym.st_shndx = _bfd_elf_section_from_bfd_section
6328 (abfd, syms[idx]->section);
6330 else
6332 asection *sec = syms[idx]->section;
6333 unsigned int shndx;
6335 if (sec->output_section)
6337 value += sec->output_offset;
6338 sec = sec->output_section;
6341 /* Don't add in the section vma for relocatable output. */
6342 if (! relocatable_p)
6343 value += sec->vma;
6344 sym.st_value = value;
6345 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6347 if (bfd_is_abs_section (sec)
6348 && type_ptr != NULL
6349 && type_ptr->internal_elf_sym.st_shndx != 0)
6351 /* This symbol is in a real ELF section which we did
6352 not create as a BFD section. Undo the mapping done
6353 by copy_private_symbol_data. */
6354 shndx = type_ptr->internal_elf_sym.st_shndx;
6355 switch (shndx)
6357 case MAP_ONESYMTAB:
6358 shndx = elf_onesymtab (abfd);
6359 break;
6360 case MAP_DYNSYMTAB:
6361 shndx = elf_dynsymtab (abfd);
6362 break;
6363 case MAP_STRTAB:
6364 shndx = elf_tdata (abfd)->strtab_section;
6365 break;
6366 case MAP_SHSTRTAB:
6367 shndx = elf_tdata (abfd)->shstrtab_section;
6368 break;
6369 case MAP_SYM_SHNDX:
6370 shndx = elf_tdata (abfd)->symtab_shndx_section;
6371 break;
6372 default:
6373 break;
6376 else
6378 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6380 if (shndx == SHN_BAD)
6382 asection *sec2;
6384 /* Writing this would be a hell of a lot easier if
6385 we had some decent documentation on bfd, and
6386 knew what to expect of the library, and what to
6387 demand of applications. For example, it
6388 appears that `objcopy' might not set the
6389 section of a symbol to be a section that is
6390 actually in the output file. */
6391 sec2 = bfd_get_section_by_name (abfd, sec->name);
6392 if (sec2 == NULL)
6394 _bfd_error_handler (_("\
6395 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6396 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6397 sec->name);
6398 bfd_set_error (bfd_error_invalid_operation);
6399 _bfd_stringtab_free (stt);
6400 return FALSE;
6403 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6404 BFD_ASSERT (shndx != SHN_BAD);
6408 sym.st_shndx = shndx;
6411 if ((flags & BSF_THREAD_LOCAL) != 0)
6412 type = STT_TLS;
6413 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6414 type = STT_GNU_IFUNC;
6415 else if ((flags & BSF_FUNCTION) != 0)
6416 type = STT_FUNC;
6417 else if ((flags & BSF_OBJECT) != 0)
6418 type = STT_OBJECT;
6419 else if ((flags & BSF_RELC) != 0)
6420 type = STT_RELC;
6421 else if ((flags & BSF_SRELC) != 0)
6422 type = STT_SRELC;
6423 else
6424 type = STT_NOTYPE;
6426 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6427 type = STT_TLS;
6429 /* Processor-specific types. */
6430 if (type_ptr != NULL
6431 && bed->elf_backend_get_symbol_type)
6432 type = ((*bed->elf_backend_get_symbol_type)
6433 (&type_ptr->internal_elf_sym, type));
6435 if (flags & BSF_SECTION_SYM)
6437 if (flags & BSF_GLOBAL)
6438 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6439 else
6440 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6442 else if (bfd_is_com_section (syms[idx]->section))
6444 #ifdef USE_STT_COMMON
6445 if (type == STT_OBJECT)
6446 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6447 else
6448 #endif
6449 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6451 else if (bfd_is_und_section (syms[idx]->section))
6452 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6453 ? STB_WEAK
6454 : STB_GLOBAL),
6455 type);
6456 else if (flags & BSF_FILE)
6457 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6458 else
6460 int bind = STB_LOCAL;
6462 if (flags & BSF_LOCAL)
6463 bind = STB_LOCAL;
6464 else if (flags & BSF_GNU_UNIQUE)
6465 bind = STB_GNU_UNIQUE;
6466 else if (flags & BSF_WEAK)
6467 bind = STB_WEAK;
6468 else if (flags & BSF_GLOBAL)
6469 bind = STB_GLOBAL;
6471 sym.st_info = ELF_ST_INFO (bind, type);
6474 if (type_ptr != NULL)
6475 sym.st_other = type_ptr->internal_elf_sym.st_other;
6476 else
6477 sym.st_other = 0;
6479 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6480 outbound_syms += bed->s->sizeof_sym;
6481 if (outbound_shndx != NULL)
6482 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6485 *sttp = stt;
6486 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6487 symstrtab_hdr->sh_type = SHT_STRTAB;
6489 symstrtab_hdr->sh_flags = 0;
6490 symstrtab_hdr->sh_addr = 0;
6491 symstrtab_hdr->sh_entsize = 0;
6492 symstrtab_hdr->sh_link = 0;
6493 symstrtab_hdr->sh_info = 0;
6494 symstrtab_hdr->sh_addralign = 1;
6496 return TRUE;
6499 /* Return the number of bytes required to hold the symtab vector.
6501 Note that we base it on the count plus 1, since we will null terminate
6502 the vector allocated based on this size. However, the ELF symbol table
6503 always has a dummy entry as symbol #0, so it ends up even. */
6505 long
6506 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6508 long symcount;
6509 long symtab_size;
6510 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6512 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6513 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6514 if (symcount > 0)
6515 symtab_size -= sizeof (asymbol *);
6517 return symtab_size;
6520 long
6521 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6523 long symcount;
6524 long symtab_size;
6525 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6527 if (elf_dynsymtab (abfd) == 0)
6529 bfd_set_error (bfd_error_invalid_operation);
6530 return -1;
6533 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6534 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6535 if (symcount > 0)
6536 symtab_size -= sizeof (asymbol *);
6538 return symtab_size;
6541 long
6542 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6543 sec_ptr asect)
6545 return (asect->reloc_count + 1) * sizeof (arelent *);
6548 /* Canonicalize the relocs. */
6550 long
6551 _bfd_elf_canonicalize_reloc (bfd *abfd,
6552 sec_ptr section,
6553 arelent **relptr,
6554 asymbol **symbols)
6556 arelent *tblptr;
6557 unsigned int i;
6558 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6560 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6561 return -1;
6563 tblptr = section->relocation;
6564 for (i = 0; i < section->reloc_count; i++)
6565 *relptr++ = tblptr++;
6567 *relptr = NULL;
6569 return section->reloc_count;
6572 long
6573 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6575 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6576 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6578 if (symcount >= 0)
6579 bfd_get_symcount (abfd) = symcount;
6580 return symcount;
6583 long
6584 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6585 asymbol **allocation)
6587 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6588 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6590 if (symcount >= 0)
6591 bfd_get_dynamic_symcount (abfd) = symcount;
6592 return symcount;
6595 /* Return the size required for the dynamic reloc entries. Any loadable
6596 section that was actually installed in the BFD, and has type SHT_REL
6597 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6598 dynamic reloc section. */
6600 long
6601 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6603 long ret;
6604 asection *s;
6606 if (elf_dynsymtab (abfd) == 0)
6608 bfd_set_error (bfd_error_invalid_operation);
6609 return -1;
6612 ret = sizeof (arelent *);
6613 for (s = abfd->sections; s != NULL; s = s->next)
6614 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6615 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6616 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6617 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6618 * sizeof (arelent *));
6620 return ret;
6623 /* Canonicalize the dynamic relocation entries. Note that we return the
6624 dynamic relocations as a single block, although they are actually
6625 associated with particular sections; the interface, which was
6626 designed for SunOS style shared libraries, expects that there is only
6627 one set of dynamic relocs. Any loadable section that was actually
6628 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6629 dynamic symbol table, is considered to be a dynamic reloc section. */
6631 long
6632 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6633 arelent **storage,
6634 asymbol **syms)
6636 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6637 asection *s;
6638 long ret;
6640 if (elf_dynsymtab (abfd) == 0)
6642 bfd_set_error (bfd_error_invalid_operation);
6643 return -1;
6646 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6647 ret = 0;
6648 for (s = abfd->sections; s != NULL; s = s->next)
6650 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6651 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6652 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6654 arelent *p;
6655 long count, i;
6657 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6658 return -1;
6659 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6660 p = s->relocation;
6661 for (i = 0; i < count; i++)
6662 *storage++ = p++;
6663 ret += count;
6667 *storage = NULL;
6669 return ret;
6672 /* Read in the version information. */
6674 bfd_boolean
6675 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6677 bfd_byte *contents = NULL;
6678 unsigned int freeidx = 0;
6680 if (elf_dynverref (abfd) != 0)
6682 Elf_Internal_Shdr *hdr;
6683 Elf_External_Verneed *everneed;
6684 Elf_Internal_Verneed *iverneed;
6685 unsigned int i;
6686 bfd_byte *contents_end;
6688 hdr = &elf_tdata (abfd)->dynverref_hdr;
6690 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
6691 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
6692 if (elf_tdata (abfd)->verref == NULL)
6693 goto error_return;
6695 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6697 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6698 if (contents == NULL)
6700 error_return_verref:
6701 elf_tdata (abfd)->verref = NULL;
6702 elf_tdata (abfd)->cverrefs = 0;
6703 goto error_return;
6705 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6706 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6707 goto error_return_verref;
6709 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6710 goto error_return_verref;
6712 BFD_ASSERT (sizeof (Elf_External_Verneed)
6713 == sizeof (Elf_External_Vernaux));
6714 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6715 everneed = (Elf_External_Verneed *) contents;
6716 iverneed = elf_tdata (abfd)->verref;
6717 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6719 Elf_External_Vernaux *evernaux;
6720 Elf_Internal_Vernaux *ivernaux;
6721 unsigned int j;
6723 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6725 iverneed->vn_bfd = abfd;
6727 iverneed->vn_filename =
6728 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6729 iverneed->vn_file);
6730 if (iverneed->vn_filename == NULL)
6731 goto error_return_verref;
6733 if (iverneed->vn_cnt == 0)
6734 iverneed->vn_auxptr = NULL;
6735 else
6737 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
6738 bfd_alloc2 (abfd, iverneed->vn_cnt,
6739 sizeof (Elf_Internal_Vernaux));
6740 if (iverneed->vn_auxptr == NULL)
6741 goto error_return_verref;
6744 if (iverneed->vn_aux
6745 > (size_t) (contents_end - (bfd_byte *) everneed))
6746 goto error_return_verref;
6748 evernaux = ((Elf_External_Vernaux *)
6749 ((bfd_byte *) everneed + iverneed->vn_aux));
6750 ivernaux = iverneed->vn_auxptr;
6751 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6753 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6755 ivernaux->vna_nodename =
6756 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6757 ivernaux->vna_name);
6758 if (ivernaux->vna_nodename == NULL)
6759 goto error_return_verref;
6761 if (j + 1 < iverneed->vn_cnt)
6762 ivernaux->vna_nextptr = ivernaux + 1;
6763 else
6764 ivernaux->vna_nextptr = NULL;
6766 if (ivernaux->vna_next
6767 > (size_t) (contents_end - (bfd_byte *) evernaux))
6768 goto error_return_verref;
6770 evernaux = ((Elf_External_Vernaux *)
6771 ((bfd_byte *) evernaux + ivernaux->vna_next));
6773 if (ivernaux->vna_other > freeidx)
6774 freeidx = ivernaux->vna_other;
6777 if (i + 1 < hdr->sh_info)
6778 iverneed->vn_nextref = iverneed + 1;
6779 else
6780 iverneed->vn_nextref = NULL;
6782 if (iverneed->vn_next
6783 > (size_t) (contents_end - (bfd_byte *) everneed))
6784 goto error_return_verref;
6786 everneed = ((Elf_External_Verneed *)
6787 ((bfd_byte *) everneed + iverneed->vn_next));
6790 free (contents);
6791 contents = NULL;
6794 if (elf_dynverdef (abfd) != 0)
6796 Elf_Internal_Shdr *hdr;
6797 Elf_External_Verdef *everdef;
6798 Elf_Internal_Verdef *iverdef;
6799 Elf_Internal_Verdef *iverdefarr;
6800 Elf_Internal_Verdef iverdefmem;
6801 unsigned int i;
6802 unsigned int maxidx;
6803 bfd_byte *contents_end_def, *contents_end_aux;
6805 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6807 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6808 if (contents == NULL)
6809 goto error_return;
6810 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6811 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6812 goto error_return;
6814 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6815 goto error_return;
6817 BFD_ASSERT (sizeof (Elf_External_Verdef)
6818 >= sizeof (Elf_External_Verdaux));
6819 contents_end_def = contents + hdr->sh_size
6820 - sizeof (Elf_External_Verdef);
6821 contents_end_aux = contents + hdr->sh_size
6822 - sizeof (Elf_External_Verdaux);
6824 /* We know the number of entries in the section but not the maximum
6825 index. Therefore we have to run through all entries and find
6826 the maximum. */
6827 everdef = (Elf_External_Verdef *) contents;
6828 maxidx = 0;
6829 for (i = 0; i < hdr->sh_info; ++i)
6831 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6833 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6834 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6836 if (iverdefmem.vd_next
6837 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6838 goto error_return;
6840 everdef = ((Elf_External_Verdef *)
6841 ((bfd_byte *) everdef + iverdefmem.vd_next));
6844 if (default_imported_symver)
6846 if (freeidx > maxidx)
6847 maxidx = ++freeidx;
6848 else
6849 freeidx = ++maxidx;
6851 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
6852 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
6853 if (elf_tdata (abfd)->verdef == NULL)
6854 goto error_return;
6856 elf_tdata (abfd)->cverdefs = maxidx;
6858 everdef = (Elf_External_Verdef *) contents;
6859 iverdefarr = elf_tdata (abfd)->verdef;
6860 for (i = 0; i < hdr->sh_info; i++)
6862 Elf_External_Verdaux *everdaux;
6863 Elf_Internal_Verdaux *iverdaux;
6864 unsigned int j;
6866 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6868 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6870 error_return_verdef:
6871 elf_tdata (abfd)->verdef = NULL;
6872 elf_tdata (abfd)->cverdefs = 0;
6873 goto error_return;
6876 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6877 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6879 iverdef->vd_bfd = abfd;
6881 if (iverdef->vd_cnt == 0)
6882 iverdef->vd_auxptr = NULL;
6883 else
6885 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
6886 bfd_alloc2 (abfd, iverdef->vd_cnt,
6887 sizeof (Elf_Internal_Verdaux));
6888 if (iverdef->vd_auxptr == NULL)
6889 goto error_return_verdef;
6892 if (iverdef->vd_aux
6893 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6894 goto error_return_verdef;
6896 everdaux = ((Elf_External_Verdaux *)
6897 ((bfd_byte *) everdef + iverdef->vd_aux));
6898 iverdaux = iverdef->vd_auxptr;
6899 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6901 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6903 iverdaux->vda_nodename =
6904 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6905 iverdaux->vda_name);
6906 if (iverdaux->vda_nodename == NULL)
6907 goto error_return_verdef;
6909 if (j + 1 < iverdef->vd_cnt)
6910 iverdaux->vda_nextptr = iverdaux + 1;
6911 else
6912 iverdaux->vda_nextptr = NULL;
6914 if (iverdaux->vda_next
6915 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6916 goto error_return_verdef;
6918 everdaux = ((Elf_External_Verdaux *)
6919 ((bfd_byte *) everdaux + iverdaux->vda_next));
6922 if (iverdef->vd_cnt)
6923 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6925 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6926 iverdef->vd_nextdef = iverdef + 1;
6927 else
6928 iverdef->vd_nextdef = NULL;
6930 everdef = ((Elf_External_Verdef *)
6931 ((bfd_byte *) everdef + iverdef->vd_next));
6934 free (contents);
6935 contents = NULL;
6937 else if (default_imported_symver)
6939 if (freeidx < 3)
6940 freeidx = 3;
6941 else
6942 freeidx++;
6944 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
6945 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
6946 if (elf_tdata (abfd)->verdef == NULL)
6947 goto error_return;
6949 elf_tdata (abfd)->cverdefs = freeidx;
6952 /* Create a default version based on the soname. */
6953 if (default_imported_symver)
6955 Elf_Internal_Verdef *iverdef;
6956 Elf_Internal_Verdaux *iverdaux;
6958 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6960 iverdef->vd_version = VER_DEF_CURRENT;
6961 iverdef->vd_flags = 0;
6962 iverdef->vd_ndx = freeidx;
6963 iverdef->vd_cnt = 1;
6965 iverdef->vd_bfd = abfd;
6967 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6968 if (iverdef->vd_nodename == NULL)
6969 goto error_return_verdef;
6970 iverdef->vd_nextdef = NULL;
6971 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
6972 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6973 if (iverdef->vd_auxptr == NULL)
6974 goto error_return_verdef;
6976 iverdaux = iverdef->vd_auxptr;
6977 iverdaux->vda_nodename = iverdef->vd_nodename;
6978 iverdaux->vda_nextptr = NULL;
6981 return TRUE;
6983 error_return:
6984 if (contents != NULL)
6985 free (contents);
6986 return FALSE;
6989 asymbol *
6990 _bfd_elf_make_empty_symbol (bfd *abfd)
6992 elf_symbol_type *newsym;
6993 bfd_size_type amt = sizeof (elf_symbol_type);
6995 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
6996 if (!newsym)
6997 return NULL;
6998 else
7000 newsym->symbol.the_bfd = abfd;
7001 return &newsym->symbol;
7005 void
7006 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7007 asymbol *symbol,
7008 symbol_info *ret)
7010 bfd_symbol_info (symbol, ret);
7013 /* Return whether a symbol name implies a local symbol. Most targets
7014 use this function for the is_local_label_name entry point, but some
7015 override it. */
7017 bfd_boolean
7018 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7019 const char *name)
7021 /* Normal local symbols start with ``.L''. */
7022 if (name[0] == '.' && name[1] == 'L')
7023 return TRUE;
7025 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7026 DWARF debugging symbols starting with ``..''. */
7027 if (name[0] == '.' && name[1] == '.')
7028 return TRUE;
7030 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7031 emitting DWARF debugging output. I suspect this is actually a
7032 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7033 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7034 underscore to be emitted on some ELF targets). For ease of use,
7035 we treat such symbols as local. */
7036 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7037 return TRUE;
7039 return FALSE;
7042 alent *
7043 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7044 asymbol *symbol ATTRIBUTE_UNUSED)
7046 abort ();
7047 return NULL;
7050 bfd_boolean
7051 _bfd_elf_set_arch_mach (bfd *abfd,
7052 enum bfd_architecture arch,
7053 unsigned long machine)
7055 /* If this isn't the right architecture for this backend, and this
7056 isn't the generic backend, fail. */
7057 if (arch != get_elf_backend_data (abfd)->arch
7058 && arch != bfd_arch_unknown
7059 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7060 return FALSE;
7062 return bfd_default_set_arch_mach (abfd, arch, machine);
7065 /* Find the function to a particular section and offset,
7066 for error reporting. */
7068 static bfd_boolean
7069 elf_find_function (bfd *abfd,
7070 asection *section,
7071 asymbol **symbols,
7072 bfd_vma offset,
7073 const char **filename_ptr,
7074 const char **functionname_ptr)
7076 const char *filename;
7077 asymbol *func, *file;
7078 bfd_vma low_func;
7079 asymbol **p;
7080 /* ??? Given multiple file symbols, it is impossible to reliably
7081 choose the right file name for global symbols. File symbols are
7082 local symbols, and thus all file symbols must sort before any
7083 global symbols. The ELF spec may be interpreted to say that a
7084 file symbol must sort before other local symbols, but currently
7085 ld -r doesn't do this. So, for ld -r output, it is possible to
7086 make a better choice of file name for local symbols by ignoring
7087 file symbols appearing after a given local symbol. */
7088 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7089 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7091 filename = NULL;
7092 func = NULL;
7093 file = NULL;
7094 low_func = 0;
7095 state = nothing_seen;
7097 for (p = symbols; *p != NULL; p++)
7099 elf_symbol_type *q;
7100 unsigned int type;
7102 q = (elf_symbol_type *) *p;
7104 type = ELF_ST_TYPE (q->internal_elf_sym.st_info);
7105 switch (type)
7107 case STT_FILE:
7108 file = &q->symbol;
7109 if (state == symbol_seen)
7110 state = file_after_symbol_seen;
7111 continue;
7112 default:
7113 if (!bed->is_function_type (type))
7114 break;
7115 case STT_NOTYPE:
7116 if (bfd_get_section (&q->symbol) == section
7117 && q->symbol.value >= low_func
7118 && q->symbol.value <= offset)
7120 func = (asymbol *) q;
7121 low_func = q->symbol.value;
7122 filename = NULL;
7123 if (file != NULL
7124 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7125 || state != file_after_symbol_seen))
7126 filename = bfd_asymbol_name (file);
7128 break;
7130 if (state == nothing_seen)
7131 state = symbol_seen;
7134 if (func == NULL)
7135 return FALSE;
7137 if (filename_ptr)
7138 *filename_ptr = filename;
7139 if (functionname_ptr)
7140 *functionname_ptr = bfd_asymbol_name (func);
7142 return TRUE;
7145 /* Find the nearest line to a particular section and offset,
7146 for error reporting. */
7148 bfd_boolean
7149 _bfd_elf_find_nearest_line (bfd *abfd,
7150 asection *section,
7151 asymbol **symbols,
7152 bfd_vma offset,
7153 const char **filename_ptr,
7154 const char **functionname_ptr,
7155 unsigned int *line_ptr)
7157 bfd_boolean found;
7159 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7160 filename_ptr, functionname_ptr,
7161 line_ptr))
7163 if (!*functionname_ptr)
7164 elf_find_function (abfd, section, symbols, offset,
7165 *filename_ptr ? NULL : filename_ptr,
7166 functionname_ptr);
7168 return TRUE;
7171 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7172 filename_ptr, functionname_ptr,
7173 line_ptr, 0,
7174 &elf_tdata (abfd)->dwarf2_find_line_info))
7176 if (!*functionname_ptr)
7177 elf_find_function (abfd, section, symbols, offset,
7178 *filename_ptr ? NULL : filename_ptr,
7179 functionname_ptr);
7181 return TRUE;
7184 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7185 &found, filename_ptr,
7186 functionname_ptr, line_ptr,
7187 &elf_tdata (abfd)->line_info))
7188 return FALSE;
7189 if (found && (*functionname_ptr || *line_ptr))
7190 return TRUE;
7192 if (symbols == NULL)
7193 return FALSE;
7195 if (! elf_find_function (abfd, section, symbols, offset,
7196 filename_ptr, functionname_ptr))
7197 return FALSE;
7199 *line_ptr = 0;
7200 return TRUE;
7203 /* Find the line for a symbol. */
7205 bfd_boolean
7206 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7207 const char **filename_ptr, unsigned int *line_ptr)
7209 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7210 filename_ptr, line_ptr, 0,
7211 &elf_tdata (abfd)->dwarf2_find_line_info);
7214 /* After a call to bfd_find_nearest_line, successive calls to
7215 bfd_find_inliner_info can be used to get source information about
7216 each level of function inlining that terminated at the address
7217 passed to bfd_find_nearest_line. Currently this is only supported
7218 for DWARF2 with appropriate DWARF3 extensions. */
7220 bfd_boolean
7221 _bfd_elf_find_inliner_info (bfd *abfd,
7222 const char **filename_ptr,
7223 const char **functionname_ptr,
7224 unsigned int *line_ptr)
7226 bfd_boolean found;
7227 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7228 functionname_ptr, line_ptr,
7229 & elf_tdata (abfd)->dwarf2_find_line_info);
7230 return found;
7234 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7236 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7237 int ret = bed->s->sizeof_ehdr;
7239 if (!info->relocatable)
7241 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7243 if (phdr_size == (bfd_size_type) -1)
7245 struct elf_segment_map *m;
7247 phdr_size = 0;
7248 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7249 phdr_size += bed->s->sizeof_phdr;
7251 if (phdr_size == 0)
7252 phdr_size = get_program_header_size (abfd, info);
7255 elf_tdata (abfd)->program_header_size = phdr_size;
7256 ret += phdr_size;
7259 return ret;
7262 bfd_boolean
7263 _bfd_elf_set_section_contents (bfd *abfd,
7264 sec_ptr section,
7265 const void *location,
7266 file_ptr offset,
7267 bfd_size_type count)
7269 Elf_Internal_Shdr *hdr;
7270 bfd_signed_vma pos;
7272 if (! abfd->output_has_begun
7273 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7274 return FALSE;
7276 hdr = &elf_section_data (section)->this_hdr;
7277 pos = hdr->sh_offset + offset;
7278 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7279 || bfd_bwrite (location, count, abfd) != count)
7280 return FALSE;
7282 return TRUE;
7285 void
7286 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7287 arelent *cache_ptr ATTRIBUTE_UNUSED,
7288 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7290 abort ();
7293 /* Try to convert a non-ELF reloc into an ELF one. */
7295 bfd_boolean
7296 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7298 /* Check whether we really have an ELF howto. */
7300 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7302 bfd_reloc_code_real_type code;
7303 reloc_howto_type *howto;
7305 /* Alien reloc: Try to determine its type to replace it with an
7306 equivalent ELF reloc. */
7308 if (areloc->howto->pc_relative)
7310 switch (areloc->howto->bitsize)
7312 case 8:
7313 code = BFD_RELOC_8_PCREL;
7314 break;
7315 case 12:
7316 code = BFD_RELOC_12_PCREL;
7317 break;
7318 case 16:
7319 code = BFD_RELOC_16_PCREL;
7320 break;
7321 case 24:
7322 code = BFD_RELOC_24_PCREL;
7323 break;
7324 case 32:
7325 code = BFD_RELOC_32_PCREL;
7326 break;
7327 case 64:
7328 code = BFD_RELOC_64_PCREL;
7329 break;
7330 default:
7331 goto fail;
7334 howto = bfd_reloc_type_lookup (abfd, code);
7336 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7338 if (howto->pcrel_offset)
7339 areloc->addend += areloc->address;
7340 else
7341 areloc->addend -= areloc->address; /* addend is unsigned!! */
7344 else
7346 switch (areloc->howto->bitsize)
7348 case 8:
7349 code = BFD_RELOC_8;
7350 break;
7351 case 14:
7352 code = BFD_RELOC_14;
7353 break;
7354 case 16:
7355 code = BFD_RELOC_16;
7356 break;
7357 case 26:
7358 code = BFD_RELOC_26;
7359 break;
7360 case 32:
7361 code = BFD_RELOC_32;
7362 break;
7363 case 64:
7364 code = BFD_RELOC_64;
7365 break;
7366 default:
7367 goto fail;
7370 howto = bfd_reloc_type_lookup (abfd, code);
7373 if (howto)
7374 areloc->howto = howto;
7375 else
7376 goto fail;
7379 return TRUE;
7381 fail:
7382 (*_bfd_error_handler)
7383 (_("%B: unsupported relocation type %s"),
7384 abfd, areloc->howto->name);
7385 bfd_set_error (bfd_error_bad_value);
7386 return FALSE;
7389 bfd_boolean
7390 _bfd_elf_close_and_cleanup (bfd *abfd)
7392 if (bfd_get_format (abfd) == bfd_object)
7394 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7395 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7396 _bfd_dwarf2_cleanup_debug_info (abfd);
7399 return _bfd_generic_close_and_cleanup (abfd);
7402 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7403 in the relocation's offset. Thus we cannot allow any sort of sanity
7404 range-checking to interfere. There is nothing else to do in processing
7405 this reloc. */
7407 bfd_reloc_status_type
7408 _bfd_elf_rel_vtable_reloc_fn
7409 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7410 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7411 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7412 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7414 return bfd_reloc_ok;
7417 /* Elf core file support. Much of this only works on native
7418 toolchains, since we rely on knowing the
7419 machine-dependent procfs structure in order to pick
7420 out details about the corefile. */
7422 #ifdef HAVE_SYS_PROCFS_H
7423 # include <sys/procfs.h>
7424 #endif
7426 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7428 static int
7429 elfcore_make_pid (bfd *abfd)
7431 return ((elf_tdata (abfd)->core_lwpid << 16)
7432 + (elf_tdata (abfd)->core_pid));
7435 /* If there isn't a section called NAME, make one, using
7436 data from SECT. Note, this function will generate a
7437 reference to NAME, so you shouldn't deallocate or
7438 overwrite it. */
7440 static bfd_boolean
7441 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7443 asection *sect2;
7445 if (bfd_get_section_by_name (abfd, name) != NULL)
7446 return TRUE;
7448 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7449 if (sect2 == NULL)
7450 return FALSE;
7452 sect2->size = sect->size;
7453 sect2->filepos = sect->filepos;
7454 sect2->alignment_power = sect->alignment_power;
7455 return TRUE;
7458 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7459 actually creates up to two pseudosections:
7460 - For the single-threaded case, a section named NAME, unless
7461 such a section already exists.
7462 - For the multi-threaded case, a section named "NAME/PID", where
7463 PID is elfcore_make_pid (abfd).
7464 Both pseudosections have identical contents. */
7465 bfd_boolean
7466 _bfd_elfcore_make_pseudosection (bfd *abfd,
7467 char *name,
7468 size_t size,
7469 ufile_ptr filepos)
7471 char buf[100];
7472 char *threaded_name;
7473 size_t len;
7474 asection *sect;
7476 /* Build the section name. */
7478 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7479 len = strlen (buf) + 1;
7480 threaded_name = (char *) bfd_alloc (abfd, len);
7481 if (threaded_name == NULL)
7482 return FALSE;
7483 memcpy (threaded_name, buf, len);
7485 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7486 SEC_HAS_CONTENTS);
7487 if (sect == NULL)
7488 return FALSE;
7489 sect->size = size;
7490 sect->filepos = filepos;
7491 sect->alignment_power = 2;
7493 return elfcore_maybe_make_sect (abfd, name, sect);
7496 /* prstatus_t exists on:
7497 solaris 2.5+
7498 linux 2.[01] + glibc
7499 unixware 4.2
7502 #if defined (HAVE_PRSTATUS_T)
7504 static bfd_boolean
7505 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7507 size_t size;
7508 int offset;
7510 if (note->descsz == sizeof (prstatus_t))
7512 prstatus_t prstat;
7514 size = sizeof (prstat.pr_reg);
7515 offset = offsetof (prstatus_t, pr_reg);
7516 memcpy (&prstat, note->descdata, sizeof (prstat));
7518 /* Do not overwrite the core signal if it
7519 has already been set by another thread. */
7520 if (elf_tdata (abfd)->core_signal == 0)
7521 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7522 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7524 /* pr_who exists on:
7525 solaris 2.5+
7526 unixware 4.2
7527 pr_who doesn't exist on:
7528 linux 2.[01]
7530 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7531 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7532 #endif
7534 #if defined (HAVE_PRSTATUS32_T)
7535 else if (note->descsz == sizeof (prstatus32_t))
7537 /* 64-bit host, 32-bit corefile */
7538 prstatus32_t prstat;
7540 size = sizeof (prstat.pr_reg);
7541 offset = offsetof (prstatus32_t, pr_reg);
7542 memcpy (&prstat, note->descdata, sizeof (prstat));
7544 /* Do not overwrite the core signal if it
7545 has already been set by another thread. */
7546 if (elf_tdata (abfd)->core_signal == 0)
7547 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7548 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7550 /* pr_who exists on:
7551 solaris 2.5+
7552 unixware 4.2
7553 pr_who doesn't exist on:
7554 linux 2.[01]
7556 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7557 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7558 #endif
7560 #endif /* HAVE_PRSTATUS32_T */
7561 else
7563 /* Fail - we don't know how to handle any other
7564 note size (ie. data object type). */
7565 return TRUE;
7568 /* Make a ".reg/999" section and a ".reg" section. */
7569 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7570 size, note->descpos + offset);
7572 #endif /* defined (HAVE_PRSTATUS_T) */
7574 /* Create a pseudosection containing the exact contents of NOTE. */
7575 static bfd_boolean
7576 elfcore_make_note_pseudosection (bfd *abfd,
7577 char *name,
7578 Elf_Internal_Note *note)
7580 return _bfd_elfcore_make_pseudosection (abfd, name,
7581 note->descsz, note->descpos);
7584 /* There isn't a consistent prfpregset_t across platforms,
7585 but it doesn't matter, because we don't have to pick this
7586 data structure apart. */
7588 static bfd_boolean
7589 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7591 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7594 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7595 type of NT_PRXFPREG. Just include the whole note's contents
7596 literally. */
7598 static bfd_boolean
7599 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7601 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7604 static bfd_boolean
7605 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7607 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7610 static bfd_boolean
7611 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7613 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7616 #if defined (HAVE_PRPSINFO_T)
7617 typedef prpsinfo_t elfcore_psinfo_t;
7618 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7619 typedef prpsinfo32_t elfcore_psinfo32_t;
7620 #endif
7621 #endif
7623 #if defined (HAVE_PSINFO_T)
7624 typedef psinfo_t elfcore_psinfo_t;
7625 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7626 typedef psinfo32_t elfcore_psinfo32_t;
7627 #endif
7628 #endif
7630 /* return a malloc'ed copy of a string at START which is at
7631 most MAX bytes long, possibly without a terminating '\0'.
7632 the copy will always have a terminating '\0'. */
7634 char *
7635 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7637 char *dups;
7638 char *end = (char *) memchr (start, '\0', max);
7639 size_t len;
7641 if (end == NULL)
7642 len = max;
7643 else
7644 len = end - start;
7646 dups = (char *) bfd_alloc (abfd, len + 1);
7647 if (dups == NULL)
7648 return NULL;
7650 memcpy (dups, start, len);
7651 dups[len] = '\0';
7653 return dups;
7656 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7657 static bfd_boolean
7658 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7660 if (note->descsz == sizeof (elfcore_psinfo_t))
7662 elfcore_psinfo_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 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7675 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7677 /* 64-bit host, 32-bit corefile */
7678 elfcore_psinfo32_t psinfo;
7680 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7682 elf_tdata (abfd)->core_program
7683 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7684 sizeof (psinfo.pr_fname));
7686 elf_tdata (abfd)->core_command
7687 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7688 sizeof (psinfo.pr_psargs));
7690 #endif
7692 else
7694 /* Fail - we don't know how to handle any other
7695 note size (ie. data object type). */
7696 return TRUE;
7699 /* Note that for some reason, a spurious space is tacked
7700 onto the end of the args in some (at least one anyway)
7701 implementations, so strip it off if it exists. */
7704 char *command = elf_tdata (abfd)->core_command;
7705 int n = strlen (command);
7707 if (0 < n && command[n - 1] == ' ')
7708 command[n - 1] = '\0';
7711 return TRUE;
7713 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7715 #if defined (HAVE_PSTATUS_T)
7716 static bfd_boolean
7717 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7719 if (note->descsz == sizeof (pstatus_t)
7720 #if defined (HAVE_PXSTATUS_T)
7721 || note->descsz == sizeof (pxstatus_t)
7722 #endif
7725 pstatus_t pstat;
7727 memcpy (&pstat, note->descdata, sizeof (pstat));
7729 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7731 #if defined (HAVE_PSTATUS32_T)
7732 else if (note->descsz == sizeof (pstatus32_t))
7734 /* 64-bit host, 32-bit corefile */
7735 pstatus32_t pstat;
7737 memcpy (&pstat, note->descdata, sizeof (pstat));
7739 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7741 #endif
7742 /* Could grab some more details from the "representative"
7743 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7744 NT_LWPSTATUS note, presumably. */
7746 return TRUE;
7748 #endif /* defined (HAVE_PSTATUS_T) */
7750 #if defined (HAVE_LWPSTATUS_T)
7751 static bfd_boolean
7752 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7754 lwpstatus_t lwpstat;
7755 char buf[100];
7756 char *name;
7757 size_t len;
7758 asection *sect;
7760 if (note->descsz != sizeof (lwpstat)
7761 #if defined (HAVE_LWPXSTATUS_T)
7762 && note->descsz != sizeof (lwpxstatus_t)
7763 #endif
7765 return TRUE;
7767 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7769 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7770 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7772 /* Make a ".reg/999" section. */
7774 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7775 len = strlen (buf) + 1;
7776 name = bfd_alloc (abfd, len);
7777 if (name == NULL)
7778 return FALSE;
7779 memcpy (name, buf, len);
7781 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7782 if (sect == NULL)
7783 return FALSE;
7785 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7786 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7787 sect->filepos = note->descpos
7788 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7789 #endif
7791 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7792 sect->size = sizeof (lwpstat.pr_reg);
7793 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7794 #endif
7796 sect->alignment_power = 2;
7798 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7799 return FALSE;
7801 /* Make a ".reg2/999" section */
7803 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7804 len = strlen (buf) + 1;
7805 name = bfd_alloc (abfd, len);
7806 if (name == NULL)
7807 return FALSE;
7808 memcpy (name, buf, len);
7810 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7811 if (sect == NULL)
7812 return FALSE;
7814 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7815 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7816 sect->filepos = note->descpos
7817 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7818 #endif
7820 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7821 sect->size = sizeof (lwpstat.pr_fpreg);
7822 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7823 #endif
7825 sect->alignment_power = 2;
7827 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7829 #endif /* defined (HAVE_LWPSTATUS_T) */
7831 static bfd_boolean
7832 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7834 char buf[30];
7835 char *name;
7836 size_t len;
7837 asection *sect;
7838 int type;
7839 int is_active_thread;
7840 bfd_vma base_addr;
7842 if (note->descsz < 728)
7843 return TRUE;
7845 if (! CONST_STRNEQ (note->namedata, "win32"))
7846 return TRUE;
7848 type = bfd_get_32 (abfd, note->descdata);
7850 switch (type)
7852 case 1 /* NOTE_INFO_PROCESS */:
7853 /* FIXME: need to add ->core_command. */
7854 /* process_info.pid */
7855 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7856 /* process_info.signal */
7857 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7858 break;
7860 case 2 /* NOTE_INFO_THREAD */:
7861 /* Make a ".reg/999" section. */
7862 /* thread_info.tid */
7863 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7865 len = strlen (buf) + 1;
7866 name = (char *) bfd_alloc (abfd, len);
7867 if (name == NULL)
7868 return FALSE;
7870 memcpy (name, buf, len);
7872 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7873 if (sect == NULL)
7874 return FALSE;
7876 /* sizeof (thread_info.thread_context) */
7877 sect->size = 716;
7878 /* offsetof (thread_info.thread_context) */
7879 sect->filepos = note->descpos + 12;
7880 sect->alignment_power = 2;
7882 /* thread_info.is_active_thread */
7883 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7885 if (is_active_thread)
7886 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7887 return FALSE;
7888 break;
7890 case 3 /* NOTE_INFO_MODULE */:
7891 /* Make a ".module/xxxxxxxx" section. */
7892 /* module_info.base_address */
7893 base_addr = bfd_get_32 (abfd, note->descdata + 4);
7894 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
7896 len = strlen (buf) + 1;
7897 name = (char *) bfd_alloc (abfd, len);
7898 if (name == NULL)
7899 return FALSE;
7901 memcpy (name, buf, len);
7903 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7905 if (sect == NULL)
7906 return FALSE;
7908 sect->size = note->descsz;
7909 sect->filepos = note->descpos;
7910 sect->alignment_power = 2;
7911 break;
7913 default:
7914 return TRUE;
7917 return TRUE;
7920 static bfd_boolean
7921 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7923 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7925 switch (note->type)
7927 default:
7928 return TRUE;
7930 case NT_PRSTATUS:
7931 if (bed->elf_backend_grok_prstatus)
7932 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7933 return TRUE;
7934 #if defined (HAVE_PRSTATUS_T)
7935 return elfcore_grok_prstatus (abfd, note);
7936 #else
7937 return TRUE;
7938 #endif
7940 #if defined (HAVE_PSTATUS_T)
7941 case NT_PSTATUS:
7942 return elfcore_grok_pstatus (abfd, note);
7943 #endif
7945 #if defined (HAVE_LWPSTATUS_T)
7946 case NT_LWPSTATUS:
7947 return elfcore_grok_lwpstatus (abfd, note);
7948 #endif
7950 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7951 return elfcore_grok_prfpreg (abfd, note);
7953 case NT_WIN32PSTATUS:
7954 return elfcore_grok_win32pstatus (abfd, note);
7956 case NT_PRXFPREG: /* Linux SSE extension */
7957 if (note->namesz == 6
7958 && strcmp (note->namedata, "LINUX") == 0)
7959 return elfcore_grok_prxfpreg (abfd, note);
7960 else
7961 return TRUE;
7963 case NT_PPC_VMX:
7964 if (note->namesz == 6
7965 && strcmp (note->namedata, "LINUX") == 0)
7966 return elfcore_grok_ppc_vmx (abfd, note);
7967 else
7968 return TRUE;
7970 case NT_PPC_VSX:
7971 if (note->namesz == 6
7972 && strcmp (note->namedata, "LINUX") == 0)
7973 return elfcore_grok_ppc_vsx (abfd, note);
7974 else
7975 return TRUE;
7977 case NT_PRPSINFO:
7978 case NT_PSINFO:
7979 if (bed->elf_backend_grok_psinfo)
7980 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7981 return TRUE;
7982 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7983 return elfcore_grok_psinfo (abfd, note);
7984 #else
7985 return TRUE;
7986 #endif
7988 case NT_AUXV:
7990 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7991 SEC_HAS_CONTENTS);
7993 if (sect == NULL)
7994 return FALSE;
7995 sect->size = note->descsz;
7996 sect->filepos = note->descpos;
7997 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7999 return TRUE;
8004 static bfd_boolean
8005 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8007 elf_tdata (abfd)->build_id_size = note->descsz;
8008 elf_tdata (abfd)->build_id = (bfd_byte *) bfd_alloc (abfd, note->descsz);
8009 if (elf_tdata (abfd)->build_id == NULL)
8010 return FALSE;
8012 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
8014 return TRUE;
8017 static bfd_boolean
8018 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8020 switch (note->type)
8022 default:
8023 return TRUE;
8025 case NT_GNU_BUILD_ID:
8026 return elfobj_grok_gnu_build_id (abfd, note);
8030 static bfd_boolean
8031 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8033 char *cp;
8035 cp = strchr (note->namedata, '@');
8036 if (cp != NULL)
8038 *lwpidp = atoi(cp + 1);
8039 return TRUE;
8041 return FALSE;
8044 static bfd_boolean
8045 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8047 /* Signal number at offset 0x08. */
8048 elf_tdata (abfd)->core_signal
8049 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8051 /* Process ID at offset 0x50. */
8052 elf_tdata (abfd)->core_pid
8053 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8055 /* Command name at 0x7c (max 32 bytes, including nul). */
8056 elf_tdata (abfd)->core_command
8057 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8059 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8060 note);
8063 static bfd_boolean
8064 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8066 int lwp;
8068 if (elfcore_netbsd_get_lwpid (note, &lwp))
8069 elf_tdata (abfd)->core_lwpid = lwp;
8071 if (note->type == NT_NETBSDCORE_PROCINFO)
8073 /* NetBSD-specific core "procinfo". Note that we expect to
8074 find this note before any of the others, which is fine,
8075 since the kernel writes this note out first when it
8076 creates a core file. */
8078 return elfcore_grok_netbsd_procinfo (abfd, note);
8081 /* As of Jan 2002 there are no other machine-independent notes
8082 defined for NetBSD core files. If the note type is less
8083 than the start of the machine-dependent note types, we don't
8084 understand it. */
8086 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8087 return TRUE;
8090 switch (bfd_get_arch (abfd))
8092 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8093 PT_GETFPREGS == mach+2. */
8095 case bfd_arch_alpha:
8096 case bfd_arch_sparc:
8097 switch (note->type)
8099 case NT_NETBSDCORE_FIRSTMACH+0:
8100 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8102 case NT_NETBSDCORE_FIRSTMACH+2:
8103 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8105 default:
8106 return TRUE;
8109 /* On all other arch's, PT_GETREGS == mach+1 and
8110 PT_GETFPREGS == mach+3. */
8112 default:
8113 switch (note->type)
8115 case NT_NETBSDCORE_FIRSTMACH+1:
8116 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8118 case NT_NETBSDCORE_FIRSTMACH+3:
8119 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8121 default:
8122 return TRUE;
8125 /* NOTREACHED */
8128 static bfd_boolean
8129 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8131 /* Signal number at offset 0x08. */
8132 elf_tdata (abfd)->core_signal
8133 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8135 /* Process ID at offset 0x20. */
8136 elf_tdata (abfd)->core_pid
8137 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8139 /* Command name at 0x48 (max 32 bytes, including nul). */
8140 elf_tdata (abfd)->core_command
8141 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8143 return TRUE;
8146 static bfd_boolean
8147 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8149 if (note->type == NT_OPENBSD_PROCINFO)
8150 return elfcore_grok_openbsd_procinfo (abfd, note);
8152 if (note->type == NT_OPENBSD_REGS)
8153 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8155 if (note->type == NT_OPENBSD_FPREGS)
8156 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8158 if (note->type == NT_OPENBSD_XFPREGS)
8159 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8161 if (note->type == NT_OPENBSD_AUXV)
8163 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8164 SEC_HAS_CONTENTS);
8166 if (sect == NULL)
8167 return FALSE;
8168 sect->size = note->descsz;
8169 sect->filepos = note->descpos;
8170 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8172 return TRUE;
8175 if (note->type == NT_OPENBSD_WCOOKIE)
8177 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8178 SEC_HAS_CONTENTS);
8180 if (sect == NULL)
8181 return FALSE;
8182 sect->size = note->descsz;
8183 sect->filepos = note->descpos;
8184 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8186 return TRUE;
8189 return TRUE;
8192 static bfd_boolean
8193 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8195 void *ddata = note->descdata;
8196 char buf[100];
8197 char *name;
8198 asection *sect;
8199 short sig;
8200 unsigned flags;
8202 /* nto_procfs_status 'pid' field is at offset 0. */
8203 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8205 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8206 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8208 /* nto_procfs_status 'flags' field is at offset 8. */
8209 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8211 /* nto_procfs_status 'what' field is at offset 14. */
8212 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8214 elf_tdata (abfd)->core_signal = sig;
8215 elf_tdata (abfd)->core_lwpid = *tid;
8218 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8219 do not come from signals so we make sure we set the current
8220 thread just in case. */
8221 if (flags & 0x00000080)
8222 elf_tdata (abfd)->core_lwpid = *tid;
8224 /* Make a ".qnx_core_status/%d" section. */
8225 sprintf (buf, ".qnx_core_status/%ld", *tid);
8227 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8228 if (name == NULL)
8229 return FALSE;
8230 strcpy (name, buf);
8232 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8233 if (sect == NULL)
8234 return FALSE;
8236 sect->size = note->descsz;
8237 sect->filepos = note->descpos;
8238 sect->alignment_power = 2;
8240 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8243 static bfd_boolean
8244 elfcore_grok_nto_regs (bfd *abfd,
8245 Elf_Internal_Note *note,
8246 long tid,
8247 char *base)
8249 char buf[100];
8250 char *name;
8251 asection *sect;
8253 /* Make a "(base)/%d" section. */
8254 sprintf (buf, "%s/%ld", base, tid);
8256 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8257 if (name == NULL)
8258 return FALSE;
8259 strcpy (name, buf);
8261 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8262 if (sect == NULL)
8263 return FALSE;
8265 sect->size = note->descsz;
8266 sect->filepos = note->descpos;
8267 sect->alignment_power = 2;
8269 /* This is the current thread. */
8270 if (elf_tdata (abfd)->core_lwpid == tid)
8271 return elfcore_maybe_make_sect (abfd, base, sect);
8273 return TRUE;
8276 #define BFD_QNT_CORE_INFO 7
8277 #define BFD_QNT_CORE_STATUS 8
8278 #define BFD_QNT_CORE_GREG 9
8279 #define BFD_QNT_CORE_FPREG 10
8281 static bfd_boolean
8282 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8284 /* Every GREG section has a STATUS section before it. Store the
8285 tid from the previous call to pass down to the next gregs
8286 function. */
8287 static long tid = 1;
8289 switch (note->type)
8291 case BFD_QNT_CORE_INFO:
8292 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8293 case BFD_QNT_CORE_STATUS:
8294 return elfcore_grok_nto_status (abfd, note, &tid);
8295 case BFD_QNT_CORE_GREG:
8296 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8297 case BFD_QNT_CORE_FPREG:
8298 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8299 default:
8300 return TRUE;
8304 static bfd_boolean
8305 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8307 char *name;
8308 asection *sect;
8309 size_t len;
8311 /* Use note name as section name. */
8312 len = note->namesz;
8313 name = (char *) bfd_alloc (abfd, len);
8314 if (name == NULL)
8315 return FALSE;
8316 memcpy (name, note->namedata, len);
8317 name[len - 1] = '\0';
8319 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8320 if (sect == NULL)
8321 return FALSE;
8323 sect->size = note->descsz;
8324 sect->filepos = note->descpos;
8325 sect->alignment_power = 1;
8327 return TRUE;
8330 /* Function: elfcore_write_note
8332 Inputs:
8333 buffer to hold note, and current size of buffer
8334 name of note
8335 type of note
8336 data for note
8337 size of data for note
8339 Writes note to end of buffer. ELF64 notes are written exactly as
8340 for ELF32, despite the current (as of 2006) ELF gabi specifying
8341 that they ought to have 8-byte namesz and descsz field, and have
8342 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8344 Return:
8345 Pointer to realloc'd buffer, *BUFSIZ updated. */
8347 char *
8348 elfcore_write_note (bfd *abfd,
8349 char *buf,
8350 int *bufsiz,
8351 const char *name,
8352 int type,
8353 const void *input,
8354 int size)
8356 Elf_External_Note *xnp;
8357 size_t namesz;
8358 size_t newspace;
8359 char *dest;
8361 namesz = 0;
8362 if (name != NULL)
8363 namesz = strlen (name) + 1;
8365 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8367 buf = (char *) realloc (buf, *bufsiz + newspace);
8368 if (buf == NULL)
8369 return buf;
8370 dest = buf + *bufsiz;
8371 *bufsiz += newspace;
8372 xnp = (Elf_External_Note *) dest;
8373 H_PUT_32 (abfd, namesz, xnp->namesz);
8374 H_PUT_32 (abfd, size, xnp->descsz);
8375 H_PUT_32 (abfd, type, xnp->type);
8376 dest = xnp->name;
8377 if (name != NULL)
8379 memcpy (dest, name, namesz);
8380 dest += namesz;
8381 while (namesz & 3)
8383 *dest++ = '\0';
8384 ++namesz;
8387 memcpy (dest, input, size);
8388 dest += size;
8389 while (size & 3)
8391 *dest++ = '\0';
8392 ++size;
8394 return buf;
8397 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8398 char *
8399 elfcore_write_prpsinfo (bfd *abfd,
8400 char *buf,
8401 int *bufsiz,
8402 const char *fname,
8403 const char *psargs)
8405 const char *note_name = "CORE";
8406 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8408 if (bed->elf_backend_write_core_note != NULL)
8410 char *ret;
8411 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8412 NT_PRPSINFO, fname, psargs);
8413 if (ret != NULL)
8414 return ret;
8417 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8418 if (bed->s->elfclass == ELFCLASS32)
8420 #if defined (HAVE_PSINFO32_T)
8421 psinfo32_t data;
8422 int note_type = NT_PSINFO;
8423 #else
8424 prpsinfo32_t data;
8425 int note_type = NT_PRPSINFO;
8426 #endif
8428 memset (&data, 0, sizeof (data));
8429 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8430 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8431 return elfcore_write_note (abfd, buf, bufsiz,
8432 note_name, note_type, &data, sizeof (data));
8434 else
8435 #endif
8437 #if defined (HAVE_PSINFO_T)
8438 psinfo_t data;
8439 int note_type = NT_PSINFO;
8440 #else
8441 prpsinfo_t data;
8442 int note_type = NT_PRPSINFO;
8443 #endif
8445 memset (&data, 0, sizeof (data));
8446 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8447 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8448 return elfcore_write_note (abfd, buf, bufsiz,
8449 note_name, note_type, &data, sizeof (data));
8452 #endif /* PSINFO_T or PRPSINFO_T */
8454 #if defined (HAVE_PRSTATUS_T)
8455 char *
8456 elfcore_write_prstatus (bfd *abfd,
8457 char *buf,
8458 int *bufsiz,
8459 long pid,
8460 int cursig,
8461 const void *gregs)
8463 const char *note_name = "CORE";
8464 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8466 if (bed->elf_backend_write_core_note != NULL)
8468 char *ret;
8469 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8470 NT_PRSTATUS,
8471 pid, cursig, gregs);
8472 if (ret != NULL)
8473 return ret;
8476 #if defined (HAVE_PRSTATUS32_T)
8477 if (bed->s->elfclass == ELFCLASS32)
8479 prstatus32_t prstat;
8481 memset (&prstat, 0, sizeof (prstat));
8482 prstat.pr_pid = pid;
8483 prstat.pr_cursig = cursig;
8484 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8485 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8486 NT_PRSTATUS, &prstat, sizeof (prstat));
8488 else
8489 #endif
8491 prstatus_t prstat;
8493 memset (&prstat, 0, sizeof (prstat));
8494 prstat.pr_pid = pid;
8495 prstat.pr_cursig = cursig;
8496 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8497 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8498 NT_PRSTATUS, &prstat, sizeof (prstat));
8501 #endif /* HAVE_PRSTATUS_T */
8503 #if defined (HAVE_LWPSTATUS_T)
8504 char *
8505 elfcore_write_lwpstatus (bfd *abfd,
8506 char *buf,
8507 int *bufsiz,
8508 long pid,
8509 int cursig,
8510 const void *gregs)
8512 lwpstatus_t lwpstat;
8513 const char *note_name = "CORE";
8515 memset (&lwpstat, 0, sizeof (lwpstat));
8516 lwpstat.pr_lwpid = pid >> 16;
8517 lwpstat.pr_cursig = cursig;
8518 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8519 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8520 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8521 #if !defined(gregs)
8522 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8523 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8524 #else
8525 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8526 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8527 #endif
8528 #endif
8529 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8530 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8532 #endif /* HAVE_LWPSTATUS_T */
8534 #if defined (HAVE_PSTATUS_T)
8535 char *
8536 elfcore_write_pstatus (bfd *abfd,
8537 char *buf,
8538 int *bufsiz,
8539 long pid,
8540 int cursig ATTRIBUTE_UNUSED,
8541 const void *gregs ATTRIBUTE_UNUSED)
8543 const char *note_name = "CORE";
8544 #if defined (HAVE_PSTATUS32_T)
8545 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8547 if (bed->s->elfclass == ELFCLASS32)
8549 pstatus32_t pstat;
8551 memset (&pstat, 0, sizeof (pstat));
8552 pstat.pr_pid = pid & 0xffff;
8553 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8554 NT_PSTATUS, &pstat, sizeof (pstat));
8555 return buf;
8557 else
8558 #endif
8560 pstatus_t pstat;
8562 memset (&pstat, 0, sizeof (pstat));
8563 pstat.pr_pid = pid & 0xffff;
8564 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8565 NT_PSTATUS, &pstat, sizeof (pstat));
8566 return buf;
8569 #endif /* HAVE_PSTATUS_T */
8571 char *
8572 elfcore_write_prfpreg (bfd *abfd,
8573 char *buf,
8574 int *bufsiz,
8575 const void *fpregs,
8576 int size)
8578 const char *note_name = "CORE";
8579 return elfcore_write_note (abfd, buf, bufsiz,
8580 note_name, NT_FPREGSET, fpregs, size);
8583 char *
8584 elfcore_write_prxfpreg (bfd *abfd,
8585 char *buf,
8586 int *bufsiz,
8587 const void *xfpregs,
8588 int size)
8590 char *note_name = "LINUX";
8591 return elfcore_write_note (abfd, buf, bufsiz,
8592 note_name, NT_PRXFPREG, xfpregs, size);
8595 char *
8596 elfcore_write_ppc_vmx (bfd *abfd,
8597 char *buf,
8598 int *bufsiz,
8599 const void *ppc_vmx,
8600 int size)
8602 char *note_name = "LINUX";
8603 return elfcore_write_note (abfd, buf, bufsiz,
8604 note_name, NT_PPC_VMX, ppc_vmx, size);
8607 char *
8608 elfcore_write_ppc_vsx (bfd *abfd,
8609 char *buf,
8610 int *bufsiz,
8611 const void *ppc_vsx,
8612 int size)
8614 char *note_name = "LINUX";
8615 return elfcore_write_note (abfd, buf, bufsiz,
8616 note_name, NT_PPC_VSX, ppc_vsx, size);
8619 char *
8620 elfcore_write_register_note (bfd *abfd,
8621 char *buf,
8622 int *bufsiz,
8623 const char *section,
8624 const void *data,
8625 int size)
8627 if (strcmp (section, ".reg2") == 0)
8628 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8629 if (strcmp (section, ".reg-xfp") == 0)
8630 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8631 if (strcmp (section, ".reg-ppc-vmx") == 0)
8632 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8633 if (strcmp (section, ".reg-ppc-vsx") == 0)
8634 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
8635 return NULL;
8638 static bfd_boolean
8639 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8641 char *p;
8643 p = buf;
8644 while (p < buf + size)
8646 /* FIXME: bad alignment assumption. */
8647 Elf_External_Note *xnp = (Elf_External_Note *) p;
8648 Elf_Internal_Note in;
8650 if (offsetof (Elf_External_Note, name) > buf - p + size)
8651 return FALSE;
8653 in.type = H_GET_32 (abfd, xnp->type);
8655 in.namesz = H_GET_32 (abfd, xnp->namesz);
8656 in.namedata = xnp->name;
8657 if (in.namesz > buf - in.namedata + size)
8658 return FALSE;
8660 in.descsz = H_GET_32 (abfd, xnp->descsz);
8661 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8662 in.descpos = offset + (in.descdata - buf);
8663 if (in.descsz != 0
8664 && (in.descdata >= buf + size
8665 || in.descsz > buf - in.descdata + size))
8666 return FALSE;
8668 switch (bfd_get_format (abfd))
8670 default:
8671 return TRUE;
8673 case bfd_core:
8674 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8676 if (! elfcore_grok_netbsd_note (abfd, &in))
8677 return FALSE;
8679 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
8681 if (! elfcore_grok_openbsd_note (abfd, &in))
8682 return FALSE;
8684 else if (CONST_STRNEQ (in.namedata, "QNX"))
8686 if (! elfcore_grok_nto_note (abfd, &in))
8687 return FALSE;
8689 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8691 if (! elfcore_grok_spu_note (abfd, &in))
8692 return FALSE;
8694 else
8696 if (! elfcore_grok_note (abfd, &in))
8697 return FALSE;
8699 break;
8701 case bfd_object:
8702 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8704 if (! elfobj_grok_gnu_note (abfd, &in))
8705 return FALSE;
8707 break;
8710 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8713 return TRUE;
8716 static bfd_boolean
8717 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8719 char *buf;
8721 if (size <= 0)
8722 return TRUE;
8724 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8725 return FALSE;
8727 buf = (char *) bfd_malloc (size);
8728 if (buf == NULL)
8729 return FALSE;
8731 if (bfd_bread (buf, size, abfd) != size
8732 || !elf_parse_notes (abfd, buf, size, offset))
8734 free (buf);
8735 return FALSE;
8738 free (buf);
8739 return TRUE;
8742 /* Providing external access to the ELF program header table. */
8744 /* Return an upper bound on the number of bytes required to store a
8745 copy of ABFD's program header table entries. Return -1 if an error
8746 occurs; bfd_get_error will return an appropriate code. */
8748 long
8749 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8751 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8753 bfd_set_error (bfd_error_wrong_format);
8754 return -1;
8757 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8760 /* Copy ABFD's program header table entries to *PHDRS. The entries
8761 will be stored as an array of Elf_Internal_Phdr structures, as
8762 defined in include/elf/internal.h. To find out how large the
8763 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8765 Return the number of program header table entries read, or -1 if an
8766 error occurs; bfd_get_error will return an appropriate code. */
8769 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8771 int num_phdrs;
8773 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8775 bfd_set_error (bfd_error_wrong_format);
8776 return -1;
8779 num_phdrs = elf_elfheader (abfd)->e_phnum;
8780 memcpy (phdrs, elf_tdata (abfd)->phdr,
8781 num_phdrs * sizeof (Elf_Internal_Phdr));
8783 return num_phdrs;
8786 enum elf_reloc_type_class
8787 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8789 return reloc_class_normal;
8792 /* For RELA architectures, return the relocation value for a
8793 relocation against a local symbol. */
8795 bfd_vma
8796 _bfd_elf_rela_local_sym (bfd *abfd,
8797 Elf_Internal_Sym *sym,
8798 asection **psec,
8799 Elf_Internal_Rela *rel)
8801 asection *sec = *psec;
8802 bfd_vma relocation;
8804 relocation = (sec->output_section->vma
8805 + sec->output_offset
8806 + sym->st_value);
8807 if ((sec->flags & SEC_MERGE)
8808 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8809 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8811 rel->r_addend =
8812 _bfd_merged_section_offset (abfd, psec,
8813 elf_section_data (sec)->sec_info,
8814 sym->st_value + rel->r_addend);
8815 if (sec != *psec)
8817 /* If we have changed the section, and our original section is
8818 marked with SEC_EXCLUDE, it means that the original
8819 SEC_MERGE section has been completely subsumed in some
8820 other SEC_MERGE section. In this case, we need to leave
8821 some info around for --emit-relocs. */
8822 if ((sec->flags & SEC_EXCLUDE) != 0)
8823 sec->kept_section = *psec;
8824 sec = *psec;
8826 rel->r_addend -= relocation;
8827 rel->r_addend += sec->output_section->vma + sec->output_offset;
8829 return relocation;
8832 bfd_vma
8833 _bfd_elf_rel_local_sym (bfd *abfd,
8834 Elf_Internal_Sym *sym,
8835 asection **psec,
8836 bfd_vma addend)
8838 asection *sec = *psec;
8840 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8841 return sym->st_value + addend;
8843 return _bfd_merged_section_offset (abfd, psec,
8844 elf_section_data (sec)->sec_info,
8845 sym->st_value + addend);
8848 bfd_vma
8849 _bfd_elf_section_offset (bfd *abfd,
8850 struct bfd_link_info *info,
8851 asection *sec,
8852 bfd_vma offset)
8854 switch (sec->sec_info_type)
8856 case ELF_INFO_TYPE_STABS:
8857 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8858 offset);
8859 case ELF_INFO_TYPE_EH_FRAME:
8860 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8861 default:
8862 return offset;
8866 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8867 reconstruct an ELF file by reading the segments out of remote memory
8868 based on the ELF file header at EHDR_VMA and the ELF program headers it
8869 points to. If not null, *LOADBASEP is filled in with the difference
8870 between the VMAs from which the segments were read, and the VMAs the
8871 file headers (and hence BFD's idea of each section's VMA) put them at.
8873 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8874 remote memory at target address VMA into the local buffer at MYADDR; it
8875 should return zero on success or an `errno' code on failure. TEMPL must
8876 be a BFD for an ELF target with the word size and byte order found in
8877 the remote memory. */
8879 bfd *
8880 bfd_elf_bfd_from_remote_memory
8881 (bfd *templ,
8882 bfd_vma ehdr_vma,
8883 bfd_vma *loadbasep,
8884 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8886 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8887 (templ, ehdr_vma, loadbasep, target_read_memory);
8890 long
8891 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8892 long symcount ATTRIBUTE_UNUSED,
8893 asymbol **syms ATTRIBUTE_UNUSED,
8894 long dynsymcount,
8895 asymbol **dynsyms,
8896 asymbol **ret)
8898 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8899 asection *relplt;
8900 asymbol *s;
8901 const char *relplt_name;
8902 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8903 arelent *p;
8904 long count, i, n;
8905 size_t size;
8906 Elf_Internal_Shdr *hdr;
8907 char *names;
8908 asection *plt;
8910 *ret = NULL;
8912 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8913 return 0;
8915 if (dynsymcount <= 0)
8916 return 0;
8918 if (!bed->plt_sym_val)
8919 return 0;
8921 relplt_name = bed->relplt_name;
8922 if (relplt_name == NULL)
8923 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
8924 relplt = bfd_get_section_by_name (abfd, relplt_name);
8925 if (relplt == NULL)
8926 return 0;
8928 hdr = &elf_section_data (relplt)->this_hdr;
8929 if (hdr->sh_link != elf_dynsymtab (abfd)
8930 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8931 return 0;
8933 plt = bfd_get_section_by_name (abfd, ".plt");
8934 if (plt == NULL)
8935 return 0;
8937 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8938 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8939 return -1;
8941 count = relplt->size / hdr->sh_entsize;
8942 size = count * sizeof (asymbol);
8943 p = relplt->relocation;
8944 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8946 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8947 if (p->addend != 0)
8949 #ifdef BFD64
8950 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
8951 #else
8952 size += sizeof ("+0x") - 1 + 8;
8953 #endif
8957 s = *ret = (asymbol *) bfd_malloc (size);
8958 if (s == NULL)
8959 return -1;
8961 names = (char *) (s + count);
8962 p = relplt->relocation;
8963 n = 0;
8964 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8966 size_t len;
8967 bfd_vma addr;
8969 addr = bed->plt_sym_val (i, plt, p);
8970 if (addr == (bfd_vma) -1)
8971 continue;
8973 *s = **p->sym_ptr_ptr;
8974 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8975 we are defining a symbol, ensure one of them is set. */
8976 if ((s->flags & BSF_LOCAL) == 0)
8977 s->flags |= BSF_GLOBAL;
8978 s->flags |= BSF_SYNTHETIC;
8979 s->section = plt;
8980 s->value = addr - plt->vma;
8981 s->name = names;
8982 s->udata.p = NULL;
8983 len = strlen ((*p->sym_ptr_ptr)->name);
8984 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8985 names += len;
8986 if (p->addend != 0)
8988 char buf[30], *a;
8989 int len;
8990 memcpy (names, "+0x", sizeof ("+0x") - 1);
8991 names += sizeof ("+0x") - 1;
8992 bfd_sprintf_vma (abfd, buf, p->addend);
8993 for (a = buf; *a == '0'; ++a)
8995 len = strlen (a);
8996 memcpy (names, a, len);
8997 names += len;
8999 memcpy (names, "@plt", sizeof ("@plt"));
9000 names += sizeof ("@plt");
9001 ++s, ++n;
9004 return n;
9007 /* It is only used by x86-64 so far. */
9008 asection _bfd_elf_large_com_section
9009 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
9010 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
9012 void
9013 _bfd_elf_set_osabi (bfd * abfd,
9014 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
9016 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
9018 i_ehdrp = elf_elfheader (abfd);
9020 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
9022 /* To make things simpler for the loader on Linux systems we set the
9023 osabi field to ELFOSABI_LINUX if the binary contains symbols of
9024 the STT_GNU_IFUNC type. */
9025 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
9026 && elf_tdata (abfd)->has_ifunc_symbols)
9027 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
9031 /* Return TRUE for ELF symbol types that represent functions.
9032 This is the default version of this function, which is sufficient for
9033 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
9035 bfd_boolean
9036 _bfd_elf_is_function_type (unsigned int type)
9038 return (type == STT_FUNC
9039 || type == STT_GNU_IFUNC);