Mach-O: add objdump -P function_starts to display function starts.
[binutils-gdb.git] / bfd / elf.c
blob3ded683efe45035b58c0f1bce95c4da03f030c37
1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2014 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
24 SECTION
25 ELF backends
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
36 #define _SYSCALL32
37 #include "sysdep.h"
38 #include "bfd.h"
39 #include "bfdlink.h"
40 #include "libbfd.h"
41 #define ARCH_SIZE 0
42 #include "elf-bfd.h"
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-psinfo.h"
47 #ifdef CORE_HEADER
48 #include CORE_HEADER
49 #endif
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
53 static bfd_boolean prep_headers (bfd *);
54 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
55 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
56 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 file_ptr offset);
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
65 void
66 _bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
70 dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 dst->vd_next = H_GET_32 (abfd, src->vd_next);
79 /* Swap out a Verdef structure. */
81 void
82 _bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
86 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
95 /* Swap in a Verdaux structure. */
97 void
98 _bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
106 /* Swap out a Verdaux structure. */
108 void
109 _bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
117 /* Swap in a Verneed structure. */
119 void
120 _bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
124 dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 dst->vn_next = H_GET_32 (abfd, src->vn_next);
131 /* Swap out a Verneed structure. */
133 void
134 _bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
138 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
145 /* Swap in a Vernaux structure. */
147 void
148 _bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 dst->vna_next = H_GET_32 (abfd, src->vna_next);
159 /* Swap out a Vernaux structure. */
161 void
162 _bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
173 /* Swap in a Versym structure. */
175 void
176 _bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
183 /* Swap out a Versym structure. */
185 void
186 _bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
196 unsigned long
197 bfd_elf_hash (const char *namearg)
199 const unsigned char *name = (const unsigned char *) namearg;
200 unsigned long h = 0;
201 unsigned long g;
202 int ch;
204 while ((ch = *name++) != '\0')
206 h = (h << 4) + ch;
207 if ((g = (h & 0xf0000000)) != 0)
209 h ^= g >> 24;
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
212 h ^= g;
215 return h & 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
221 unsigned long
222 bfd_elf_gnu_hash (const char *namearg)
224 const unsigned char *name = (const unsigned char *) namearg;
225 unsigned long h = 5381;
226 unsigned char ch;
228 while ((ch = *name++) != '\0')
229 h = (h << 5) + h + ch;
230 return h & 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
235 bfd_boolean
236 bfd_elf_allocate_object (bfd *abfd,
237 size_t object_size,
238 enum elf_target_id object_id)
240 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241 abfd->tdata.any = bfd_zalloc (abfd, object_size);
242 if (abfd->tdata.any == NULL)
243 return FALSE;
245 elf_object_id (abfd) = object_id;
246 if (abfd->direction != read_direction)
248 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
249 if (o == NULL)
250 return FALSE;
251 elf_tdata (abfd)->o = o;
252 elf_program_header_size (abfd) = (bfd_size_type) -1;
254 return TRUE;
258 bfd_boolean
259 bfd_elf_make_object (bfd *abfd)
261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
262 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
263 bed->target_id);
266 bfd_boolean
267 bfd_elf_mkcorefile (bfd *abfd)
269 /* I think this can be done just like an object file. */
270 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
271 return FALSE;
272 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273 return elf_tdata (abfd)->core != NULL;
276 static char *
277 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
279 Elf_Internal_Shdr **i_shdrp;
280 bfd_byte *shstrtab = NULL;
281 file_ptr offset;
282 bfd_size_type shstrtabsize;
284 i_shdrp = elf_elfsections (abfd);
285 if (i_shdrp == 0
286 || shindex >= elf_numsections (abfd)
287 || i_shdrp[shindex] == 0)
288 return NULL;
290 shstrtab = i_shdrp[shindex]->contents;
291 if (shstrtab == NULL)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset = i_shdrp[shindex]->sh_offset;
295 shstrtabsize = i_shdrp[shindex]->sh_size;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize + 1 <= 1
300 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL
301 || bfd_seek (abfd, offset, SEEK_SET) != 0)
302 shstrtab = NULL;
303 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
305 if (bfd_get_error () != bfd_error_system_call)
306 bfd_set_error (bfd_error_file_truncated);
307 shstrtab = NULL;
308 /* Once we've failed to read it, make sure we don't keep
309 trying. Otherwise, we'll keep allocating space for
310 the string table over and over. */
311 i_shdrp[shindex]->sh_size = 0;
313 else
314 shstrtab[shstrtabsize] = '\0';
315 i_shdrp[shindex]->contents = shstrtab;
317 return (char *) shstrtab;
320 char *
321 bfd_elf_string_from_elf_section (bfd *abfd,
322 unsigned int shindex,
323 unsigned int strindex)
325 Elf_Internal_Shdr *hdr;
327 if (strindex == 0)
328 return "";
330 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
331 return NULL;
333 hdr = elf_elfsections (abfd)[shindex];
335 if (hdr->contents == NULL
336 && bfd_elf_get_str_section (abfd, shindex) == NULL)
337 return NULL;
339 if (strindex >= hdr->sh_size)
341 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
342 (*_bfd_error_handler)
343 (_("%B: invalid string offset %u >= %lu for section `%s'"),
344 abfd, strindex, (unsigned long) hdr->sh_size,
345 (shindex == shstrndx && strindex == hdr->sh_name
346 ? ".shstrtab"
347 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
348 return NULL;
351 return ((char *) hdr->contents) + strindex;
354 /* Read and convert symbols to internal format.
355 SYMCOUNT specifies the number of symbols to read, starting from
356 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
357 are non-NULL, they are used to store the internal symbols, external
358 symbols, and symbol section index extensions, respectively.
359 Returns a pointer to the internal symbol buffer (malloced if necessary)
360 or NULL if there were no symbols or some kind of problem. */
362 Elf_Internal_Sym *
363 bfd_elf_get_elf_syms (bfd *ibfd,
364 Elf_Internal_Shdr *symtab_hdr,
365 size_t symcount,
366 size_t symoffset,
367 Elf_Internal_Sym *intsym_buf,
368 void *extsym_buf,
369 Elf_External_Sym_Shndx *extshndx_buf)
371 Elf_Internal_Shdr *shndx_hdr;
372 void *alloc_ext;
373 const bfd_byte *esym;
374 Elf_External_Sym_Shndx *alloc_extshndx;
375 Elf_External_Sym_Shndx *shndx;
376 Elf_Internal_Sym *alloc_intsym;
377 Elf_Internal_Sym *isym;
378 Elf_Internal_Sym *isymend;
379 const struct elf_backend_data *bed;
380 size_t extsym_size;
381 bfd_size_type amt;
382 file_ptr pos;
384 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
385 abort ();
387 if (symcount == 0)
388 return intsym_buf;
390 /* Normal syms might have section extension entries. */
391 shndx_hdr = NULL;
392 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
393 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
395 /* Read the symbols. */
396 alloc_ext = NULL;
397 alloc_extshndx = NULL;
398 alloc_intsym = NULL;
399 bed = get_elf_backend_data (ibfd);
400 extsym_size = bed->s->sizeof_sym;
401 amt = symcount * extsym_size;
402 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
403 if (extsym_buf == NULL)
405 alloc_ext = bfd_malloc2 (symcount, extsym_size);
406 extsym_buf = alloc_ext;
408 if (extsym_buf == NULL
409 || bfd_seek (ibfd, pos, SEEK_SET) != 0
410 || bfd_bread (extsym_buf, amt, ibfd) != amt)
412 intsym_buf = NULL;
413 goto out;
416 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
417 extshndx_buf = NULL;
418 else
420 amt = symcount * sizeof (Elf_External_Sym_Shndx);
421 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
422 if (extshndx_buf == NULL)
424 alloc_extshndx = (Elf_External_Sym_Shndx *)
425 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
426 extshndx_buf = alloc_extshndx;
428 if (extshndx_buf == NULL
429 || bfd_seek (ibfd, pos, SEEK_SET) != 0
430 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
432 intsym_buf = NULL;
433 goto out;
437 if (intsym_buf == NULL)
439 alloc_intsym = (Elf_Internal_Sym *)
440 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
441 intsym_buf = alloc_intsym;
442 if (intsym_buf == NULL)
443 goto out;
446 /* Convert the symbols to internal form. */
447 isymend = intsym_buf + symcount;
448 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
449 shndx = extshndx_buf;
450 isym < isymend;
451 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
452 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
454 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
455 (*_bfd_error_handler) (_("%B symbol number %lu references "
456 "nonexistent SHT_SYMTAB_SHNDX section"),
457 ibfd, (unsigned long) symoffset);
458 if (alloc_intsym != NULL)
459 free (alloc_intsym);
460 intsym_buf = NULL;
461 goto out;
464 out:
465 if (alloc_ext != NULL)
466 free (alloc_ext);
467 if (alloc_extshndx != NULL)
468 free (alloc_extshndx);
470 return intsym_buf;
473 /* Look up a symbol name. */
474 const char *
475 bfd_elf_sym_name (bfd *abfd,
476 Elf_Internal_Shdr *symtab_hdr,
477 Elf_Internal_Sym *isym,
478 asection *sym_sec)
480 const char *name;
481 unsigned int iname = isym->st_name;
482 unsigned int shindex = symtab_hdr->sh_link;
484 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
485 /* Check for a bogus st_shndx to avoid crashing. */
486 && isym->st_shndx < elf_numsections (abfd))
488 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
489 shindex = elf_elfheader (abfd)->e_shstrndx;
492 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
493 if (name == NULL)
494 name = "(null)";
495 else if (sym_sec && *name == '\0')
496 name = bfd_section_name (abfd, sym_sec);
498 return name;
501 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
502 sections. The first element is the flags, the rest are section
503 pointers. */
505 typedef union elf_internal_group {
506 Elf_Internal_Shdr *shdr;
507 unsigned int flags;
508 } Elf_Internal_Group;
510 /* Return the name of the group signature symbol. Why isn't the
511 signature just a string? */
513 static const char *
514 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
516 Elf_Internal_Shdr *hdr;
517 unsigned char esym[sizeof (Elf64_External_Sym)];
518 Elf_External_Sym_Shndx eshndx;
519 Elf_Internal_Sym isym;
521 /* First we need to ensure the symbol table is available. Make sure
522 that it is a symbol table section. */
523 if (ghdr->sh_link >= elf_numsections (abfd))
524 return NULL;
525 hdr = elf_elfsections (abfd) [ghdr->sh_link];
526 if (hdr->sh_type != SHT_SYMTAB
527 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
528 return NULL;
530 /* Go read the symbol. */
531 hdr = &elf_tdata (abfd)->symtab_hdr;
532 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
533 &isym, esym, &eshndx) == NULL)
534 return NULL;
536 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
539 /* Set next_in_group list pointer, and group name for NEWSECT. */
541 static bfd_boolean
542 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
544 unsigned int num_group = elf_tdata (abfd)->num_group;
546 /* If num_group is zero, read in all SHT_GROUP sections. The count
547 is set to -1 if there are no SHT_GROUP sections. */
548 if (num_group == 0)
550 unsigned int i, shnum;
552 /* First count the number of groups. If we have a SHT_GROUP
553 section with just a flag word (ie. sh_size is 4), ignore it. */
554 shnum = elf_numsections (abfd);
555 num_group = 0;
557 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
558 ( (shdr)->sh_type == SHT_GROUP \
559 && (shdr)->sh_size >= minsize \
560 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
561 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
563 for (i = 0; i < shnum; i++)
565 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
567 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
568 num_group += 1;
571 if (num_group == 0)
573 num_group = (unsigned) -1;
574 elf_tdata (abfd)->num_group = num_group;
576 else
578 /* We keep a list of elf section headers for group sections,
579 so we can find them quickly. */
580 bfd_size_type amt;
582 elf_tdata (abfd)->num_group = num_group;
583 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
584 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
585 if (elf_tdata (abfd)->group_sect_ptr == NULL)
586 return FALSE;
588 num_group = 0;
589 for (i = 0; i < shnum; i++)
591 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
593 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
595 unsigned char *src;
596 Elf_Internal_Group *dest;
598 /* Add to list of sections. */
599 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
600 num_group += 1;
602 /* Read the raw contents. */
603 BFD_ASSERT (sizeof (*dest) >= 4);
604 amt = shdr->sh_size * sizeof (*dest) / 4;
605 shdr->contents = (unsigned char *)
606 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
607 /* PR binutils/4110: Handle corrupt group headers. */
608 if (shdr->contents == NULL)
610 _bfd_error_handler
611 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
612 bfd_set_error (bfd_error_bad_value);
613 return FALSE;
616 memset (shdr->contents, 0, amt);
618 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
619 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
620 != shdr->sh_size))
621 return FALSE;
623 /* Translate raw contents, a flag word followed by an
624 array of elf section indices all in target byte order,
625 to the flag word followed by an array of elf section
626 pointers. */
627 src = shdr->contents + shdr->sh_size;
628 dest = (Elf_Internal_Group *) (shdr->contents + amt);
629 while (1)
631 unsigned int idx;
633 src -= 4;
634 --dest;
635 idx = H_GET_32 (abfd, src);
636 if (src == shdr->contents)
638 dest->flags = idx;
639 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
640 shdr->bfd_section->flags
641 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
642 break;
644 if (idx >= shnum)
646 ((*_bfd_error_handler)
647 (_("%B: invalid SHT_GROUP entry"), abfd));
648 idx = 0;
650 dest->shdr = elf_elfsections (abfd)[idx];
657 if (num_group != (unsigned) -1)
659 unsigned int i;
661 for (i = 0; i < num_group; i++)
663 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
664 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
665 unsigned int n_elt = shdr->sh_size / 4;
667 /* Look through this group's sections to see if current
668 section is a member. */
669 while (--n_elt != 0)
670 if ((++idx)->shdr == hdr)
672 asection *s = NULL;
674 /* We are a member of this group. Go looking through
675 other members to see if any others are linked via
676 next_in_group. */
677 idx = (Elf_Internal_Group *) shdr->contents;
678 n_elt = shdr->sh_size / 4;
679 while (--n_elt != 0)
680 if ((s = (++idx)->shdr->bfd_section) != NULL
681 && elf_next_in_group (s) != NULL)
682 break;
683 if (n_elt != 0)
685 /* Snarf the group name from other member, and
686 insert current section in circular list. */
687 elf_group_name (newsect) = elf_group_name (s);
688 elf_next_in_group (newsect) = elf_next_in_group (s);
689 elf_next_in_group (s) = newsect;
691 else
693 const char *gname;
695 gname = group_signature (abfd, shdr);
696 if (gname == NULL)
697 return FALSE;
698 elf_group_name (newsect) = gname;
700 /* Start a circular list with one element. */
701 elf_next_in_group (newsect) = newsect;
704 /* If the group section has been created, point to the
705 new member. */
706 if (shdr->bfd_section != NULL)
707 elf_next_in_group (shdr->bfd_section) = newsect;
709 i = num_group - 1;
710 break;
715 if (elf_group_name (newsect) == NULL)
717 (*_bfd_error_handler) (_("%B: no group info for section %A"),
718 abfd, newsect);
720 return TRUE;
723 bfd_boolean
724 _bfd_elf_setup_sections (bfd *abfd)
726 unsigned int i;
727 unsigned int num_group = elf_tdata (abfd)->num_group;
728 bfd_boolean result = TRUE;
729 asection *s;
731 /* Process SHF_LINK_ORDER. */
732 for (s = abfd->sections; s != NULL; s = s->next)
734 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
735 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
737 unsigned int elfsec = this_hdr->sh_link;
738 /* FIXME: The old Intel compiler and old strip/objcopy may
739 not set the sh_link or sh_info fields. Hence we could
740 get the situation where elfsec is 0. */
741 if (elfsec == 0)
743 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
744 if (bed->link_order_error_handler)
745 bed->link_order_error_handler
746 (_("%B: warning: sh_link not set for section `%A'"),
747 abfd, s);
749 else
751 asection *linksec = NULL;
753 if (elfsec < elf_numsections (abfd))
755 this_hdr = elf_elfsections (abfd)[elfsec];
756 linksec = this_hdr->bfd_section;
759 /* PR 1991, 2008:
760 Some strip/objcopy may leave an incorrect value in
761 sh_link. We don't want to proceed. */
762 if (linksec == NULL)
764 (*_bfd_error_handler)
765 (_("%B: sh_link [%d] in section `%A' is incorrect"),
766 s->owner, s, elfsec);
767 result = FALSE;
770 elf_linked_to_section (s) = linksec;
775 /* Process section groups. */
776 if (num_group == (unsigned) -1)
777 return result;
779 for (i = 0; i < num_group; i++)
781 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
782 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
783 unsigned int n_elt = shdr->sh_size / 4;
785 while (--n_elt != 0)
786 if ((++idx)->shdr->bfd_section)
787 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
788 else if (idx->shdr->sh_type == SHT_RELA
789 || idx->shdr->sh_type == SHT_REL)
790 /* We won't include relocation sections in section groups in
791 output object files. We adjust the group section size here
792 so that relocatable link will work correctly when
793 relocation sections are in section group in input object
794 files. */
795 shdr->bfd_section->size -= 4;
796 else
798 /* There are some unknown sections in the group. */
799 (*_bfd_error_handler)
800 (_("%B: unknown [%d] section `%s' in group [%s]"),
801 abfd,
802 (unsigned int) idx->shdr->sh_type,
803 bfd_elf_string_from_elf_section (abfd,
804 (elf_elfheader (abfd)
805 ->e_shstrndx),
806 idx->shdr->sh_name),
807 shdr->bfd_section->name);
808 result = FALSE;
811 return result;
814 bfd_boolean
815 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
817 return elf_next_in_group (sec) != NULL;
820 /* Make a BFD section from an ELF section. We store a pointer to the
821 BFD section in the bfd_section field of the header. */
823 bfd_boolean
824 _bfd_elf_make_section_from_shdr (bfd *abfd,
825 Elf_Internal_Shdr *hdr,
826 const char *name,
827 int shindex)
829 asection *newsect;
830 flagword flags;
831 const struct elf_backend_data *bed;
833 if (hdr->bfd_section != NULL)
834 return TRUE;
836 newsect = bfd_make_section_anyway (abfd, name);
837 if (newsect == NULL)
838 return FALSE;
840 hdr->bfd_section = newsect;
841 elf_section_data (newsect)->this_hdr = *hdr;
842 elf_section_data (newsect)->this_idx = shindex;
844 /* Always use the real type/flags. */
845 elf_section_type (newsect) = hdr->sh_type;
846 elf_section_flags (newsect) = hdr->sh_flags;
848 newsect->filepos = hdr->sh_offset;
850 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
851 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
852 || ! bfd_set_section_alignment (abfd, newsect,
853 bfd_log2 (hdr->sh_addralign)))
854 return FALSE;
856 flags = SEC_NO_FLAGS;
857 if (hdr->sh_type != SHT_NOBITS)
858 flags |= SEC_HAS_CONTENTS;
859 if (hdr->sh_type == SHT_GROUP)
860 flags |= SEC_GROUP | SEC_EXCLUDE;
861 if ((hdr->sh_flags & SHF_ALLOC) != 0)
863 flags |= SEC_ALLOC;
864 if (hdr->sh_type != SHT_NOBITS)
865 flags |= SEC_LOAD;
867 if ((hdr->sh_flags & SHF_WRITE) == 0)
868 flags |= SEC_READONLY;
869 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
870 flags |= SEC_CODE;
871 else if ((flags & SEC_LOAD) != 0)
872 flags |= SEC_DATA;
873 if ((hdr->sh_flags & SHF_MERGE) != 0)
875 flags |= SEC_MERGE;
876 newsect->entsize = hdr->sh_entsize;
877 if ((hdr->sh_flags & SHF_STRINGS) != 0)
878 flags |= SEC_STRINGS;
880 if (hdr->sh_flags & SHF_GROUP)
881 if (!setup_group (abfd, hdr, newsect))
882 return FALSE;
883 if ((hdr->sh_flags & SHF_TLS) != 0)
884 flags |= SEC_THREAD_LOCAL;
885 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
886 flags |= SEC_EXCLUDE;
888 if ((flags & SEC_ALLOC) == 0)
890 /* The debugging sections appear to be recognized only by name,
891 not any sort of flag. Their SEC_ALLOC bits are cleared. */
892 if (name [0] == '.')
894 const char *p;
895 int n;
896 if (name[1] == 'd')
897 p = ".debug", n = 6;
898 else if (name[1] == 'g' && name[2] == 'n')
899 p = ".gnu.linkonce.wi.", n = 17;
900 else if (name[1] == 'g' && name[2] == 'd')
901 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
902 else if (name[1] == 'l')
903 p = ".line", n = 5;
904 else if (name[1] == 's')
905 p = ".stab", n = 5;
906 else if (name[1] == 'z')
907 p = ".zdebug", n = 7;
908 else
909 p = NULL, n = 0;
910 if (p != NULL && strncmp (name, p, n) == 0)
911 flags |= SEC_DEBUGGING;
915 /* As a GNU extension, if the name begins with .gnu.linkonce, we
916 only link a single copy of the section. This is used to support
917 g++. g++ will emit each template expansion in its own section.
918 The symbols will be defined as weak, so that multiple definitions
919 are permitted. The GNU linker extension is to actually discard
920 all but one of the sections. */
921 if (CONST_STRNEQ (name, ".gnu.linkonce")
922 && elf_next_in_group (newsect) == NULL)
923 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
925 bed = get_elf_backend_data (abfd);
926 if (bed->elf_backend_section_flags)
927 if (! bed->elf_backend_section_flags (&flags, hdr))
928 return FALSE;
930 if (! bfd_set_section_flags (abfd, newsect, flags))
931 return FALSE;
933 /* We do not parse the PT_NOTE segments as we are interested even in the
934 separate debug info files which may have the segments offsets corrupted.
935 PT_NOTEs from the core files are currently not parsed using BFD. */
936 if (hdr->sh_type == SHT_NOTE)
938 bfd_byte *contents;
940 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
941 return FALSE;
943 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
944 free (contents);
947 if ((flags & SEC_ALLOC) != 0)
949 Elf_Internal_Phdr *phdr;
950 unsigned int i, nload;
952 /* Some ELF linkers produce binaries with all the program header
953 p_paddr fields zero. If we have such a binary with more than
954 one PT_LOAD header, then leave the section lma equal to vma
955 so that we don't create sections with overlapping lma. */
956 phdr = elf_tdata (abfd)->phdr;
957 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
958 if (phdr->p_paddr != 0)
959 break;
960 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
961 ++nload;
962 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
963 return TRUE;
965 phdr = elf_tdata (abfd)->phdr;
966 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
968 if (((phdr->p_type == PT_LOAD
969 && (hdr->sh_flags & SHF_TLS) == 0)
970 || phdr->p_type == PT_TLS)
971 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
973 if ((flags & SEC_LOAD) == 0)
974 newsect->lma = (phdr->p_paddr
975 + hdr->sh_addr - phdr->p_vaddr);
976 else
977 /* We used to use the same adjustment for SEC_LOAD
978 sections, but that doesn't work if the segment
979 is packed with code from multiple VMAs.
980 Instead we calculate the section LMA based on
981 the segment LMA. It is assumed that the
982 segment will contain sections with contiguous
983 LMAs, even if the VMAs are not. */
984 newsect->lma = (phdr->p_paddr
985 + hdr->sh_offset - phdr->p_offset);
987 /* With contiguous segments, we can't tell from file
988 offsets whether a section with zero size should
989 be placed at the end of one segment or the
990 beginning of the next. Decide based on vaddr. */
991 if (hdr->sh_addr >= phdr->p_vaddr
992 && (hdr->sh_addr + hdr->sh_size
993 <= phdr->p_vaddr + phdr->p_memsz))
994 break;
999 /* Compress/decompress DWARF debug sections with names: .debug_* and
1000 .zdebug_*, after the section flags is set. */
1001 if ((flags & SEC_DEBUGGING)
1002 && ((name[1] == 'd' && name[6] == '_')
1003 || (name[1] == 'z' && name[7] == '_')))
1005 enum { nothing, compress, decompress } action = nothing;
1006 char *new_name;
1008 if (bfd_is_section_compressed (abfd, newsect))
1010 /* Compressed section. Check if we should decompress. */
1011 if ((abfd->flags & BFD_DECOMPRESS))
1012 action = decompress;
1014 else
1016 /* Normal section. Check if we should compress. */
1017 if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
1018 action = compress;
1021 new_name = NULL;
1022 switch (action)
1024 case nothing:
1025 break;
1026 case compress:
1027 if (!bfd_init_section_compress_status (abfd, newsect))
1029 (*_bfd_error_handler)
1030 (_("%B: unable to initialize compress status for section %s"),
1031 abfd, name);
1032 return FALSE;
1034 if (name[1] != 'z')
1036 unsigned int len = strlen (name);
1038 new_name = bfd_alloc (abfd, len + 2);
1039 if (new_name == NULL)
1040 return FALSE;
1041 new_name[0] = '.';
1042 new_name[1] = 'z';
1043 memcpy (new_name + 2, name + 1, len);
1045 break;
1046 case decompress:
1047 if (!bfd_init_section_decompress_status (abfd, newsect))
1049 (*_bfd_error_handler)
1050 (_("%B: unable to initialize decompress status for section %s"),
1051 abfd, name);
1052 return FALSE;
1054 if (name[1] == 'z')
1056 unsigned int len = strlen (name);
1058 new_name = bfd_alloc (abfd, len);
1059 if (new_name == NULL)
1060 return FALSE;
1061 new_name[0] = '.';
1062 memcpy (new_name + 1, name + 2, len - 1);
1064 break;
1066 if (new_name != NULL)
1067 bfd_rename_section (abfd, newsect, new_name);
1070 return TRUE;
1073 const char *const bfd_elf_section_type_names[] = {
1074 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1075 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1076 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1079 /* ELF relocs are against symbols. If we are producing relocatable
1080 output, and the reloc is against an external symbol, and nothing
1081 has given us any additional addend, the resulting reloc will also
1082 be against the same symbol. In such a case, we don't want to
1083 change anything about the way the reloc is handled, since it will
1084 all be done at final link time. Rather than put special case code
1085 into bfd_perform_relocation, all the reloc types use this howto
1086 function. It just short circuits the reloc if producing
1087 relocatable output against an external symbol. */
1089 bfd_reloc_status_type
1090 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1091 arelent *reloc_entry,
1092 asymbol *symbol,
1093 void *data ATTRIBUTE_UNUSED,
1094 asection *input_section,
1095 bfd *output_bfd,
1096 char **error_message ATTRIBUTE_UNUSED)
1098 if (output_bfd != NULL
1099 && (symbol->flags & BSF_SECTION_SYM) == 0
1100 && (! reloc_entry->howto->partial_inplace
1101 || reloc_entry->addend == 0))
1103 reloc_entry->address += input_section->output_offset;
1104 return bfd_reloc_ok;
1107 return bfd_reloc_continue;
1110 /* Copy the program header and other data from one object module to
1111 another. */
1113 bfd_boolean
1114 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1116 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1117 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1118 return TRUE;
1120 if (!elf_flags_init (obfd))
1122 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1123 elf_flags_init (obfd) = TRUE;
1126 elf_gp (obfd) = elf_gp (ibfd);
1128 /* Also copy the EI_OSABI field. */
1129 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1130 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1132 /* Copy object attributes. */
1133 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1134 return TRUE;
1137 static const char *
1138 get_segment_type (unsigned int p_type)
1140 const char *pt;
1141 switch (p_type)
1143 case PT_NULL: pt = "NULL"; break;
1144 case PT_LOAD: pt = "LOAD"; break;
1145 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1146 case PT_INTERP: pt = "INTERP"; break;
1147 case PT_NOTE: pt = "NOTE"; break;
1148 case PT_SHLIB: pt = "SHLIB"; break;
1149 case PT_PHDR: pt = "PHDR"; break;
1150 case PT_TLS: pt = "TLS"; break;
1151 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1152 case PT_GNU_STACK: pt = "STACK"; break;
1153 case PT_GNU_RELRO: pt = "RELRO"; break;
1154 default: pt = NULL; break;
1156 return pt;
1159 /* Print out the program headers. */
1161 bfd_boolean
1162 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1164 FILE *f = (FILE *) farg;
1165 Elf_Internal_Phdr *p;
1166 asection *s;
1167 bfd_byte *dynbuf = NULL;
1169 p = elf_tdata (abfd)->phdr;
1170 if (p != NULL)
1172 unsigned int i, c;
1174 fprintf (f, _("\nProgram Header:\n"));
1175 c = elf_elfheader (abfd)->e_phnum;
1176 for (i = 0; i < c; i++, p++)
1178 const char *pt = get_segment_type (p->p_type);
1179 char buf[20];
1181 if (pt == NULL)
1183 sprintf (buf, "0x%lx", p->p_type);
1184 pt = buf;
1186 fprintf (f, "%8s off 0x", pt);
1187 bfd_fprintf_vma (abfd, f, p->p_offset);
1188 fprintf (f, " vaddr 0x");
1189 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1190 fprintf (f, " paddr 0x");
1191 bfd_fprintf_vma (abfd, f, p->p_paddr);
1192 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1193 fprintf (f, " filesz 0x");
1194 bfd_fprintf_vma (abfd, f, p->p_filesz);
1195 fprintf (f, " memsz 0x");
1196 bfd_fprintf_vma (abfd, f, p->p_memsz);
1197 fprintf (f, " flags %c%c%c",
1198 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1199 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1200 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1201 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1202 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1203 fprintf (f, "\n");
1207 s = bfd_get_section_by_name (abfd, ".dynamic");
1208 if (s != NULL)
1210 unsigned int elfsec;
1211 unsigned long shlink;
1212 bfd_byte *extdyn, *extdynend;
1213 size_t extdynsize;
1214 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1216 fprintf (f, _("\nDynamic Section:\n"));
1218 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1219 goto error_return;
1221 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1222 if (elfsec == SHN_BAD)
1223 goto error_return;
1224 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1226 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1227 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1229 extdyn = dynbuf;
1230 extdynend = extdyn + s->size;
1231 for (; extdyn < extdynend; extdyn += extdynsize)
1233 Elf_Internal_Dyn dyn;
1234 const char *name = "";
1235 char ab[20];
1236 bfd_boolean stringp;
1237 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1239 (*swap_dyn_in) (abfd, extdyn, &dyn);
1241 if (dyn.d_tag == DT_NULL)
1242 break;
1244 stringp = FALSE;
1245 switch (dyn.d_tag)
1247 default:
1248 if (bed->elf_backend_get_target_dtag)
1249 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1251 if (!strcmp (name, ""))
1253 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1254 name = ab;
1256 break;
1258 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1259 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1260 case DT_PLTGOT: name = "PLTGOT"; break;
1261 case DT_HASH: name = "HASH"; break;
1262 case DT_STRTAB: name = "STRTAB"; break;
1263 case DT_SYMTAB: name = "SYMTAB"; break;
1264 case DT_RELA: name = "RELA"; break;
1265 case DT_RELASZ: name = "RELASZ"; break;
1266 case DT_RELAENT: name = "RELAENT"; break;
1267 case DT_STRSZ: name = "STRSZ"; break;
1268 case DT_SYMENT: name = "SYMENT"; break;
1269 case DT_INIT: name = "INIT"; break;
1270 case DT_FINI: name = "FINI"; break;
1271 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1272 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1273 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1274 case DT_REL: name = "REL"; break;
1275 case DT_RELSZ: name = "RELSZ"; break;
1276 case DT_RELENT: name = "RELENT"; break;
1277 case DT_PLTREL: name = "PLTREL"; break;
1278 case DT_DEBUG: name = "DEBUG"; break;
1279 case DT_TEXTREL: name = "TEXTREL"; break;
1280 case DT_JMPREL: name = "JMPREL"; break;
1281 case DT_BIND_NOW: name = "BIND_NOW"; break;
1282 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1283 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1284 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1285 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1286 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1287 case DT_FLAGS: name = "FLAGS"; break;
1288 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1289 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1290 case DT_CHECKSUM: name = "CHECKSUM"; break;
1291 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1292 case DT_MOVEENT: name = "MOVEENT"; break;
1293 case DT_MOVESZ: name = "MOVESZ"; break;
1294 case DT_FEATURE: name = "FEATURE"; break;
1295 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1296 case DT_SYMINSZ: name = "SYMINSZ"; break;
1297 case DT_SYMINENT: name = "SYMINENT"; break;
1298 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1299 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1300 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1301 case DT_PLTPAD: name = "PLTPAD"; break;
1302 case DT_MOVETAB: name = "MOVETAB"; break;
1303 case DT_SYMINFO: name = "SYMINFO"; break;
1304 case DT_RELACOUNT: name = "RELACOUNT"; break;
1305 case DT_RELCOUNT: name = "RELCOUNT"; break;
1306 case DT_FLAGS_1: name = "FLAGS_1"; break;
1307 case DT_VERSYM: name = "VERSYM"; break;
1308 case DT_VERDEF: name = "VERDEF"; break;
1309 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1310 case DT_VERNEED: name = "VERNEED"; break;
1311 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1312 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1313 case DT_USED: name = "USED"; break;
1314 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1315 case DT_GNU_HASH: name = "GNU_HASH"; break;
1318 fprintf (f, " %-20s ", name);
1319 if (! stringp)
1321 fprintf (f, "0x");
1322 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1324 else
1326 const char *string;
1327 unsigned int tagv = dyn.d_un.d_val;
1329 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1330 if (string == NULL)
1331 goto error_return;
1332 fprintf (f, "%s", string);
1334 fprintf (f, "\n");
1337 free (dynbuf);
1338 dynbuf = NULL;
1341 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1342 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1344 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1345 return FALSE;
1348 if (elf_dynverdef (abfd) != 0)
1350 Elf_Internal_Verdef *t;
1352 fprintf (f, _("\nVersion definitions:\n"));
1353 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1355 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1356 t->vd_flags, t->vd_hash,
1357 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1358 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1360 Elf_Internal_Verdaux *a;
1362 fprintf (f, "\t");
1363 for (a = t->vd_auxptr->vda_nextptr;
1364 a != NULL;
1365 a = a->vda_nextptr)
1366 fprintf (f, "%s ",
1367 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1368 fprintf (f, "\n");
1373 if (elf_dynverref (abfd) != 0)
1375 Elf_Internal_Verneed *t;
1377 fprintf (f, _("\nVersion References:\n"));
1378 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1380 Elf_Internal_Vernaux *a;
1382 fprintf (f, _(" required from %s:\n"),
1383 t->vn_filename ? t->vn_filename : "<corrupt>");
1384 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1385 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1386 a->vna_flags, a->vna_other,
1387 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1391 return TRUE;
1393 error_return:
1394 if (dynbuf != NULL)
1395 free (dynbuf);
1396 return FALSE;
1399 /* Display ELF-specific fields of a symbol. */
1401 void
1402 bfd_elf_print_symbol (bfd *abfd,
1403 void *filep,
1404 asymbol *symbol,
1405 bfd_print_symbol_type how)
1407 FILE *file = (FILE *) filep;
1408 switch (how)
1410 case bfd_print_symbol_name:
1411 fprintf (file, "%s", symbol->name);
1412 break;
1413 case bfd_print_symbol_more:
1414 fprintf (file, "elf ");
1415 bfd_fprintf_vma (abfd, file, symbol->value);
1416 fprintf (file, " %lx", (unsigned long) symbol->flags);
1417 break;
1418 case bfd_print_symbol_all:
1420 const char *section_name;
1421 const char *name = NULL;
1422 const struct elf_backend_data *bed;
1423 unsigned char st_other;
1424 bfd_vma val;
1426 section_name = symbol->section ? symbol->section->name : "(*none*)";
1428 bed = get_elf_backend_data (abfd);
1429 if (bed->elf_backend_print_symbol_all)
1430 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1432 if (name == NULL)
1434 name = symbol->name;
1435 bfd_print_symbol_vandf (abfd, file, symbol);
1438 fprintf (file, " %s\t", section_name);
1439 /* Print the "other" value for a symbol. For common symbols,
1440 we've already printed the size; now print the alignment.
1441 For other symbols, we have no specified alignment, and
1442 we've printed the address; now print the size. */
1443 if (symbol->section && bfd_is_com_section (symbol->section))
1444 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1445 else
1446 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1447 bfd_fprintf_vma (abfd, file, val);
1449 /* If we have version information, print it. */
1450 if (elf_dynversym (abfd) != 0
1451 && (elf_dynverdef (abfd) != 0
1452 || elf_dynverref (abfd) != 0))
1454 unsigned int vernum;
1455 const char *version_string;
1457 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1459 if (vernum == 0)
1460 version_string = "";
1461 else if (vernum == 1)
1462 version_string = "Base";
1463 else if (vernum <= elf_tdata (abfd)->cverdefs)
1464 version_string =
1465 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1466 else
1468 Elf_Internal_Verneed *t;
1470 version_string = "";
1471 for (t = elf_tdata (abfd)->verref;
1472 t != NULL;
1473 t = t->vn_nextref)
1475 Elf_Internal_Vernaux *a;
1477 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1479 if (a->vna_other == vernum)
1481 version_string = a->vna_nodename;
1482 break;
1488 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1489 fprintf (file, " %-11s", version_string);
1490 else
1492 int i;
1494 fprintf (file, " (%s)", version_string);
1495 for (i = 10 - strlen (version_string); i > 0; --i)
1496 putc (' ', file);
1500 /* If the st_other field is not zero, print it. */
1501 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1503 switch (st_other)
1505 case 0: break;
1506 case STV_INTERNAL: fprintf (file, " .internal"); break;
1507 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1508 case STV_PROTECTED: fprintf (file, " .protected"); break;
1509 default:
1510 /* Some other non-defined flags are also present, so print
1511 everything hex. */
1512 fprintf (file, " 0x%02x", (unsigned int) st_other);
1515 fprintf (file, " %s", name);
1517 break;
1521 /* Allocate an ELF string table--force the first byte to be zero. */
1523 struct bfd_strtab_hash *
1524 _bfd_elf_stringtab_init (void)
1526 struct bfd_strtab_hash *ret;
1528 ret = _bfd_stringtab_init ();
1529 if (ret != NULL)
1531 bfd_size_type loc;
1533 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1534 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1535 if (loc == (bfd_size_type) -1)
1537 _bfd_stringtab_free (ret);
1538 ret = NULL;
1541 return ret;
1544 /* ELF .o/exec file reading */
1546 /* Create a new bfd section from an ELF section header. */
1548 bfd_boolean
1549 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1551 Elf_Internal_Shdr *hdr;
1552 Elf_Internal_Ehdr *ehdr;
1553 const struct elf_backend_data *bed;
1554 const char *name;
1556 if (shindex >= elf_numsections (abfd))
1557 return FALSE;
1559 hdr = elf_elfsections (abfd)[shindex];
1560 ehdr = elf_elfheader (abfd);
1561 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1562 hdr->sh_name);
1563 if (name == NULL)
1564 return FALSE;
1566 bed = get_elf_backend_data (abfd);
1567 switch (hdr->sh_type)
1569 case SHT_NULL:
1570 /* Inactive section. Throw it away. */
1571 return TRUE;
1573 case SHT_PROGBITS: /* Normal section with contents. */
1574 case SHT_NOBITS: /* .bss section. */
1575 case SHT_HASH: /* .hash section. */
1576 case SHT_NOTE: /* .note section. */
1577 case SHT_INIT_ARRAY: /* .init_array section. */
1578 case SHT_FINI_ARRAY: /* .fini_array section. */
1579 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1580 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1581 case SHT_GNU_HASH: /* .gnu.hash section. */
1582 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1584 case SHT_DYNAMIC: /* Dynamic linking information. */
1585 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1586 return FALSE;
1587 if (hdr->sh_link > elf_numsections (abfd))
1589 /* PR 10478: Accept Solaris binaries with a sh_link
1590 field set to SHN_BEFORE or SHN_AFTER. */
1591 switch (bfd_get_arch (abfd))
1593 case bfd_arch_i386:
1594 case bfd_arch_sparc:
1595 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1596 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1597 break;
1598 /* Otherwise fall through. */
1599 default:
1600 return FALSE;
1603 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1604 return FALSE;
1605 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1607 Elf_Internal_Shdr *dynsymhdr;
1609 /* The shared libraries distributed with hpux11 have a bogus
1610 sh_link field for the ".dynamic" section. Find the
1611 string table for the ".dynsym" section instead. */
1612 if (elf_dynsymtab (abfd) != 0)
1614 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1615 hdr->sh_link = dynsymhdr->sh_link;
1617 else
1619 unsigned int i, num_sec;
1621 num_sec = elf_numsections (abfd);
1622 for (i = 1; i < num_sec; i++)
1624 dynsymhdr = elf_elfsections (abfd)[i];
1625 if (dynsymhdr->sh_type == SHT_DYNSYM)
1627 hdr->sh_link = dynsymhdr->sh_link;
1628 break;
1633 break;
1635 case SHT_SYMTAB: /* A symbol table */
1636 if (elf_onesymtab (abfd) == shindex)
1637 return TRUE;
1639 if (hdr->sh_entsize != bed->s->sizeof_sym)
1640 return FALSE;
1641 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1643 if (hdr->sh_size != 0)
1644 return FALSE;
1645 /* Some assemblers erroneously set sh_info to one with a
1646 zero sh_size. ld sees this as a global symbol count
1647 of (unsigned) -1. Fix it here. */
1648 hdr->sh_info = 0;
1649 return TRUE;
1651 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1652 elf_onesymtab (abfd) = shindex;
1653 elf_tdata (abfd)->symtab_hdr = *hdr;
1654 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1655 abfd->flags |= HAS_SYMS;
1657 /* Sometimes a shared object will map in the symbol table. If
1658 SHF_ALLOC is set, and this is a shared object, then we also
1659 treat this section as a BFD section. We can not base the
1660 decision purely on SHF_ALLOC, because that flag is sometimes
1661 set in a relocatable object file, which would confuse the
1662 linker. */
1663 if ((hdr->sh_flags & SHF_ALLOC) != 0
1664 && (abfd->flags & DYNAMIC) != 0
1665 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1666 shindex))
1667 return FALSE;
1669 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1670 can't read symbols without that section loaded as well. It
1671 is most likely specified by the next section header. */
1672 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1674 unsigned int i, num_sec;
1676 num_sec = elf_numsections (abfd);
1677 for (i = shindex + 1; i < num_sec; i++)
1679 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1680 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1681 && hdr2->sh_link == shindex)
1682 break;
1684 if (i == num_sec)
1685 for (i = 1; i < shindex; i++)
1687 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1688 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1689 && hdr2->sh_link == shindex)
1690 break;
1692 if (i != shindex)
1693 return bfd_section_from_shdr (abfd, i);
1695 return TRUE;
1697 case SHT_DYNSYM: /* A dynamic symbol table */
1698 if (elf_dynsymtab (abfd) == shindex)
1699 return TRUE;
1701 if (hdr->sh_entsize != bed->s->sizeof_sym)
1702 return FALSE;
1703 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1705 if (hdr->sh_size != 0)
1706 return FALSE;
1707 /* Some linkers erroneously set sh_info to one with a
1708 zero sh_size. ld sees this as a global symbol count
1709 of (unsigned) -1. Fix it here. */
1710 hdr->sh_info = 0;
1711 return TRUE;
1713 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1714 elf_dynsymtab (abfd) = shindex;
1715 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1716 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1717 abfd->flags |= HAS_SYMS;
1719 /* Besides being a symbol table, we also treat this as a regular
1720 section, so that objcopy can handle it. */
1721 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1723 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1724 if (elf_symtab_shndx (abfd) == shindex)
1725 return TRUE;
1727 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1728 elf_symtab_shndx (abfd) = shindex;
1729 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1730 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1731 return TRUE;
1733 case SHT_STRTAB: /* A string table */
1734 if (hdr->bfd_section != NULL)
1735 return TRUE;
1736 if (ehdr->e_shstrndx == shindex)
1738 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1739 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1740 return TRUE;
1742 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1744 symtab_strtab:
1745 elf_tdata (abfd)->strtab_hdr = *hdr;
1746 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1747 return TRUE;
1749 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1751 dynsymtab_strtab:
1752 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1753 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1754 elf_elfsections (abfd)[shindex] = hdr;
1755 /* We also treat this as a regular section, so that objcopy
1756 can handle it. */
1757 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1758 shindex);
1761 /* If the string table isn't one of the above, then treat it as a
1762 regular section. We need to scan all the headers to be sure,
1763 just in case this strtab section appeared before the above. */
1764 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1766 unsigned int i, num_sec;
1768 num_sec = elf_numsections (abfd);
1769 for (i = 1; i < num_sec; i++)
1771 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1772 if (hdr2->sh_link == shindex)
1774 /* Prevent endless recursion on broken objects. */
1775 if (i == shindex)
1776 return FALSE;
1777 if (! bfd_section_from_shdr (abfd, i))
1778 return FALSE;
1779 if (elf_onesymtab (abfd) == i)
1780 goto symtab_strtab;
1781 if (elf_dynsymtab (abfd) == i)
1782 goto dynsymtab_strtab;
1786 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1788 case SHT_REL:
1789 case SHT_RELA:
1790 /* *These* do a lot of work -- but build no sections! */
1792 asection *target_sect;
1793 Elf_Internal_Shdr *hdr2, **p_hdr;
1794 unsigned int num_sec = elf_numsections (abfd);
1795 struct bfd_elf_section_data *esdt;
1796 bfd_size_type amt;
1798 if (hdr->sh_entsize
1799 != (bfd_size_type) (hdr->sh_type == SHT_REL
1800 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1801 return FALSE;
1803 /* Check for a bogus link to avoid crashing. */
1804 if (hdr->sh_link >= num_sec)
1806 ((*_bfd_error_handler)
1807 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1808 abfd, hdr->sh_link, name, shindex));
1809 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1810 shindex);
1813 /* For some incomprehensible reason Oracle distributes
1814 libraries for Solaris in which some of the objects have
1815 bogus sh_link fields. It would be nice if we could just
1816 reject them, but, unfortunately, some people need to use
1817 them. We scan through the section headers; if we find only
1818 one suitable symbol table, we clobber the sh_link to point
1819 to it. I hope this doesn't break anything.
1821 Don't do it on executable nor shared library. */
1822 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1823 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1824 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1826 unsigned int scan;
1827 int found;
1829 found = 0;
1830 for (scan = 1; scan < num_sec; scan++)
1832 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1833 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1835 if (found != 0)
1837 found = 0;
1838 break;
1840 found = scan;
1843 if (found != 0)
1844 hdr->sh_link = found;
1847 /* Get the symbol table. */
1848 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1849 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1850 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1851 return FALSE;
1853 /* If this reloc section does not use the main symbol table we
1854 don't treat it as a reloc section. BFD can't adequately
1855 represent such a section, so at least for now, we don't
1856 try. We just present it as a normal section. We also
1857 can't use it as a reloc section if it points to the null
1858 section, an invalid section, another reloc section, or its
1859 sh_link points to the null section. */
1860 if (hdr->sh_link != elf_onesymtab (abfd)
1861 || hdr->sh_link == SHN_UNDEF
1862 || hdr->sh_info == SHN_UNDEF
1863 || hdr->sh_info >= num_sec
1864 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1865 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1866 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1867 shindex);
1869 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1870 return FALSE;
1871 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1872 if (target_sect == NULL)
1873 return FALSE;
1875 esdt = elf_section_data (target_sect);
1876 if (hdr->sh_type == SHT_RELA)
1877 p_hdr = &esdt->rela.hdr;
1878 else
1879 p_hdr = &esdt->rel.hdr;
1881 BFD_ASSERT (*p_hdr == NULL);
1882 amt = sizeof (*hdr2);
1883 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1884 if (hdr2 == NULL)
1885 return FALSE;
1886 *hdr2 = *hdr;
1887 *p_hdr = hdr2;
1888 elf_elfsections (abfd)[shindex] = hdr2;
1889 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1890 target_sect->flags |= SEC_RELOC;
1891 target_sect->relocation = NULL;
1892 target_sect->rel_filepos = hdr->sh_offset;
1893 /* In the section to which the relocations apply, mark whether
1894 its relocations are of the REL or RELA variety. */
1895 if (hdr->sh_size != 0)
1897 if (hdr->sh_type == SHT_RELA)
1898 target_sect->use_rela_p = 1;
1900 abfd->flags |= HAS_RELOC;
1901 return TRUE;
1904 case SHT_GNU_verdef:
1905 elf_dynverdef (abfd) = shindex;
1906 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1907 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1909 case SHT_GNU_versym:
1910 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1911 return FALSE;
1912 elf_dynversym (abfd) = shindex;
1913 elf_tdata (abfd)->dynversym_hdr = *hdr;
1914 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1916 case SHT_GNU_verneed:
1917 elf_dynverref (abfd) = shindex;
1918 elf_tdata (abfd)->dynverref_hdr = *hdr;
1919 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1921 case SHT_SHLIB:
1922 return TRUE;
1924 case SHT_GROUP:
1925 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
1926 return FALSE;
1927 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1928 return FALSE;
1929 if (hdr->contents != NULL)
1931 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1932 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1933 asection *s;
1935 if (idx->flags & GRP_COMDAT)
1936 hdr->bfd_section->flags
1937 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1939 /* We try to keep the same section order as it comes in. */
1940 idx += n_elt;
1941 while (--n_elt != 0)
1943 --idx;
1945 if (idx->shdr != NULL
1946 && (s = idx->shdr->bfd_section) != NULL
1947 && elf_next_in_group (s) != NULL)
1949 elf_next_in_group (hdr->bfd_section) = s;
1950 break;
1954 break;
1956 default:
1957 /* Possibly an attributes section. */
1958 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1959 || hdr->sh_type == bed->obj_attrs_section_type)
1961 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1962 return FALSE;
1963 _bfd_elf_parse_attributes (abfd, hdr);
1964 return TRUE;
1967 /* Check for any processor-specific section types. */
1968 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1969 return TRUE;
1971 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1973 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1974 /* FIXME: How to properly handle allocated section reserved
1975 for applications? */
1976 (*_bfd_error_handler)
1977 (_("%B: don't know how to handle allocated, application "
1978 "specific section `%s' [0x%8x]"),
1979 abfd, name, hdr->sh_type);
1980 else
1981 /* Allow sections reserved for applications. */
1982 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1983 shindex);
1985 else if (hdr->sh_type >= SHT_LOPROC
1986 && hdr->sh_type <= SHT_HIPROC)
1987 /* FIXME: We should handle this section. */
1988 (*_bfd_error_handler)
1989 (_("%B: don't know how to handle processor specific section "
1990 "`%s' [0x%8x]"),
1991 abfd, name, hdr->sh_type);
1992 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1994 /* Unrecognised OS-specific sections. */
1995 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1996 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1997 required to correctly process the section and the file should
1998 be rejected with an error message. */
1999 (*_bfd_error_handler)
2000 (_("%B: don't know how to handle OS specific section "
2001 "`%s' [0x%8x]"),
2002 abfd, name, hdr->sh_type);
2003 else
2004 /* Otherwise it should be processed. */
2005 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2007 else
2008 /* FIXME: We should handle this section. */
2009 (*_bfd_error_handler)
2010 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2011 abfd, name, hdr->sh_type);
2013 return FALSE;
2016 return TRUE;
2019 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2021 Elf_Internal_Sym *
2022 bfd_sym_from_r_symndx (struct sym_cache *cache,
2023 bfd *abfd,
2024 unsigned long r_symndx)
2026 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2028 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2030 Elf_Internal_Shdr *symtab_hdr;
2031 unsigned char esym[sizeof (Elf64_External_Sym)];
2032 Elf_External_Sym_Shndx eshndx;
2034 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2035 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2036 &cache->sym[ent], esym, &eshndx) == NULL)
2037 return NULL;
2039 if (cache->abfd != abfd)
2041 memset (cache->indx, -1, sizeof (cache->indx));
2042 cache->abfd = abfd;
2044 cache->indx[ent] = r_symndx;
2047 return &cache->sym[ent];
2050 /* Given an ELF section number, retrieve the corresponding BFD
2051 section. */
2053 asection *
2054 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2056 if (sec_index >= elf_numsections (abfd))
2057 return NULL;
2058 return elf_elfsections (abfd)[sec_index]->bfd_section;
2061 static const struct bfd_elf_special_section special_sections_b[] =
2063 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2064 { NULL, 0, 0, 0, 0 }
2067 static const struct bfd_elf_special_section special_sections_c[] =
2069 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2070 { NULL, 0, 0, 0, 0 }
2073 static const struct bfd_elf_special_section special_sections_d[] =
2075 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2076 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2077 /* There are more DWARF sections than these, but they needn't be added here
2078 unless you have to cope with broken compilers that don't emit section
2079 attributes or you want to help the user writing assembler. */
2080 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2081 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2082 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2083 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2084 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2085 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2086 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2087 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2088 { NULL, 0, 0, 0, 0 }
2091 static const struct bfd_elf_special_section special_sections_f[] =
2093 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2094 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2095 { NULL, 0, 0, 0, 0 }
2098 static const struct bfd_elf_special_section special_sections_g[] =
2100 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2101 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2102 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2103 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2104 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2105 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2106 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2107 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2108 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2109 { NULL, 0, 0, 0, 0 }
2112 static const struct bfd_elf_special_section special_sections_h[] =
2114 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2115 { NULL, 0, 0, 0, 0 }
2118 static const struct bfd_elf_special_section special_sections_i[] =
2120 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2121 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2122 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2123 { NULL, 0, 0, 0, 0 }
2126 static const struct bfd_elf_special_section special_sections_l[] =
2128 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2129 { NULL, 0, 0, 0, 0 }
2132 static const struct bfd_elf_special_section special_sections_n[] =
2134 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2135 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2136 { NULL, 0, 0, 0, 0 }
2139 static const struct bfd_elf_special_section special_sections_p[] =
2141 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2142 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2143 { NULL, 0, 0, 0, 0 }
2146 static const struct bfd_elf_special_section special_sections_r[] =
2148 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2149 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2150 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2151 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2152 { NULL, 0, 0, 0, 0 }
2155 static const struct bfd_elf_special_section special_sections_s[] =
2157 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2158 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2159 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2160 /* See struct bfd_elf_special_section declaration for the semantics of
2161 this special case where .prefix_length != strlen (.prefix). */
2162 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2163 { NULL, 0, 0, 0, 0 }
2166 static const struct bfd_elf_special_section special_sections_t[] =
2168 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2169 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2170 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2171 { NULL, 0, 0, 0, 0 }
2174 static const struct bfd_elf_special_section special_sections_z[] =
2176 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2177 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2178 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2179 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2180 { NULL, 0, 0, 0, 0 }
2183 static const struct bfd_elf_special_section * const special_sections[] =
2185 special_sections_b, /* 'b' */
2186 special_sections_c, /* 'c' */
2187 special_sections_d, /* 'd' */
2188 NULL, /* 'e' */
2189 special_sections_f, /* 'f' */
2190 special_sections_g, /* 'g' */
2191 special_sections_h, /* 'h' */
2192 special_sections_i, /* 'i' */
2193 NULL, /* 'j' */
2194 NULL, /* 'k' */
2195 special_sections_l, /* 'l' */
2196 NULL, /* 'm' */
2197 special_sections_n, /* 'n' */
2198 NULL, /* 'o' */
2199 special_sections_p, /* 'p' */
2200 NULL, /* 'q' */
2201 special_sections_r, /* 'r' */
2202 special_sections_s, /* 's' */
2203 special_sections_t, /* 't' */
2204 NULL, /* 'u' */
2205 NULL, /* 'v' */
2206 NULL, /* 'w' */
2207 NULL, /* 'x' */
2208 NULL, /* 'y' */
2209 special_sections_z /* 'z' */
2212 const struct bfd_elf_special_section *
2213 _bfd_elf_get_special_section (const char *name,
2214 const struct bfd_elf_special_section *spec,
2215 unsigned int rela)
2217 int i;
2218 int len;
2220 len = strlen (name);
2222 for (i = 0; spec[i].prefix != NULL; i++)
2224 int suffix_len;
2225 int prefix_len = spec[i].prefix_length;
2227 if (len < prefix_len)
2228 continue;
2229 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2230 continue;
2232 suffix_len = spec[i].suffix_length;
2233 if (suffix_len <= 0)
2235 if (name[prefix_len] != 0)
2237 if (suffix_len == 0)
2238 continue;
2239 if (name[prefix_len] != '.'
2240 && (suffix_len == -2
2241 || (rela && spec[i].type == SHT_REL)))
2242 continue;
2245 else
2247 if (len < prefix_len + suffix_len)
2248 continue;
2249 if (memcmp (name + len - suffix_len,
2250 spec[i].prefix + prefix_len,
2251 suffix_len) != 0)
2252 continue;
2254 return &spec[i];
2257 return NULL;
2260 const struct bfd_elf_special_section *
2261 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2263 int i;
2264 const struct bfd_elf_special_section *spec;
2265 const struct elf_backend_data *bed;
2267 /* See if this is one of the special sections. */
2268 if (sec->name == NULL)
2269 return NULL;
2271 bed = get_elf_backend_data (abfd);
2272 spec = bed->special_sections;
2273 if (spec)
2275 spec = _bfd_elf_get_special_section (sec->name,
2276 bed->special_sections,
2277 sec->use_rela_p);
2278 if (spec != NULL)
2279 return spec;
2282 if (sec->name[0] != '.')
2283 return NULL;
2285 i = sec->name[1] - 'b';
2286 if (i < 0 || i > 'z' - 'b')
2287 return NULL;
2289 spec = special_sections[i];
2291 if (spec == NULL)
2292 return NULL;
2294 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2297 bfd_boolean
2298 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2300 struct bfd_elf_section_data *sdata;
2301 const struct elf_backend_data *bed;
2302 const struct bfd_elf_special_section *ssect;
2304 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2305 if (sdata == NULL)
2307 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2308 sizeof (*sdata));
2309 if (sdata == NULL)
2310 return FALSE;
2311 sec->used_by_bfd = sdata;
2314 /* Indicate whether or not this section should use RELA relocations. */
2315 bed = get_elf_backend_data (abfd);
2316 sec->use_rela_p = bed->default_use_rela_p;
2318 /* When we read a file, we don't need to set ELF section type and
2319 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2320 anyway. We will set ELF section type and flags for all linker
2321 created sections. If user specifies BFD section flags, we will
2322 set ELF section type and flags based on BFD section flags in
2323 elf_fake_sections. Special handling for .init_array/.fini_array
2324 output sections since they may contain .ctors/.dtors input
2325 sections. We don't want _bfd_elf_init_private_section_data to
2326 copy ELF section type from .ctors/.dtors input sections. */
2327 if (abfd->direction != read_direction
2328 || (sec->flags & SEC_LINKER_CREATED) != 0)
2330 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2331 if (ssect != NULL
2332 && (!sec->flags
2333 || (sec->flags & SEC_LINKER_CREATED) != 0
2334 || ssect->type == SHT_INIT_ARRAY
2335 || ssect->type == SHT_FINI_ARRAY))
2337 elf_section_type (sec) = ssect->type;
2338 elf_section_flags (sec) = ssect->attr;
2342 return _bfd_generic_new_section_hook (abfd, sec);
2345 /* Create a new bfd section from an ELF program header.
2347 Since program segments have no names, we generate a synthetic name
2348 of the form segment<NUM>, where NUM is generally the index in the
2349 program header table. For segments that are split (see below) we
2350 generate the names segment<NUM>a and segment<NUM>b.
2352 Note that some program segments may have a file size that is different than
2353 (less than) the memory size. All this means is that at execution the
2354 system must allocate the amount of memory specified by the memory size,
2355 but only initialize it with the first "file size" bytes read from the
2356 file. This would occur for example, with program segments consisting
2357 of combined data+bss.
2359 To handle the above situation, this routine generates TWO bfd sections
2360 for the single program segment. The first has the length specified by
2361 the file size of the segment, and the second has the length specified
2362 by the difference between the two sizes. In effect, the segment is split
2363 into its initialized and uninitialized parts.
2367 bfd_boolean
2368 _bfd_elf_make_section_from_phdr (bfd *abfd,
2369 Elf_Internal_Phdr *hdr,
2370 int hdr_index,
2371 const char *type_name)
2373 asection *newsect;
2374 char *name;
2375 char namebuf[64];
2376 size_t len;
2377 int split;
2379 split = ((hdr->p_memsz > 0)
2380 && (hdr->p_filesz > 0)
2381 && (hdr->p_memsz > hdr->p_filesz));
2383 if (hdr->p_filesz > 0)
2385 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2386 len = strlen (namebuf) + 1;
2387 name = (char *) bfd_alloc (abfd, len);
2388 if (!name)
2389 return FALSE;
2390 memcpy (name, namebuf, len);
2391 newsect = bfd_make_section (abfd, name);
2392 if (newsect == NULL)
2393 return FALSE;
2394 newsect->vma = hdr->p_vaddr;
2395 newsect->lma = hdr->p_paddr;
2396 newsect->size = hdr->p_filesz;
2397 newsect->filepos = hdr->p_offset;
2398 newsect->flags |= SEC_HAS_CONTENTS;
2399 newsect->alignment_power = bfd_log2 (hdr->p_align);
2400 if (hdr->p_type == PT_LOAD)
2402 newsect->flags |= SEC_ALLOC;
2403 newsect->flags |= SEC_LOAD;
2404 if (hdr->p_flags & PF_X)
2406 /* FIXME: all we known is that it has execute PERMISSION,
2407 may be data. */
2408 newsect->flags |= SEC_CODE;
2411 if (!(hdr->p_flags & PF_W))
2413 newsect->flags |= SEC_READONLY;
2417 if (hdr->p_memsz > hdr->p_filesz)
2419 bfd_vma align;
2421 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2422 len = strlen (namebuf) + 1;
2423 name = (char *) bfd_alloc (abfd, len);
2424 if (!name)
2425 return FALSE;
2426 memcpy (name, namebuf, len);
2427 newsect = bfd_make_section (abfd, name);
2428 if (newsect == NULL)
2429 return FALSE;
2430 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2431 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2432 newsect->size = hdr->p_memsz - hdr->p_filesz;
2433 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2434 align = newsect->vma & -newsect->vma;
2435 if (align == 0 || align > hdr->p_align)
2436 align = hdr->p_align;
2437 newsect->alignment_power = bfd_log2 (align);
2438 if (hdr->p_type == PT_LOAD)
2440 /* Hack for gdb. Segments that have not been modified do
2441 not have their contents written to a core file, on the
2442 assumption that a debugger can find the contents in the
2443 executable. We flag this case by setting the fake
2444 section size to zero. Note that "real" bss sections will
2445 always have their contents dumped to the core file. */
2446 if (bfd_get_format (abfd) == bfd_core)
2447 newsect->size = 0;
2448 newsect->flags |= SEC_ALLOC;
2449 if (hdr->p_flags & PF_X)
2450 newsect->flags |= SEC_CODE;
2452 if (!(hdr->p_flags & PF_W))
2453 newsect->flags |= SEC_READONLY;
2456 return TRUE;
2459 bfd_boolean
2460 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2462 const struct elf_backend_data *bed;
2464 switch (hdr->p_type)
2466 case PT_NULL:
2467 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2469 case PT_LOAD:
2470 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2472 case PT_DYNAMIC:
2473 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2475 case PT_INTERP:
2476 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2478 case PT_NOTE:
2479 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2480 return FALSE;
2481 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2482 return FALSE;
2483 return TRUE;
2485 case PT_SHLIB:
2486 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2488 case PT_PHDR:
2489 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2491 case PT_GNU_EH_FRAME:
2492 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2493 "eh_frame_hdr");
2495 case PT_GNU_STACK:
2496 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2498 case PT_GNU_RELRO:
2499 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2501 default:
2502 /* Check for any processor-specific program segment types. */
2503 bed = get_elf_backend_data (abfd);
2504 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2508 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2509 REL or RELA. */
2511 Elf_Internal_Shdr *
2512 _bfd_elf_single_rel_hdr (asection *sec)
2514 if (elf_section_data (sec)->rel.hdr)
2516 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2517 return elf_section_data (sec)->rel.hdr;
2519 else
2520 return elf_section_data (sec)->rela.hdr;
2523 /* Allocate and initialize a section-header for a new reloc section,
2524 containing relocations against ASECT. It is stored in RELDATA. If
2525 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2526 relocations. */
2528 static bfd_boolean
2529 _bfd_elf_init_reloc_shdr (bfd *abfd,
2530 struct bfd_elf_section_reloc_data *reldata,
2531 asection *asect,
2532 bfd_boolean use_rela_p)
2534 Elf_Internal_Shdr *rel_hdr;
2535 char *name;
2536 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2537 bfd_size_type amt;
2539 amt = sizeof (Elf_Internal_Shdr);
2540 BFD_ASSERT (reldata->hdr == NULL);
2541 rel_hdr = bfd_zalloc (abfd, amt);
2542 reldata->hdr = rel_hdr;
2544 amt = sizeof ".rela" + strlen (asect->name);
2545 name = (char *) bfd_alloc (abfd, amt);
2546 if (name == NULL)
2547 return FALSE;
2548 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2549 rel_hdr->sh_name =
2550 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2551 FALSE);
2552 if (rel_hdr->sh_name == (unsigned int) -1)
2553 return FALSE;
2554 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2555 rel_hdr->sh_entsize = (use_rela_p
2556 ? bed->s->sizeof_rela
2557 : bed->s->sizeof_rel);
2558 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2559 rel_hdr->sh_flags = 0;
2560 rel_hdr->sh_addr = 0;
2561 rel_hdr->sh_size = 0;
2562 rel_hdr->sh_offset = 0;
2564 return TRUE;
2567 /* Return the default section type based on the passed in section flags. */
2570 bfd_elf_get_default_section_type (flagword flags)
2572 if ((flags & SEC_ALLOC) != 0
2573 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2574 return SHT_NOBITS;
2575 return SHT_PROGBITS;
2578 struct fake_section_arg
2580 struct bfd_link_info *link_info;
2581 bfd_boolean failed;
2584 /* Set up an ELF internal section header for a section. */
2586 static void
2587 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2589 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2590 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2591 struct bfd_elf_section_data *esd = elf_section_data (asect);
2592 Elf_Internal_Shdr *this_hdr;
2593 unsigned int sh_type;
2595 if (arg->failed)
2597 /* We already failed; just get out of the bfd_map_over_sections
2598 loop. */
2599 return;
2602 this_hdr = &esd->this_hdr;
2604 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2605 asect->name, FALSE);
2606 if (this_hdr->sh_name == (unsigned int) -1)
2608 arg->failed = TRUE;
2609 return;
2612 /* Don't clear sh_flags. Assembler may set additional bits. */
2614 if ((asect->flags & SEC_ALLOC) != 0
2615 || asect->user_set_vma)
2616 this_hdr->sh_addr = asect->vma;
2617 else
2618 this_hdr->sh_addr = 0;
2620 this_hdr->sh_offset = 0;
2621 this_hdr->sh_size = asect->size;
2622 this_hdr->sh_link = 0;
2623 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2624 /* The sh_entsize and sh_info fields may have been set already by
2625 copy_private_section_data. */
2627 this_hdr->bfd_section = asect;
2628 this_hdr->contents = NULL;
2630 /* If the section type is unspecified, we set it based on
2631 asect->flags. */
2632 if ((asect->flags & SEC_GROUP) != 0)
2633 sh_type = SHT_GROUP;
2634 else
2635 sh_type = bfd_elf_get_default_section_type (asect->flags);
2637 if (this_hdr->sh_type == SHT_NULL)
2638 this_hdr->sh_type = sh_type;
2639 else if (this_hdr->sh_type == SHT_NOBITS
2640 && sh_type == SHT_PROGBITS
2641 && (asect->flags & SEC_ALLOC) != 0)
2643 /* Warn if we are changing a NOBITS section to PROGBITS, but
2644 allow the link to proceed. This can happen when users link
2645 non-bss input sections to bss output sections, or emit data
2646 to a bss output section via a linker script. */
2647 (*_bfd_error_handler)
2648 (_("warning: section `%A' type changed to PROGBITS"), asect);
2649 this_hdr->sh_type = sh_type;
2652 switch (this_hdr->sh_type)
2654 default:
2655 break;
2657 case SHT_STRTAB:
2658 case SHT_INIT_ARRAY:
2659 case SHT_FINI_ARRAY:
2660 case SHT_PREINIT_ARRAY:
2661 case SHT_NOTE:
2662 case SHT_NOBITS:
2663 case SHT_PROGBITS:
2664 break;
2666 case SHT_HASH:
2667 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2668 break;
2670 case SHT_DYNSYM:
2671 this_hdr->sh_entsize = bed->s->sizeof_sym;
2672 break;
2674 case SHT_DYNAMIC:
2675 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2676 break;
2678 case SHT_RELA:
2679 if (get_elf_backend_data (abfd)->may_use_rela_p)
2680 this_hdr->sh_entsize = bed->s->sizeof_rela;
2681 break;
2683 case SHT_REL:
2684 if (get_elf_backend_data (abfd)->may_use_rel_p)
2685 this_hdr->sh_entsize = bed->s->sizeof_rel;
2686 break;
2688 case SHT_GNU_versym:
2689 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2690 break;
2692 case SHT_GNU_verdef:
2693 this_hdr->sh_entsize = 0;
2694 /* objcopy or strip will copy over sh_info, but may not set
2695 cverdefs. The linker will set cverdefs, but sh_info will be
2696 zero. */
2697 if (this_hdr->sh_info == 0)
2698 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2699 else
2700 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2701 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2702 break;
2704 case SHT_GNU_verneed:
2705 this_hdr->sh_entsize = 0;
2706 /* objcopy or strip will copy over sh_info, but may not set
2707 cverrefs. The linker will set cverrefs, but sh_info will be
2708 zero. */
2709 if (this_hdr->sh_info == 0)
2710 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2711 else
2712 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2713 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2714 break;
2716 case SHT_GROUP:
2717 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2718 break;
2720 case SHT_GNU_HASH:
2721 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2722 break;
2725 if ((asect->flags & SEC_ALLOC) != 0)
2726 this_hdr->sh_flags |= SHF_ALLOC;
2727 if ((asect->flags & SEC_READONLY) == 0)
2728 this_hdr->sh_flags |= SHF_WRITE;
2729 if ((asect->flags & SEC_CODE) != 0)
2730 this_hdr->sh_flags |= SHF_EXECINSTR;
2731 if ((asect->flags & SEC_MERGE) != 0)
2733 this_hdr->sh_flags |= SHF_MERGE;
2734 this_hdr->sh_entsize = asect->entsize;
2735 if ((asect->flags & SEC_STRINGS) != 0)
2736 this_hdr->sh_flags |= SHF_STRINGS;
2738 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2739 this_hdr->sh_flags |= SHF_GROUP;
2740 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2742 this_hdr->sh_flags |= SHF_TLS;
2743 if (asect->size == 0
2744 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2746 struct bfd_link_order *o = asect->map_tail.link_order;
2748 this_hdr->sh_size = 0;
2749 if (o != NULL)
2751 this_hdr->sh_size = o->offset + o->size;
2752 if (this_hdr->sh_size != 0)
2753 this_hdr->sh_type = SHT_NOBITS;
2757 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2758 this_hdr->sh_flags |= SHF_EXCLUDE;
2760 /* If the section has relocs, set up a section header for the
2761 SHT_REL[A] section. If two relocation sections are required for
2762 this section, it is up to the processor-specific back-end to
2763 create the other. */
2764 if ((asect->flags & SEC_RELOC) != 0)
2766 /* When doing a relocatable link, create both REL and RELA sections if
2767 needed. */
2768 if (arg->link_info
2769 /* Do the normal setup if we wouldn't create any sections here. */
2770 && esd->rel.count + esd->rela.count > 0
2771 && (arg->link_info->relocatable || arg->link_info->emitrelocations))
2773 if (esd->rel.count && esd->rel.hdr == NULL
2774 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE))
2776 arg->failed = TRUE;
2777 return;
2779 if (esd->rela.count && esd->rela.hdr == NULL
2780 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE))
2782 arg->failed = TRUE;
2783 return;
2786 else if (!_bfd_elf_init_reloc_shdr (abfd,
2787 (asect->use_rela_p
2788 ? &esd->rela : &esd->rel),
2789 asect,
2790 asect->use_rela_p))
2791 arg->failed = TRUE;
2794 /* Check for processor-specific section types. */
2795 sh_type = this_hdr->sh_type;
2796 if (bed->elf_backend_fake_sections
2797 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2798 arg->failed = TRUE;
2800 if (sh_type == SHT_NOBITS && asect->size != 0)
2802 /* Don't change the header type from NOBITS if we are being
2803 called for objcopy --only-keep-debug. */
2804 this_hdr->sh_type = sh_type;
2808 /* Fill in the contents of a SHT_GROUP section. Called from
2809 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2810 when ELF targets use the generic linker, ld. Called for ld -r
2811 from bfd_elf_final_link. */
2813 void
2814 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2816 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2817 asection *elt, *first;
2818 unsigned char *loc;
2819 bfd_boolean gas;
2821 /* Ignore linker created group section. See elfNN_ia64_object_p in
2822 elfxx-ia64.c. */
2823 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2824 || *failedptr)
2825 return;
2827 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2829 unsigned long symindx = 0;
2831 /* elf_group_id will have been set up by objcopy and the
2832 generic linker. */
2833 if (elf_group_id (sec) != NULL)
2834 symindx = elf_group_id (sec)->udata.i;
2836 if (symindx == 0)
2838 /* If called from the assembler, swap_out_syms will have set up
2839 elf_section_syms. */
2840 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2841 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2843 elf_section_data (sec)->this_hdr.sh_info = symindx;
2845 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2847 /* The ELF backend linker sets sh_info to -2 when the group
2848 signature symbol is global, and thus the index can't be
2849 set until all local symbols are output. */
2850 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2851 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2852 unsigned long symndx = sec_data->this_hdr.sh_info;
2853 unsigned long extsymoff = 0;
2854 struct elf_link_hash_entry *h;
2856 if (!elf_bad_symtab (igroup->owner))
2858 Elf_Internal_Shdr *symtab_hdr;
2860 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2861 extsymoff = symtab_hdr->sh_info;
2863 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2864 while (h->root.type == bfd_link_hash_indirect
2865 || h->root.type == bfd_link_hash_warning)
2866 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2868 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2871 /* The contents won't be allocated for "ld -r" or objcopy. */
2872 gas = TRUE;
2873 if (sec->contents == NULL)
2875 gas = FALSE;
2876 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2878 /* Arrange for the section to be written out. */
2879 elf_section_data (sec)->this_hdr.contents = sec->contents;
2880 if (sec->contents == NULL)
2882 *failedptr = TRUE;
2883 return;
2887 loc = sec->contents + sec->size;
2889 /* Get the pointer to the first section in the group that gas
2890 squirreled away here. objcopy arranges for this to be set to the
2891 start of the input section group. */
2892 first = elt = elf_next_in_group (sec);
2894 /* First element is a flag word. Rest of section is elf section
2895 indices for all the sections of the group. Write them backwards
2896 just to keep the group in the same order as given in .section
2897 directives, not that it matters. */
2898 while (elt != NULL)
2900 asection *s;
2902 s = elt;
2903 if (!gas)
2904 s = s->output_section;
2905 if (s != NULL
2906 && !bfd_is_abs_section (s))
2908 unsigned int idx = elf_section_data (s)->this_idx;
2910 loc -= 4;
2911 H_PUT_32 (abfd, idx, loc);
2913 elt = elf_next_in_group (elt);
2914 if (elt == first)
2915 break;
2918 if ((loc -= 4) != sec->contents)
2919 abort ();
2921 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2924 /* Assign all ELF section numbers. The dummy first section is handled here
2925 too. The link/info pointers for the standard section types are filled
2926 in here too, while we're at it. */
2928 static bfd_boolean
2929 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2931 struct elf_obj_tdata *t = elf_tdata (abfd);
2932 asection *sec;
2933 unsigned int section_number, secn;
2934 Elf_Internal_Shdr **i_shdrp;
2935 struct bfd_elf_section_data *d;
2936 bfd_boolean need_symtab;
2938 section_number = 1;
2940 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2942 /* SHT_GROUP sections are in relocatable files only. */
2943 if (link_info == NULL || link_info->relocatable)
2945 /* Put SHT_GROUP sections first. */
2946 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2948 d = elf_section_data (sec);
2950 if (d->this_hdr.sh_type == SHT_GROUP)
2952 if (sec->flags & SEC_LINKER_CREATED)
2954 /* Remove the linker created SHT_GROUP sections. */
2955 bfd_section_list_remove (abfd, sec);
2956 abfd->section_count--;
2958 else
2959 d->this_idx = section_number++;
2964 for (sec = abfd->sections; sec; sec = sec->next)
2966 d = elf_section_data (sec);
2968 if (d->this_hdr.sh_type != SHT_GROUP)
2969 d->this_idx = section_number++;
2970 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2971 if (d->rel.hdr)
2973 d->rel.idx = section_number++;
2974 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2976 else
2977 d->rel.idx = 0;
2979 if (d->rela.hdr)
2981 d->rela.idx = section_number++;
2982 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2984 else
2985 d->rela.idx = 0;
2988 elf_shstrtab_sec (abfd) = section_number++;
2989 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2990 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
2992 need_symtab = (bfd_get_symcount (abfd) > 0
2993 || (link_info == NULL
2994 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
2995 == HAS_RELOC)));
2996 if (need_symtab)
2998 elf_onesymtab (abfd) = section_number++;
2999 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3000 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3002 elf_symtab_shndx (abfd) = section_number++;
3003 t->symtab_shndx_hdr.sh_name
3004 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3005 ".symtab_shndx", FALSE);
3006 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
3007 return FALSE;
3009 elf_strtab_sec (abfd) = section_number++;
3010 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3013 if (section_number >= SHN_LORESERVE)
3015 _bfd_error_handler (_("%B: too many sections: %u"),
3016 abfd, section_number);
3017 return FALSE;
3020 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
3021 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3023 elf_numsections (abfd) = section_number;
3024 elf_elfheader (abfd)->e_shnum = section_number;
3026 /* Set up the list of section header pointers, in agreement with the
3027 indices. */
3028 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3029 sizeof (Elf_Internal_Shdr *));
3030 if (i_shdrp == NULL)
3031 return FALSE;
3033 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3034 sizeof (Elf_Internal_Shdr));
3035 if (i_shdrp[0] == NULL)
3037 bfd_release (abfd, i_shdrp);
3038 return FALSE;
3041 elf_elfsections (abfd) = i_shdrp;
3043 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3044 if (need_symtab)
3046 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3047 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3049 i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr;
3050 t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd);
3052 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3053 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3056 for (sec = abfd->sections; sec; sec = sec->next)
3058 asection *s;
3059 const char *name;
3061 d = elf_section_data (sec);
3063 i_shdrp[d->this_idx] = &d->this_hdr;
3064 if (d->rel.idx != 0)
3065 i_shdrp[d->rel.idx] = d->rel.hdr;
3066 if (d->rela.idx != 0)
3067 i_shdrp[d->rela.idx] = d->rela.hdr;
3069 /* Fill in the sh_link and sh_info fields while we're at it. */
3071 /* sh_link of a reloc section is the section index of the symbol
3072 table. sh_info is the section index of the section to which
3073 the relocation entries apply. */
3074 if (d->rel.idx != 0)
3076 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3077 d->rel.hdr->sh_info = d->this_idx;
3078 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3080 if (d->rela.idx != 0)
3082 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3083 d->rela.hdr->sh_info = d->this_idx;
3084 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3087 /* We need to set up sh_link for SHF_LINK_ORDER. */
3088 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3090 s = elf_linked_to_section (sec);
3091 if (s)
3093 /* elf_linked_to_section points to the input section. */
3094 if (link_info != NULL)
3096 /* Check discarded linkonce section. */
3097 if (discarded_section (s))
3099 asection *kept;
3100 (*_bfd_error_handler)
3101 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3102 abfd, d->this_hdr.bfd_section,
3103 s, s->owner);
3104 /* Point to the kept section if it has the same
3105 size as the discarded one. */
3106 kept = _bfd_elf_check_kept_section (s, link_info);
3107 if (kept == NULL)
3109 bfd_set_error (bfd_error_bad_value);
3110 return FALSE;
3112 s = kept;
3115 s = s->output_section;
3116 BFD_ASSERT (s != NULL);
3118 else
3120 /* Handle objcopy. */
3121 if (s->output_section == NULL)
3123 (*_bfd_error_handler)
3124 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3125 abfd, d->this_hdr.bfd_section, s, s->owner);
3126 bfd_set_error (bfd_error_bad_value);
3127 return FALSE;
3129 s = s->output_section;
3131 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3133 else
3135 /* PR 290:
3136 The Intel C compiler generates SHT_IA_64_UNWIND with
3137 SHF_LINK_ORDER. But it doesn't set the sh_link or
3138 sh_info fields. Hence we could get the situation
3139 where s is NULL. */
3140 const struct elf_backend_data *bed
3141 = get_elf_backend_data (abfd);
3142 if (bed->link_order_error_handler)
3143 bed->link_order_error_handler
3144 (_("%B: warning: sh_link not set for section `%A'"),
3145 abfd, sec);
3149 switch (d->this_hdr.sh_type)
3151 case SHT_REL:
3152 case SHT_RELA:
3153 /* A reloc section which we are treating as a normal BFD
3154 section. sh_link is the section index of the symbol
3155 table. sh_info is the section index of the section to
3156 which the relocation entries apply. We assume that an
3157 allocated reloc section uses the dynamic symbol table.
3158 FIXME: How can we be sure? */
3159 s = bfd_get_section_by_name (abfd, ".dynsym");
3160 if (s != NULL)
3161 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3163 /* We look up the section the relocs apply to by name. */
3164 name = sec->name;
3165 if (d->this_hdr.sh_type == SHT_REL)
3166 name += 4;
3167 else
3168 name += 5;
3169 s = bfd_get_section_by_name (abfd, name);
3170 if (s != NULL)
3172 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3173 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3175 break;
3177 case SHT_STRTAB:
3178 /* We assume that a section named .stab*str is a stabs
3179 string section. We look for a section with the same name
3180 but without the trailing ``str'', and set its sh_link
3181 field to point to this section. */
3182 if (CONST_STRNEQ (sec->name, ".stab")
3183 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3185 size_t len;
3186 char *alc;
3188 len = strlen (sec->name);
3189 alc = (char *) bfd_malloc (len - 2);
3190 if (alc == NULL)
3191 return FALSE;
3192 memcpy (alc, sec->name, len - 3);
3193 alc[len - 3] = '\0';
3194 s = bfd_get_section_by_name (abfd, alc);
3195 free (alc);
3196 if (s != NULL)
3198 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3200 /* This is a .stab section. */
3201 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3202 elf_section_data (s)->this_hdr.sh_entsize
3203 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3206 break;
3208 case SHT_DYNAMIC:
3209 case SHT_DYNSYM:
3210 case SHT_GNU_verneed:
3211 case SHT_GNU_verdef:
3212 /* sh_link is the section header index of the string table
3213 used for the dynamic entries, or the symbol table, or the
3214 version strings. */
3215 s = bfd_get_section_by_name (abfd, ".dynstr");
3216 if (s != NULL)
3217 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3218 break;
3220 case SHT_GNU_LIBLIST:
3221 /* sh_link is the section header index of the prelink library
3222 list used for the dynamic entries, or the symbol table, or
3223 the version strings. */
3224 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3225 ? ".dynstr" : ".gnu.libstr");
3226 if (s != NULL)
3227 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3228 break;
3230 case SHT_HASH:
3231 case SHT_GNU_HASH:
3232 case SHT_GNU_versym:
3233 /* sh_link is the section header index of the symbol table
3234 this hash table or version table is for. */
3235 s = bfd_get_section_by_name (abfd, ".dynsym");
3236 if (s != NULL)
3237 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3238 break;
3240 case SHT_GROUP:
3241 d->this_hdr.sh_link = elf_onesymtab (abfd);
3245 for (secn = 1; secn < section_number; ++secn)
3246 if (i_shdrp[secn] == NULL)
3247 i_shdrp[secn] = i_shdrp[0];
3248 else
3249 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3250 i_shdrp[secn]->sh_name);
3251 return TRUE;
3254 static bfd_boolean
3255 sym_is_global (bfd *abfd, asymbol *sym)
3257 /* If the backend has a special mapping, use it. */
3258 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3259 if (bed->elf_backend_sym_is_global)
3260 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3262 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3263 || bfd_is_und_section (bfd_get_section (sym))
3264 || bfd_is_com_section (bfd_get_section (sym)));
3267 /* Don't output section symbols for sections that are not going to be
3268 output, that are duplicates or there is no BFD section. */
3270 static bfd_boolean
3271 ignore_section_sym (bfd *abfd, asymbol *sym)
3273 elf_symbol_type *type_ptr;
3275 if ((sym->flags & BSF_SECTION_SYM) == 0)
3276 return FALSE;
3278 type_ptr = elf_symbol_from (abfd, sym);
3279 return ((type_ptr != NULL
3280 && type_ptr->internal_elf_sym.st_shndx != 0
3281 && bfd_is_abs_section (sym->section))
3282 || !(sym->section->owner == abfd
3283 || (sym->section->output_section->owner == abfd
3284 && sym->section->output_offset == 0)
3285 || bfd_is_abs_section (sym->section)));
3288 /* Map symbol from it's internal number to the external number, moving
3289 all local symbols to be at the head of the list. */
3291 static bfd_boolean
3292 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
3294 unsigned int symcount = bfd_get_symcount (abfd);
3295 asymbol **syms = bfd_get_outsymbols (abfd);
3296 asymbol **sect_syms;
3297 unsigned int num_locals = 0;
3298 unsigned int num_globals = 0;
3299 unsigned int num_locals2 = 0;
3300 unsigned int num_globals2 = 0;
3301 int max_index = 0;
3302 unsigned int idx;
3303 asection *asect;
3304 asymbol **new_syms;
3306 #ifdef DEBUG
3307 fprintf (stderr, "elf_map_symbols\n");
3308 fflush (stderr);
3309 #endif
3311 for (asect = abfd->sections; asect; asect = asect->next)
3313 if (max_index < asect->index)
3314 max_index = asect->index;
3317 max_index++;
3318 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3319 if (sect_syms == NULL)
3320 return FALSE;
3321 elf_section_syms (abfd) = sect_syms;
3322 elf_num_section_syms (abfd) = max_index;
3324 /* Init sect_syms entries for any section symbols we have already
3325 decided to output. */
3326 for (idx = 0; idx < symcount; idx++)
3328 asymbol *sym = syms[idx];
3330 if ((sym->flags & BSF_SECTION_SYM) != 0
3331 && sym->value == 0
3332 && !ignore_section_sym (abfd, sym)
3333 && !bfd_is_abs_section (sym->section))
3335 asection *sec = sym->section;
3337 if (sec->owner != abfd)
3338 sec = sec->output_section;
3340 sect_syms[sec->index] = syms[idx];
3344 /* Classify all of the symbols. */
3345 for (idx = 0; idx < symcount; idx++)
3347 if (sym_is_global (abfd, syms[idx]))
3348 num_globals++;
3349 else if (!ignore_section_sym (abfd, syms[idx]))
3350 num_locals++;
3353 /* We will be adding a section symbol for each normal BFD section. Most
3354 sections will already have a section symbol in outsymbols, but
3355 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3356 at least in that case. */
3357 for (asect = abfd->sections; asect; asect = asect->next)
3359 if (sect_syms[asect->index] == NULL)
3361 if (!sym_is_global (abfd, asect->symbol))
3362 num_locals++;
3363 else
3364 num_globals++;
3368 /* Now sort the symbols so the local symbols are first. */
3369 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3370 sizeof (asymbol *));
3372 if (new_syms == NULL)
3373 return FALSE;
3375 for (idx = 0; idx < symcount; idx++)
3377 asymbol *sym = syms[idx];
3378 unsigned int i;
3380 if (sym_is_global (abfd, sym))
3381 i = num_locals + num_globals2++;
3382 else if (!ignore_section_sym (abfd, sym))
3383 i = num_locals2++;
3384 else
3385 continue;
3386 new_syms[i] = sym;
3387 sym->udata.i = i + 1;
3389 for (asect = abfd->sections; asect; asect = asect->next)
3391 if (sect_syms[asect->index] == NULL)
3393 asymbol *sym = asect->symbol;
3394 unsigned int i;
3396 sect_syms[asect->index] = sym;
3397 if (!sym_is_global (abfd, sym))
3398 i = num_locals2++;
3399 else
3400 i = num_locals + num_globals2++;
3401 new_syms[i] = sym;
3402 sym->udata.i = i + 1;
3406 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3408 *pnum_locals = num_locals;
3409 return TRUE;
3412 /* Align to the maximum file alignment that could be required for any
3413 ELF data structure. */
3415 static inline file_ptr
3416 align_file_position (file_ptr off, int align)
3418 return (off + align - 1) & ~(align - 1);
3421 /* Assign a file position to a section, optionally aligning to the
3422 required section alignment. */
3424 file_ptr
3425 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3426 file_ptr offset,
3427 bfd_boolean align)
3429 if (align && i_shdrp->sh_addralign > 1)
3430 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3431 i_shdrp->sh_offset = offset;
3432 if (i_shdrp->bfd_section != NULL)
3433 i_shdrp->bfd_section->filepos = offset;
3434 if (i_shdrp->sh_type != SHT_NOBITS)
3435 offset += i_shdrp->sh_size;
3436 return offset;
3439 /* Compute the file positions we are going to put the sections at, and
3440 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3441 is not NULL, this is being called by the ELF backend linker. */
3443 bfd_boolean
3444 _bfd_elf_compute_section_file_positions (bfd *abfd,
3445 struct bfd_link_info *link_info)
3447 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3448 struct fake_section_arg fsargs;
3449 bfd_boolean failed;
3450 struct bfd_strtab_hash *strtab = NULL;
3451 Elf_Internal_Shdr *shstrtab_hdr;
3452 bfd_boolean need_symtab;
3454 if (abfd->output_has_begun)
3455 return TRUE;
3457 /* Do any elf backend specific processing first. */
3458 if (bed->elf_backend_begin_write_processing)
3459 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3461 if (! prep_headers (abfd))
3462 return FALSE;
3464 /* Post process the headers if necessary. */
3465 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3467 fsargs.failed = FALSE;
3468 fsargs.link_info = link_info;
3469 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3470 if (fsargs.failed)
3471 return FALSE;
3473 if (!assign_section_numbers (abfd, link_info))
3474 return FALSE;
3476 /* The backend linker builds symbol table information itself. */
3477 need_symtab = (link_info == NULL
3478 && (bfd_get_symcount (abfd) > 0
3479 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3480 == HAS_RELOC)));
3481 if (need_symtab)
3483 /* Non-zero if doing a relocatable link. */
3484 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3486 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3487 return FALSE;
3490 failed = FALSE;
3491 if (link_info == NULL)
3493 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3494 if (failed)
3495 return FALSE;
3498 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3499 /* sh_name was set in prep_headers. */
3500 shstrtab_hdr->sh_type = SHT_STRTAB;
3501 shstrtab_hdr->sh_flags = 0;
3502 shstrtab_hdr->sh_addr = 0;
3503 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3504 shstrtab_hdr->sh_entsize = 0;
3505 shstrtab_hdr->sh_link = 0;
3506 shstrtab_hdr->sh_info = 0;
3507 /* sh_offset is set in assign_file_positions_except_relocs. */
3508 shstrtab_hdr->sh_addralign = 1;
3510 if (!assign_file_positions_except_relocs (abfd, link_info))
3511 return FALSE;
3513 if (need_symtab)
3515 file_ptr off;
3516 Elf_Internal_Shdr *hdr;
3518 off = elf_next_file_pos (abfd);
3520 hdr = &elf_tdata (abfd)->symtab_hdr;
3521 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3523 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3524 if (hdr->sh_size != 0)
3525 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3527 hdr = &elf_tdata (abfd)->strtab_hdr;
3528 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3530 elf_next_file_pos (abfd) = off;
3532 /* Now that we know where the .strtab section goes, write it
3533 out. */
3534 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3535 || ! _bfd_stringtab_emit (abfd, strtab))
3536 return FALSE;
3537 _bfd_stringtab_free (strtab);
3540 abfd->output_has_begun = TRUE;
3542 return TRUE;
3545 /* Make an initial estimate of the size of the program header. If we
3546 get the number wrong here, we'll redo section placement. */
3548 static bfd_size_type
3549 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3551 size_t segs;
3552 asection *s;
3553 const struct elf_backend_data *bed;
3555 /* Assume we will need exactly two PT_LOAD segments: one for text
3556 and one for data. */
3557 segs = 2;
3559 s = bfd_get_section_by_name (abfd, ".interp");
3560 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3562 /* If we have a loadable interpreter section, we need a
3563 PT_INTERP segment. In this case, assume we also need a
3564 PT_PHDR segment, although that may not be true for all
3565 targets. */
3566 segs += 2;
3569 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3571 /* We need a PT_DYNAMIC segment. */
3572 ++segs;
3575 if (info != NULL && info->relro)
3577 /* We need a PT_GNU_RELRO segment. */
3578 ++segs;
3581 if (elf_eh_frame_hdr (abfd))
3583 /* We need a PT_GNU_EH_FRAME segment. */
3584 ++segs;
3587 if (elf_stack_flags (abfd))
3589 /* We need a PT_GNU_STACK segment. */
3590 ++segs;
3593 for (s = abfd->sections; s != NULL; s = s->next)
3595 if ((s->flags & SEC_LOAD) != 0
3596 && CONST_STRNEQ (s->name, ".note"))
3598 /* We need a PT_NOTE segment. */
3599 ++segs;
3600 /* Try to create just one PT_NOTE segment
3601 for all adjacent loadable .note* sections.
3602 gABI requires that within a PT_NOTE segment
3603 (and also inside of each SHT_NOTE section)
3604 each note is padded to a multiple of 4 size,
3605 so we check whether the sections are correctly
3606 aligned. */
3607 if (s->alignment_power == 2)
3608 while (s->next != NULL
3609 && s->next->alignment_power == 2
3610 && (s->next->flags & SEC_LOAD) != 0
3611 && CONST_STRNEQ (s->next->name, ".note"))
3612 s = s->next;
3616 for (s = abfd->sections; s != NULL; s = s->next)
3618 if (s->flags & SEC_THREAD_LOCAL)
3620 /* We need a PT_TLS segment. */
3621 ++segs;
3622 break;
3626 /* Let the backend count up any program headers it might need. */
3627 bed = get_elf_backend_data (abfd);
3628 if (bed->elf_backend_additional_program_headers)
3630 int a;
3632 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3633 if (a == -1)
3634 abort ();
3635 segs += a;
3638 return segs * bed->s->sizeof_phdr;
3641 /* Find the segment that contains the output_section of section. */
3643 Elf_Internal_Phdr *
3644 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3646 struct elf_segment_map *m;
3647 Elf_Internal_Phdr *p;
3649 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
3650 m != NULL;
3651 m = m->next, p++)
3653 int i;
3655 for (i = m->count - 1; i >= 0; i--)
3656 if (m->sections[i] == section)
3657 return p;
3660 return NULL;
3663 /* Create a mapping from a set of sections to a program segment. */
3665 static struct elf_segment_map *
3666 make_mapping (bfd *abfd,
3667 asection **sections,
3668 unsigned int from,
3669 unsigned int to,
3670 bfd_boolean phdr)
3672 struct elf_segment_map *m;
3673 unsigned int i;
3674 asection **hdrpp;
3675 bfd_size_type amt;
3677 amt = sizeof (struct elf_segment_map);
3678 amt += (to - from - 1) * sizeof (asection *);
3679 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3680 if (m == NULL)
3681 return NULL;
3682 m->next = NULL;
3683 m->p_type = PT_LOAD;
3684 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3685 m->sections[i - from] = *hdrpp;
3686 m->count = to - from;
3688 if (from == 0 && phdr)
3690 /* Include the headers in the first PT_LOAD segment. */
3691 m->includes_filehdr = 1;
3692 m->includes_phdrs = 1;
3695 return m;
3698 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3699 on failure. */
3701 struct elf_segment_map *
3702 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3704 struct elf_segment_map *m;
3706 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3707 sizeof (struct elf_segment_map));
3708 if (m == NULL)
3709 return NULL;
3710 m->next = NULL;
3711 m->p_type = PT_DYNAMIC;
3712 m->count = 1;
3713 m->sections[0] = dynsec;
3715 return m;
3718 /* Possibly add or remove segments from the segment map. */
3720 static bfd_boolean
3721 elf_modify_segment_map (bfd *abfd,
3722 struct bfd_link_info *info,
3723 bfd_boolean remove_empty_load)
3725 struct elf_segment_map **m;
3726 const struct elf_backend_data *bed;
3728 /* The placement algorithm assumes that non allocated sections are
3729 not in PT_LOAD segments. We ensure this here by removing such
3730 sections from the segment map. We also remove excluded
3731 sections. Finally, any PT_LOAD segment without sections is
3732 removed. */
3733 m = &elf_seg_map (abfd);
3734 while (*m)
3736 unsigned int i, new_count;
3738 for (new_count = 0, i = 0; i < (*m)->count; i++)
3740 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3741 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3742 || (*m)->p_type != PT_LOAD))
3744 (*m)->sections[new_count] = (*m)->sections[i];
3745 new_count++;
3748 (*m)->count = new_count;
3750 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3751 *m = (*m)->next;
3752 else
3753 m = &(*m)->next;
3756 bed = get_elf_backend_data (abfd);
3757 if (bed->elf_backend_modify_segment_map != NULL)
3759 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3760 return FALSE;
3763 return TRUE;
3766 /* Set up a mapping from BFD sections to program segments. */
3768 bfd_boolean
3769 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3771 unsigned int count;
3772 struct elf_segment_map *m;
3773 asection **sections = NULL;
3774 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3775 bfd_boolean no_user_phdrs;
3777 no_user_phdrs = elf_seg_map (abfd) == NULL;
3779 if (info != NULL)
3780 info->user_phdrs = !no_user_phdrs;
3782 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3784 asection *s;
3785 unsigned int i;
3786 struct elf_segment_map *mfirst;
3787 struct elf_segment_map **pm;
3788 asection *last_hdr;
3789 bfd_vma last_size;
3790 unsigned int phdr_index;
3791 bfd_vma maxpagesize;
3792 asection **hdrpp;
3793 bfd_boolean phdr_in_segment = TRUE;
3794 bfd_boolean writable;
3795 int tls_count = 0;
3796 asection *first_tls = NULL;
3797 asection *dynsec, *eh_frame_hdr;
3798 bfd_size_type amt;
3799 bfd_vma addr_mask, wrap_to = 0;
3801 /* Select the allocated sections, and sort them. */
3803 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3804 sizeof (asection *));
3805 if (sections == NULL)
3806 goto error_return;
3808 /* Calculate top address, avoiding undefined behaviour of shift
3809 left operator when shift count is equal to size of type
3810 being shifted. */
3811 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
3812 addr_mask = (addr_mask << 1) + 1;
3814 i = 0;
3815 for (s = abfd->sections; s != NULL; s = s->next)
3817 if ((s->flags & SEC_ALLOC) != 0)
3819 sections[i] = s;
3820 ++i;
3821 /* A wrapping section potentially clashes with header. */
3822 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
3823 wrap_to = (s->lma + s->size) & addr_mask;
3826 BFD_ASSERT (i <= bfd_count_sections (abfd));
3827 count = i;
3829 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3831 /* Build the mapping. */
3833 mfirst = NULL;
3834 pm = &mfirst;
3836 /* If we have a .interp section, then create a PT_PHDR segment for
3837 the program headers and a PT_INTERP segment for the .interp
3838 section. */
3839 s = bfd_get_section_by_name (abfd, ".interp");
3840 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3842 amt = sizeof (struct elf_segment_map);
3843 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3844 if (m == NULL)
3845 goto error_return;
3846 m->next = NULL;
3847 m->p_type = PT_PHDR;
3848 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3849 m->p_flags = PF_R | PF_X;
3850 m->p_flags_valid = 1;
3851 m->includes_phdrs = 1;
3853 *pm = m;
3854 pm = &m->next;
3856 amt = sizeof (struct elf_segment_map);
3857 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3858 if (m == NULL)
3859 goto error_return;
3860 m->next = NULL;
3861 m->p_type = PT_INTERP;
3862 m->count = 1;
3863 m->sections[0] = s;
3865 *pm = m;
3866 pm = &m->next;
3869 /* Look through the sections. We put sections in the same program
3870 segment when the start of the second section can be placed within
3871 a few bytes of the end of the first section. */
3872 last_hdr = NULL;
3873 last_size = 0;
3874 phdr_index = 0;
3875 maxpagesize = bed->maxpagesize;
3876 writable = FALSE;
3877 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3878 if (dynsec != NULL
3879 && (dynsec->flags & SEC_LOAD) == 0)
3880 dynsec = NULL;
3882 /* Deal with -Ttext or something similar such that the first section
3883 is not adjacent to the program headers. This is an
3884 approximation, since at this point we don't know exactly how many
3885 program headers we will need. */
3886 if (count > 0)
3888 bfd_size_type phdr_size = elf_program_header_size (abfd);
3890 if (phdr_size == (bfd_size_type) -1)
3891 phdr_size = get_program_header_size (abfd, info);
3892 phdr_size += bed->s->sizeof_ehdr;
3893 if ((abfd->flags & D_PAGED) == 0
3894 || (sections[0]->lma & addr_mask) < phdr_size
3895 || ((sections[0]->lma & addr_mask) % maxpagesize
3896 < phdr_size % maxpagesize)
3897 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
3898 phdr_in_segment = FALSE;
3901 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3903 asection *hdr;
3904 bfd_boolean new_segment;
3906 hdr = *hdrpp;
3908 /* See if this section and the last one will fit in the same
3909 segment. */
3911 if (last_hdr == NULL)
3913 /* If we don't have a segment yet, then we don't need a new
3914 one (we build the last one after this loop). */
3915 new_segment = FALSE;
3917 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3919 /* If this section has a different relation between the
3920 virtual address and the load address, then we need a new
3921 segment. */
3922 new_segment = TRUE;
3924 else if (hdr->lma < last_hdr->lma + last_size
3925 || last_hdr->lma + last_size < last_hdr->lma)
3927 /* If this section has a load address that makes it overlap
3928 the previous section, then we need a new segment. */
3929 new_segment = TRUE;
3931 /* In the next test we have to be careful when last_hdr->lma is close
3932 to the end of the address space. If the aligned address wraps
3933 around to the start of the address space, then there are no more
3934 pages left in memory and it is OK to assume that the current
3935 section can be included in the current segment. */
3936 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3937 > last_hdr->lma)
3938 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3939 <= hdr->lma))
3941 /* If putting this section in this segment would force us to
3942 skip a page in the segment, then we need a new segment. */
3943 new_segment = TRUE;
3945 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3946 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3948 /* We don't want to put a loadable section after a
3949 nonloadable section in the same segment.
3950 Consider .tbss sections as loadable for this purpose. */
3951 new_segment = TRUE;
3953 else if ((abfd->flags & D_PAGED) == 0)
3955 /* If the file is not demand paged, which means that we
3956 don't require the sections to be correctly aligned in the
3957 file, then there is no other reason for a new segment. */
3958 new_segment = FALSE;
3960 else if (! writable
3961 && (hdr->flags & SEC_READONLY) == 0
3962 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
3963 != (hdr->lma & -maxpagesize)))
3965 /* We don't want to put a writable section in a read only
3966 segment, unless they are on the same page in memory
3967 anyhow. We already know that the last section does not
3968 bring us past the current section on the page, so the
3969 only case in which the new section is not on the same
3970 page as the previous section is when the previous section
3971 ends precisely on a page boundary. */
3972 new_segment = TRUE;
3974 else
3976 /* Otherwise, we can use the same segment. */
3977 new_segment = FALSE;
3980 /* Allow interested parties a chance to override our decision. */
3981 if (last_hdr != NULL
3982 && info != NULL
3983 && info->callbacks->override_segment_assignment != NULL)
3984 new_segment
3985 = info->callbacks->override_segment_assignment (info, abfd, hdr,
3986 last_hdr,
3987 new_segment);
3989 if (! new_segment)
3991 if ((hdr->flags & SEC_READONLY) == 0)
3992 writable = TRUE;
3993 last_hdr = hdr;
3994 /* .tbss sections effectively have zero size. */
3995 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3996 != SEC_THREAD_LOCAL)
3997 last_size = hdr->size;
3998 else
3999 last_size = 0;
4000 continue;
4003 /* We need a new program segment. We must create a new program
4004 header holding all the sections from phdr_index until hdr. */
4006 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4007 if (m == NULL)
4008 goto error_return;
4010 *pm = m;
4011 pm = &m->next;
4013 if ((hdr->flags & SEC_READONLY) == 0)
4014 writable = TRUE;
4015 else
4016 writable = FALSE;
4018 last_hdr = hdr;
4019 /* .tbss sections effectively have zero size. */
4020 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4021 last_size = hdr->size;
4022 else
4023 last_size = 0;
4024 phdr_index = i;
4025 phdr_in_segment = FALSE;
4028 /* Create a final PT_LOAD program segment, but not if it's just
4029 for .tbss. */
4030 if (last_hdr != NULL
4031 && (i - phdr_index != 1
4032 || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4033 != SEC_THREAD_LOCAL)))
4035 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4036 if (m == NULL)
4037 goto error_return;
4039 *pm = m;
4040 pm = &m->next;
4043 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4044 if (dynsec != NULL)
4046 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4047 if (m == NULL)
4048 goto error_return;
4049 *pm = m;
4050 pm = &m->next;
4053 /* For each batch of consecutive loadable .note sections,
4054 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4055 because if we link together nonloadable .note sections and
4056 loadable .note sections, we will generate two .note sections
4057 in the output file. FIXME: Using names for section types is
4058 bogus anyhow. */
4059 for (s = abfd->sections; s != NULL; s = s->next)
4061 if ((s->flags & SEC_LOAD) != 0
4062 && CONST_STRNEQ (s->name, ".note"))
4064 asection *s2;
4066 count = 1;
4067 amt = sizeof (struct elf_segment_map);
4068 if (s->alignment_power == 2)
4069 for (s2 = s; s2->next != NULL; s2 = s2->next)
4071 if (s2->next->alignment_power == 2
4072 && (s2->next->flags & SEC_LOAD) != 0
4073 && CONST_STRNEQ (s2->next->name, ".note")
4074 && align_power (s2->lma + s2->size, 2)
4075 == s2->next->lma)
4076 count++;
4077 else
4078 break;
4080 amt += (count - 1) * sizeof (asection *);
4081 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4082 if (m == NULL)
4083 goto error_return;
4084 m->next = NULL;
4085 m->p_type = PT_NOTE;
4086 m->count = count;
4087 while (count > 1)
4089 m->sections[m->count - count--] = s;
4090 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4091 s = s->next;
4093 m->sections[m->count - 1] = s;
4094 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4095 *pm = m;
4096 pm = &m->next;
4098 if (s->flags & SEC_THREAD_LOCAL)
4100 if (! tls_count)
4101 first_tls = s;
4102 tls_count++;
4106 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4107 if (tls_count > 0)
4109 amt = sizeof (struct elf_segment_map);
4110 amt += (tls_count - 1) * sizeof (asection *);
4111 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4112 if (m == NULL)
4113 goto error_return;
4114 m->next = NULL;
4115 m->p_type = PT_TLS;
4116 m->count = tls_count;
4117 /* Mandated PF_R. */
4118 m->p_flags = PF_R;
4119 m->p_flags_valid = 1;
4120 s = first_tls;
4121 for (i = 0; i < (unsigned int) tls_count; ++i)
4123 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4125 _bfd_error_handler
4126 (_("%B: TLS sections are not adjacent:"), abfd);
4127 s = first_tls;
4128 i = 0;
4129 while (i < (unsigned int) tls_count)
4131 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4133 _bfd_error_handler (_(" TLS: %A"), s);
4134 i++;
4136 else
4137 _bfd_error_handler (_(" non-TLS: %A"), s);
4138 s = s->next;
4140 bfd_set_error (bfd_error_bad_value);
4141 goto error_return;
4143 m->sections[i] = s;
4144 s = s->next;
4147 *pm = m;
4148 pm = &m->next;
4151 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4152 segment. */
4153 eh_frame_hdr = elf_eh_frame_hdr (abfd);
4154 if (eh_frame_hdr != NULL
4155 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4157 amt = sizeof (struct elf_segment_map);
4158 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4159 if (m == NULL)
4160 goto error_return;
4161 m->next = NULL;
4162 m->p_type = PT_GNU_EH_FRAME;
4163 m->count = 1;
4164 m->sections[0] = eh_frame_hdr->output_section;
4166 *pm = m;
4167 pm = &m->next;
4170 if (elf_stack_flags (abfd))
4172 amt = sizeof (struct elf_segment_map);
4173 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4174 if (m == NULL)
4175 goto error_return;
4176 m->next = NULL;
4177 m->p_type = PT_GNU_STACK;
4178 m->p_flags = elf_stack_flags (abfd);
4179 m->p_align = bed->stack_align;
4180 m->p_flags_valid = 1;
4181 m->p_align_valid = m->p_align != 0;
4182 if (info->stacksize > 0)
4184 m->p_size = info->stacksize;
4185 m->p_size_valid = 1;
4188 *pm = m;
4189 pm = &m->next;
4192 if (info != NULL && info->relro)
4194 for (m = mfirst; m != NULL; m = m->next)
4196 if (m->p_type == PT_LOAD
4197 && m->count != 0
4198 && m->sections[0]->vma >= info->relro_start
4199 && m->sections[0]->vma < info->relro_end)
4201 i = m->count;
4202 while (--i != (unsigned) -1)
4203 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
4204 == (SEC_LOAD | SEC_HAS_CONTENTS))
4205 break;
4207 if (i != (unsigned) -1)
4208 break;
4212 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4213 if (m != NULL)
4215 amt = sizeof (struct elf_segment_map);
4216 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4217 if (m == NULL)
4218 goto error_return;
4219 m->next = NULL;
4220 m->p_type = PT_GNU_RELRO;
4221 m->p_flags = PF_R;
4222 m->p_flags_valid = 1;
4224 *pm = m;
4225 pm = &m->next;
4229 free (sections);
4230 elf_seg_map (abfd) = mfirst;
4233 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4234 return FALSE;
4236 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
4237 ++count;
4238 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
4240 return TRUE;
4242 error_return:
4243 if (sections != NULL)
4244 free (sections);
4245 return FALSE;
4248 /* Sort sections by address. */
4250 static int
4251 elf_sort_sections (const void *arg1, const void *arg2)
4253 const asection *sec1 = *(const asection **) arg1;
4254 const asection *sec2 = *(const asection **) arg2;
4255 bfd_size_type size1, size2;
4257 /* Sort by LMA first, since this is the address used to
4258 place the section into a segment. */
4259 if (sec1->lma < sec2->lma)
4260 return -1;
4261 else if (sec1->lma > sec2->lma)
4262 return 1;
4264 /* Then sort by VMA. Normally the LMA and the VMA will be
4265 the same, and this will do nothing. */
4266 if (sec1->vma < sec2->vma)
4267 return -1;
4268 else if (sec1->vma > sec2->vma)
4269 return 1;
4271 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4273 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4275 if (TOEND (sec1))
4277 if (TOEND (sec2))
4279 /* If the indicies are the same, do not return 0
4280 here, but continue to try the next comparison. */
4281 if (sec1->target_index - sec2->target_index != 0)
4282 return sec1->target_index - sec2->target_index;
4284 else
4285 return 1;
4287 else if (TOEND (sec2))
4288 return -1;
4290 #undef TOEND
4292 /* Sort by size, to put zero sized sections
4293 before others at the same address. */
4295 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4296 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4298 if (size1 < size2)
4299 return -1;
4300 if (size1 > size2)
4301 return 1;
4303 return sec1->target_index - sec2->target_index;
4306 /* Ian Lance Taylor writes:
4308 We shouldn't be using % with a negative signed number. That's just
4309 not good. We have to make sure either that the number is not
4310 negative, or that the number has an unsigned type. When the types
4311 are all the same size they wind up as unsigned. When file_ptr is a
4312 larger signed type, the arithmetic winds up as signed long long,
4313 which is wrong.
4315 What we're trying to say here is something like ``increase OFF by
4316 the least amount that will cause it to be equal to the VMA modulo
4317 the page size.'' */
4318 /* In other words, something like:
4320 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4321 off_offset = off % bed->maxpagesize;
4322 if (vma_offset < off_offset)
4323 adjustment = vma_offset + bed->maxpagesize - off_offset;
4324 else
4325 adjustment = vma_offset - off_offset;
4327 which can can be collapsed into the expression below. */
4329 static file_ptr
4330 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4332 /* PR binutils/16199: Handle an alignment of zero. */
4333 if (maxpagesize == 0)
4334 maxpagesize = 1;
4335 return ((vma - off) % maxpagesize);
4338 static void
4339 print_segment_map (const struct elf_segment_map *m)
4341 unsigned int j;
4342 const char *pt = get_segment_type (m->p_type);
4343 char buf[32];
4345 if (pt == NULL)
4347 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4348 sprintf (buf, "LOPROC+%7.7x",
4349 (unsigned int) (m->p_type - PT_LOPROC));
4350 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4351 sprintf (buf, "LOOS+%7.7x",
4352 (unsigned int) (m->p_type - PT_LOOS));
4353 else
4354 snprintf (buf, sizeof (buf), "%8.8x",
4355 (unsigned int) m->p_type);
4356 pt = buf;
4358 fflush (stdout);
4359 fprintf (stderr, "%s:", pt);
4360 for (j = 0; j < m->count; j++)
4361 fprintf (stderr, " %s", m->sections [j]->name);
4362 putc ('\n',stderr);
4363 fflush (stderr);
4366 static bfd_boolean
4367 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4369 void *buf;
4370 bfd_boolean ret;
4372 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4373 return FALSE;
4374 buf = bfd_zmalloc (len);
4375 if (buf == NULL)
4376 return FALSE;
4377 ret = bfd_bwrite (buf, len, abfd) == len;
4378 free (buf);
4379 return ret;
4382 /* Assign file positions to the sections based on the mapping from
4383 sections to segments. This function also sets up some fields in
4384 the file header. */
4386 static bfd_boolean
4387 assign_file_positions_for_load_sections (bfd *abfd,
4388 struct bfd_link_info *link_info)
4390 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4391 struct elf_segment_map *m;
4392 Elf_Internal_Phdr *phdrs;
4393 Elf_Internal_Phdr *p;
4394 file_ptr off;
4395 bfd_size_type maxpagesize;
4396 unsigned int alloc;
4397 unsigned int i, j;
4398 bfd_vma header_pad = 0;
4400 if (link_info == NULL
4401 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4402 return FALSE;
4404 alloc = 0;
4405 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4407 ++alloc;
4408 if (m->header_size)
4409 header_pad = m->header_size;
4412 if (alloc)
4414 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4415 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4417 else
4419 /* PR binutils/12467. */
4420 elf_elfheader (abfd)->e_phoff = 0;
4421 elf_elfheader (abfd)->e_phentsize = 0;
4424 elf_elfheader (abfd)->e_phnum = alloc;
4426 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
4427 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
4428 else
4429 BFD_ASSERT (elf_program_header_size (abfd)
4430 >= alloc * bed->s->sizeof_phdr);
4432 if (alloc == 0)
4434 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
4435 return TRUE;
4438 /* We're writing the size in elf_program_header_size (abfd),
4439 see assign_file_positions_except_relocs, so make sure we have
4440 that amount allocated, with trailing space cleared.
4441 The variable alloc contains the computed need, while
4442 elf_program_header_size (abfd) contains the size used for the
4443 layout.
4444 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4445 where the layout is forced to according to a larger size in the
4446 last iterations for the testcase ld-elf/header. */
4447 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
4448 == 0);
4449 phdrs = (Elf_Internal_Phdr *)
4450 bfd_zalloc2 (abfd,
4451 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
4452 sizeof (Elf_Internal_Phdr));
4453 elf_tdata (abfd)->phdr = phdrs;
4454 if (phdrs == NULL)
4455 return FALSE;
4457 maxpagesize = 1;
4458 if ((abfd->flags & D_PAGED) != 0)
4459 maxpagesize = bed->maxpagesize;
4461 off = bed->s->sizeof_ehdr;
4462 off += alloc * bed->s->sizeof_phdr;
4463 if (header_pad < (bfd_vma) off)
4464 header_pad = 0;
4465 else
4466 header_pad -= off;
4467 off += header_pad;
4469 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
4470 m != NULL;
4471 m = m->next, p++, j++)
4473 asection **secpp;
4474 bfd_vma off_adjust;
4475 bfd_boolean no_contents;
4477 /* If elf_segment_map is not from map_sections_to_segments, the
4478 sections may not be correctly ordered. NOTE: sorting should
4479 not be done to the PT_NOTE section of a corefile, which may
4480 contain several pseudo-sections artificially created by bfd.
4481 Sorting these pseudo-sections breaks things badly. */
4482 if (m->count > 1
4483 && !(elf_elfheader (abfd)->e_type == ET_CORE
4484 && m->p_type == PT_NOTE))
4485 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4486 elf_sort_sections);
4488 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4489 number of sections with contents contributing to both p_filesz
4490 and p_memsz, followed by a number of sections with no contents
4491 that just contribute to p_memsz. In this loop, OFF tracks next
4492 available file offset for PT_LOAD and PT_NOTE segments. */
4493 p->p_type = m->p_type;
4494 p->p_flags = m->p_flags;
4496 if (m->count == 0)
4497 p->p_vaddr = 0;
4498 else
4499 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4501 if (m->p_paddr_valid)
4502 p->p_paddr = m->p_paddr;
4503 else if (m->count == 0)
4504 p->p_paddr = 0;
4505 else
4506 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4508 if (p->p_type == PT_LOAD
4509 && (abfd->flags & D_PAGED) != 0)
4511 /* p_align in demand paged PT_LOAD segments effectively stores
4512 the maximum page size. When copying an executable with
4513 objcopy, we set m->p_align from the input file. Use this
4514 value for maxpagesize rather than bed->maxpagesize, which
4515 may be different. Note that we use maxpagesize for PT_TLS
4516 segment alignment later in this function, so we are relying
4517 on at least one PT_LOAD segment appearing before a PT_TLS
4518 segment. */
4519 if (m->p_align_valid)
4520 maxpagesize = m->p_align;
4522 p->p_align = maxpagesize;
4524 else if (m->p_align_valid)
4525 p->p_align = m->p_align;
4526 else if (m->count == 0)
4527 p->p_align = 1 << bed->s->log_file_align;
4528 else
4529 p->p_align = 0;
4531 no_contents = FALSE;
4532 off_adjust = 0;
4533 if (p->p_type == PT_LOAD
4534 && m->count > 0)
4536 bfd_size_type align;
4537 unsigned int align_power = 0;
4539 if (m->p_align_valid)
4540 align = p->p_align;
4541 else
4543 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4545 unsigned int secalign;
4547 secalign = bfd_get_section_alignment (abfd, *secpp);
4548 if (secalign > align_power)
4549 align_power = secalign;
4551 align = (bfd_size_type) 1 << align_power;
4552 if (align < maxpagesize)
4553 align = maxpagesize;
4556 for (i = 0; i < m->count; i++)
4557 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4558 /* If we aren't making room for this section, then
4559 it must be SHT_NOBITS regardless of what we've
4560 set via struct bfd_elf_special_section. */
4561 elf_section_type (m->sections[i]) = SHT_NOBITS;
4563 /* Find out whether this segment contains any loadable
4564 sections. */
4565 no_contents = TRUE;
4566 for (i = 0; i < m->count; i++)
4567 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4569 no_contents = FALSE;
4570 break;
4573 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4574 off += off_adjust;
4575 if (no_contents)
4577 /* We shouldn't need to align the segment on disk since
4578 the segment doesn't need file space, but the gABI
4579 arguably requires the alignment and glibc ld.so
4580 checks it. So to comply with the alignment
4581 requirement but not waste file space, we adjust
4582 p_offset for just this segment. (OFF_ADJUST is
4583 subtracted from OFF later.) This may put p_offset
4584 past the end of file, but that shouldn't matter. */
4586 else
4587 off_adjust = 0;
4589 /* Make sure the .dynamic section is the first section in the
4590 PT_DYNAMIC segment. */
4591 else if (p->p_type == PT_DYNAMIC
4592 && m->count > 1
4593 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4595 _bfd_error_handler
4596 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4597 abfd);
4598 bfd_set_error (bfd_error_bad_value);
4599 return FALSE;
4601 /* Set the note section type to SHT_NOTE. */
4602 else if (p->p_type == PT_NOTE)
4603 for (i = 0; i < m->count; i++)
4604 elf_section_type (m->sections[i]) = SHT_NOTE;
4606 p->p_offset = 0;
4607 p->p_filesz = 0;
4608 p->p_memsz = 0;
4610 if (m->includes_filehdr)
4612 if (!m->p_flags_valid)
4613 p->p_flags |= PF_R;
4614 p->p_filesz = bed->s->sizeof_ehdr;
4615 p->p_memsz = bed->s->sizeof_ehdr;
4616 if (m->count > 0)
4618 if (p->p_vaddr < (bfd_vma) off)
4620 (*_bfd_error_handler)
4621 (_("%B: Not enough room for program headers, try linking with -N"),
4622 abfd);
4623 bfd_set_error (bfd_error_bad_value);
4624 return FALSE;
4627 p->p_vaddr -= off;
4628 if (!m->p_paddr_valid)
4629 p->p_paddr -= off;
4633 if (m->includes_phdrs)
4635 if (!m->p_flags_valid)
4636 p->p_flags |= PF_R;
4638 if (!m->includes_filehdr)
4640 p->p_offset = bed->s->sizeof_ehdr;
4642 if (m->count > 0)
4644 p->p_vaddr -= off - p->p_offset;
4645 if (!m->p_paddr_valid)
4646 p->p_paddr -= off - p->p_offset;
4650 p->p_filesz += alloc * bed->s->sizeof_phdr;
4651 p->p_memsz += alloc * bed->s->sizeof_phdr;
4652 if (m->count)
4654 p->p_filesz += header_pad;
4655 p->p_memsz += header_pad;
4659 if (p->p_type == PT_LOAD
4660 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4662 if (!m->includes_filehdr && !m->includes_phdrs)
4663 p->p_offset = off;
4664 else
4666 file_ptr adjust;
4668 adjust = off - (p->p_offset + p->p_filesz);
4669 if (!no_contents)
4670 p->p_filesz += adjust;
4671 p->p_memsz += adjust;
4675 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4676 maps. Set filepos for sections in PT_LOAD segments, and in
4677 core files, for sections in PT_NOTE segments.
4678 assign_file_positions_for_non_load_sections will set filepos
4679 for other sections and update p_filesz for other segments. */
4680 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4682 asection *sec;
4683 bfd_size_type align;
4684 Elf_Internal_Shdr *this_hdr;
4686 sec = *secpp;
4687 this_hdr = &elf_section_data (sec)->this_hdr;
4688 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4690 if ((p->p_type == PT_LOAD
4691 || p->p_type == PT_TLS)
4692 && (this_hdr->sh_type != SHT_NOBITS
4693 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4694 && ((this_hdr->sh_flags & SHF_TLS) == 0
4695 || p->p_type == PT_TLS))))
4697 bfd_vma p_start = p->p_paddr;
4698 bfd_vma p_end = p_start + p->p_memsz;
4699 bfd_vma s_start = sec->lma;
4700 bfd_vma adjust = s_start - p_end;
4702 if (adjust != 0
4703 && (s_start < p_end
4704 || p_end < p_start))
4706 (*_bfd_error_handler)
4707 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4708 (unsigned long) s_start, (unsigned long) p_end);
4709 adjust = 0;
4710 sec->lma = p_end;
4712 p->p_memsz += adjust;
4714 if (this_hdr->sh_type != SHT_NOBITS)
4716 if (p->p_filesz + adjust < p->p_memsz)
4718 /* We have a PROGBITS section following NOBITS ones.
4719 Allocate file space for the NOBITS section(s) and
4720 zero it. */
4721 adjust = p->p_memsz - p->p_filesz;
4722 if (!write_zeros (abfd, off, adjust))
4723 return FALSE;
4725 off += adjust;
4726 p->p_filesz += adjust;
4730 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4732 /* The section at i == 0 is the one that actually contains
4733 everything. */
4734 if (i == 0)
4736 this_hdr->sh_offset = sec->filepos = off;
4737 off += this_hdr->sh_size;
4738 p->p_filesz = this_hdr->sh_size;
4739 p->p_memsz = 0;
4740 p->p_align = 1;
4742 else
4744 /* The rest are fake sections that shouldn't be written. */
4745 sec->filepos = 0;
4746 sec->size = 0;
4747 sec->flags = 0;
4748 continue;
4751 else
4753 if (p->p_type == PT_LOAD)
4755 this_hdr->sh_offset = sec->filepos = off;
4756 if (this_hdr->sh_type != SHT_NOBITS)
4757 off += this_hdr->sh_size;
4759 else if (this_hdr->sh_type == SHT_NOBITS
4760 && (this_hdr->sh_flags & SHF_TLS) != 0
4761 && this_hdr->sh_offset == 0)
4763 /* This is a .tbss section that didn't get a PT_LOAD.
4764 (See _bfd_elf_map_sections_to_segments "Create a
4765 final PT_LOAD".) Set sh_offset to the value it
4766 would have if we had created a zero p_filesz and
4767 p_memsz PT_LOAD header for the section. This
4768 also makes the PT_TLS header have the same
4769 p_offset value. */
4770 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
4771 off, align);
4772 this_hdr->sh_offset = sec->filepos = off + adjust;
4775 if (this_hdr->sh_type != SHT_NOBITS)
4777 p->p_filesz += this_hdr->sh_size;
4778 /* A load section without SHF_ALLOC is something like
4779 a note section in a PT_NOTE segment. These take
4780 file space but are not loaded into memory. */
4781 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4782 p->p_memsz += this_hdr->sh_size;
4784 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4786 if (p->p_type == PT_TLS)
4787 p->p_memsz += this_hdr->sh_size;
4789 /* .tbss is special. It doesn't contribute to p_memsz of
4790 normal segments. */
4791 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4792 p->p_memsz += this_hdr->sh_size;
4795 if (align > p->p_align
4796 && !m->p_align_valid
4797 && (p->p_type != PT_LOAD
4798 || (abfd->flags & D_PAGED) == 0))
4799 p->p_align = align;
4802 if (!m->p_flags_valid)
4804 p->p_flags |= PF_R;
4805 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4806 p->p_flags |= PF_X;
4807 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4808 p->p_flags |= PF_W;
4812 off -= off_adjust;
4814 /* Check that all sections are in a PT_LOAD segment.
4815 Don't check funky gdb generated core files. */
4816 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4818 bfd_boolean check_vma = TRUE;
4820 for (i = 1; i < m->count; i++)
4821 if (m->sections[i]->vma == m->sections[i - 1]->vma
4822 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4823 ->this_hdr), p) != 0
4824 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4825 ->this_hdr), p) != 0)
4827 /* Looks like we have overlays packed into the segment. */
4828 check_vma = FALSE;
4829 break;
4832 for (i = 0; i < m->count; i++)
4834 Elf_Internal_Shdr *this_hdr;
4835 asection *sec;
4837 sec = m->sections[i];
4838 this_hdr = &(elf_section_data(sec)->this_hdr);
4839 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
4840 && !ELF_TBSS_SPECIAL (this_hdr, p))
4842 (*_bfd_error_handler)
4843 (_("%B: section `%A' can't be allocated in segment %d"),
4844 abfd, sec, j);
4845 print_segment_map (m);
4851 elf_next_file_pos (abfd) = off;
4852 return TRUE;
4855 /* Assign file positions for the other sections. */
4857 static bfd_boolean
4858 assign_file_positions_for_non_load_sections (bfd *abfd,
4859 struct bfd_link_info *link_info)
4861 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4862 Elf_Internal_Shdr **i_shdrpp;
4863 Elf_Internal_Shdr **hdrpp;
4864 Elf_Internal_Phdr *phdrs;
4865 Elf_Internal_Phdr *p;
4866 struct elf_segment_map *m;
4867 struct elf_segment_map *hdrs_segment;
4868 bfd_vma filehdr_vaddr, filehdr_paddr;
4869 bfd_vma phdrs_vaddr, phdrs_paddr;
4870 file_ptr off;
4871 unsigned int num_sec;
4872 unsigned int i;
4873 unsigned int count;
4875 i_shdrpp = elf_elfsections (abfd);
4876 num_sec = elf_numsections (abfd);
4877 off = elf_next_file_pos (abfd);
4878 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4880 Elf_Internal_Shdr *hdr;
4882 hdr = *hdrpp;
4883 if (hdr->bfd_section != NULL
4884 && (hdr->bfd_section->filepos != 0
4885 || (hdr->sh_type == SHT_NOBITS
4886 && hdr->contents == NULL)))
4887 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4888 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4890 if (hdr->sh_size != 0)
4891 (*_bfd_error_handler)
4892 (_("%B: warning: allocated section `%s' not in segment"),
4893 abfd,
4894 (hdr->bfd_section == NULL
4895 ? "*unknown*"
4896 : hdr->bfd_section->name));
4897 /* We don't need to page align empty sections. */
4898 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4899 off += vma_page_aligned_bias (hdr->sh_addr, off,
4900 bed->maxpagesize);
4901 else
4902 off += vma_page_aligned_bias (hdr->sh_addr, off,
4903 hdr->sh_addralign);
4904 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4905 FALSE);
4907 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4908 && hdr->bfd_section == NULL)
4909 || hdr == i_shdrpp[elf_onesymtab (abfd)]
4910 || hdr == i_shdrpp[elf_symtab_shndx (abfd)]
4911 || hdr == i_shdrpp[elf_strtab_sec (abfd)])
4912 hdr->sh_offset = -1;
4913 else
4914 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4917 /* Now that we have set the section file positions, we can set up
4918 the file positions for the non PT_LOAD segments. */
4919 count = 0;
4920 filehdr_vaddr = 0;
4921 filehdr_paddr = 0;
4922 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4923 phdrs_paddr = 0;
4924 hdrs_segment = NULL;
4925 phdrs = elf_tdata (abfd)->phdr;
4926 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
4928 ++count;
4929 if (p->p_type != PT_LOAD)
4930 continue;
4932 if (m->includes_filehdr)
4934 filehdr_vaddr = p->p_vaddr;
4935 filehdr_paddr = p->p_paddr;
4937 if (m->includes_phdrs)
4939 phdrs_vaddr = p->p_vaddr;
4940 phdrs_paddr = p->p_paddr;
4941 if (m->includes_filehdr)
4943 hdrs_segment = m;
4944 phdrs_vaddr += bed->s->sizeof_ehdr;
4945 phdrs_paddr += bed->s->sizeof_ehdr;
4950 if (hdrs_segment != NULL && link_info != NULL)
4952 /* There is a segment that contains both the file headers and the
4953 program headers, so provide a symbol __ehdr_start pointing there.
4954 A program can use this to examine itself robustly. */
4956 struct elf_link_hash_entry *hash
4957 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
4958 FALSE, FALSE, TRUE);
4959 /* If the symbol was referenced and not defined, define it. */
4960 if (hash != NULL
4961 && (hash->root.type == bfd_link_hash_new
4962 || hash->root.type == bfd_link_hash_undefined
4963 || hash->root.type == bfd_link_hash_undefweak
4964 || hash->root.type == bfd_link_hash_common))
4966 asection *s = NULL;
4967 if (hdrs_segment->count != 0)
4968 /* The segment contains sections, so use the first one. */
4969 s = hdrs_segment->sections[0];
4970 else
4971 /* Use the first (i.e. lowest-addressed) section in any segment. */
4972 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4973 if (m->count != 0)
4975 s = m->sections[0];
4976 break;
4979 if (s != NULL)
4981 hash->root.u.def.value = filehdr_vaddr - s->vma;
4982 hash->root.u.def.section = s;
4984 else
4986 hash->root.u.def.value = filehdr_vaddr;
4987 hash->root.u.def.section = bfd_abs_section_ptr;
4990 hash->root.type = bfd_link_hash_defined;
4991 hash->def_regular = 1;
4992 hash->non_elf = 0;
4996 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
4998 if (p->p_type == PT_GNU_RELRO)
5000 const Elf_Internal_Phdr *lp;
5001 struct elf_segment_map *lm;
5003 if (link_info != NULL)
5005 /* During linking the range of the RELRO segment is passed
5006 in link_info. */
5007 for (lm = elf_seg_map (abfd), lp = phdrs;
5008 lm != NULL;
5009 lm = lm->next, lp++)
5011 if (lp->p_type == PT_LOAD
5012 && lp->p_vaddr < link_info->relro_end
5013 && lm->count != 0
5014 && lm->sections[0]->vma >= link_info->relro_start)
5015 break;
5018 BFD_ASSERT (lm != NULL);
5020 else
5022 /* Otherwise we are copying an executable or shared
5023 library, but we need to use the same linker logic. */
5024 for (lp = phdrs; lp < phdrs + count; ++lp)
5026 if (lp->p_type == PT_LOAD
5027 && lp->p_paddr == p->p_paddr)
5028 break;
5032 if (lp < phdrs + count)
5034 p->p_vaddr = lp->p_vaddr;
5035 p->p_paddr = lp->p_paddr;
5036 p->p_offset = lp->p_offset;
5037 if (link_info != NULL)
5038 p->p_filesz = link_info->relro_end - lp->p_vaddr;
5039 else if (m->p_size_valid)
5040 p->p_filesz = m->p_size;
5041 else
5042 abort ();
5043 p->p_memsz = p->p_filesz;
5044 /* Preserve the alignment and flags if they are valid. The
5045 gold linker generates RW/4 for the PT_GNU_RELRO section.
5046 It is better for objcopy/strip to honor these attributes
5047 otherwise gdb will choke when using separate debug files.
5049 if (!m->p_align_valid)
5050 p->p_align = 1;
5051 if (!m->p_flags_valid)
5052 p->p_flags = (lp->p_flags & ~PF_W);
5054 else
5056 memset (p, 0, sizeof *p);
5057 p->p_type = PT_NULL;
5060 else if (p->p_type == PT_GNU_STACK)
5062 if (m->p_size_valid)
5063 p->p_memsz = m->p_size;
5065 else if (m->count != 0)
5067 if (p->p_type != PT_LOAD
5068 && (p->p_type != PT_NOTE
5069 || bfd_get_format (abfd) != bfd_core))
5071 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
5073 p->p_filesz = 0;
5074 p->p_offset = m->sections[0]->filepos;
5075 for (i = m->count; i-- != 0;)
5077 asection *sect = m->sections[i];
5078 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5079 if (hdr->sh_type != SHT_NOBITS)
5081 p->p_filesz = (sect->filepos - m->sections[0]->filepos
5082 + hdr->sh_size);
5083 break;
5088 else if (m->includes_filehdr)
5090 p->p_vaddr = filehdr_vaddr;
5091 if (! m->p_paddr_valid)
5092 p->p_paddr = filehdr_paddr;
5094 else if (m->includes_phdrs)
5096 p->p_vaddr = phdrs_vaddr;
5097 if (! m->p_paddr_valid)
5098 p->p_paddr = phdrs_paddr;
5102 elf_next_file_pos (abfd) = off;
5104 return TRUE;
5107 /* Work out the file positions of all the sections. This is called by
5108 _bfd_elf_compute_section_file_positions. All the section sizes and
5109 VMAs must be known before this is called.
5111 Reloc sections come in two flavours: Those processed specially as
5112 "side-channel" data attached to a section to which they apply, and
5113 those that bfd doesn't process as relocations. The latter sort are
5114 stored in a normal bfd section by bfd_section_from_shdr. We don't
5115 consider the former sort here, unless they form part of the loadable
5116 image. Reloc sections not assigned here will be handled later by
5117 assign_file_positions_for_relocs.
5119 We also don't set the positions of the .symtab and .strtab here. */
5121 static bfd_boolean
5122 assign_file_positions_except_relocs (bfd *abfd,
5123 struct bfd_link_info *link_info)
5125 struct elf_obj_tdata *tdata = elf_tdata (abfd);
5126 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5127 file_ptr off;
5128 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5130 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5131 && bfd_get_format (abfd) != bfd_core)
5133 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5134 unsigned int num_sec = elf_numsections (abfd);
5135 Elf_Internal_Shdr **hdrpp;
5136 unsigned int i;
5138 /* Start after the ELF header. */
5139 off = i_ehdrp->e_ehsize;
5141 /* We are not creating an executable, which means that we are
5142 not creating a program header, and that the actual order of
5143 the sections in the file is unimportant. */
5144 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5146 Elf_Internal_Shdr *hdr;
5148 hdr = *hdrpp;
5149 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5150 && hdr->bfd_section == NULL)
5151 || i == elf_onesymtab (abfd)
5152 || i == elf_symtab_shndx (abfd)
5153 || i == elf_strtab_sec (abfd))
5155 hdr->sh_offset = -1;
5157 else
5158 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5161 else
5163 unsigned int alloc;
5165 /* Assign file positions for the loaded sections based on the
5166 assignment of sections to segments. */
5167 if (!assign_file_positions_for_load_sections (abfd, link_info))
5168 return FALSE;
5170 /* And for non-load sections. */
5171 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
5172 return FALSE;
5174 if (bed->elf_backend_modify_program_headers != NULL)
5176 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
5177 return FALSE;
5180 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5181 if (link_info != NULL
5182 && link_info->executable
5183 && link_info->shared)
5185 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
5186 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
5187 Elf_Internal_Phdr *end_segment = &segment[num_segments];
5189 /* Find the lowest p_vaddr in PT_LOAD segments. */
5190 bfd_vma p_vaddr = (bfd_vma) -1;
5191 for (; segment < end_segment; segment++)
5192 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
5193 p_vaddr = segment->p_vaddr;
5195 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5196 segments is non-zero. */
5197 if (p_vaddr)
5198 i_ehdrp->e_type = ET_EXEC;
5201 /* Write out the program headers. */
5202 alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5203 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
5204 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
5205 return FALSE;
5207 off = elf_next_file_pos (abfd);
5210 /* Place the section headers. */
5211 off = align_file_position (off, 1 << bed->s->log_file_align);
5212 i_ehdrp->e_shoff = off;
5213 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
5215 elf_next_file_pos (abfd) = off;
5217 return TRUE;
5220 static bfd_boolean
5221 prep_headers (bfd *abfd)
5223 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
5224 struct elf_strtab_hash *shstrtab;
5225 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5227 i_ehdrp = elf_elfheader (abfd);
5229 shstrtab = _bfd_elf_strtab_init ();
5230 if (shstrtab == NULL)
5231 return FALSE;
5233 elf_shstrtab (abfd) = shstrtab;
5235 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5236 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5237 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5238 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5240 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5241 i_ehdrp->e_ident[EI_DATA] =
5242 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5243 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5245 if ((abfd->flags & DYNAMIC) != 0)
5246 i_ehdrp->e_type = ET_DYN;
5247 else if ((abfd->flags & EXEC_P) != 0)
5248 i_ehdrp->e_type = ET_EXEC;
5249 else if (bfd_get_format (abfd) == bfd_core)
5250 i_ehdrp->e_type = ET_CORE;
5251 else
5252 i_ehdrp->e_type = ET_REL;
5254 switch (bfd_get_arch (abfd))
5256 case bfd_arch_unknown:
5257 i_ehdrp->e_machine = EM_NONE;
5258 break;
5260 /* There used to be a long list of cases here, each one setting
5261 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5262 in the corresponding bfd definition. To avoid duplication,
5263 the switch was removed. Machines that need special handling
5264 can generally do it in elf_backend_final_write_processing(),
5265 unless they need the information earlier than the final write.
5266 Such need can generally be supplied by replacing the tests for
5267 e_machine with the conditions used to determine it. */
5268 default:
5269 i_ehdrp->e_machine = bed->elf_machine_code;
5272 i_ehdrp->e_version = bed->s->ev_current;
5273 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5275 /* No program header, for now. */
5276 i_ehdrp->e_phoff = 0;
5277 i_ehdrp->e_phentsize = 0;
5278 i_ehdrp->e_phnum = 0;
5280 /* Each bfd section is section header entry. */
5281 i_ehdrp->e_entry = bfd_get_start_address (abfd);
5282 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5284 /* If we're building an executable, we'll need a program header table. */
5285 if (abfd->flags & EXEC_P)
5286 /* It all happens later. */
5288 else
5290 i_ehdrp->e_phentsize = 0;
5291 i_ehdrp->e_phoff = 0;
5294 elf_tdata (abfd)->symtab_hdr.sh_name =
5295 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5296 elf_tdata (abfd)->strtab_hdr.sh_name =
5297 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5298 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5299 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5300 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5301 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5302 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5303 return FALSE;
5305 return TRUE;
5308 /* Assign file positions for all the reloc sections which are not part
5309 of the loadable file image. */
5311 void
5312 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
5314 file_ptr off;
5315 unsigned int i, num_sec;
5316 Elf_Internal_Shdr **shdrpp;
5318 off = elf_next_file_pos (abfd);
5320 num_sec = elf_numsections (abfd);
5321 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5323 Elf_Internal_Shdr *shdrp;
5325 shdrp = *shdrpp;
5326 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5327 && shdrp->sh_offset == -1)
5328 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5331 elf_next_file_pos (abfd) = off;
5334 bfd_boolean
5335 _bfd_elf_write_object_contents (bfd *abfd)
5337 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5338 Elf_Internal_Shdr **i_shdrp;
5339 bfd_boolean failed;
5340 unsigned int count, num_sec;
5341 struct elf_obj_tdata *t;
5343 if (! abfd->output_has_begun
5344 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5345 return FALSE;
5347 i_shdrp = elf_elfsections (abfd);
5349 failed = FALSE;
5350 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5351 if (failed)
5352 return FALSE;
5354 _bfd_elf_assign_file_positions_for_relocs (abfd);
5356 /* After writing the headers, we need to write the sections too... */
5357 num_sec = elf_numsections (abfd);
5358 for (count = 1; count < num_sec; count++)
5360 if (bed->elf_backend_section_processing)
5361 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5362 if (i_shdrp[count]->contents)
5364 bfd_size_type amt = i_shdrp[count]->sh_size;
5366 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5367 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5368 return FALSE;
5372 /* Write out the section header names. */
5373 t = elf_tdata (abfd);
5374 if (elf_shstrtab (abfd) != NULL
5375 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5376 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5377 return FALSE;
5379 if (bed->elf_backend_final_write_processing)
5380 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
5382 if (!bed->s->write_shdrs_and_ehdr (abfd))
5383 return FALSE;
5385 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5386 if (t->o->build_id.after_write_object_contents != NULL)
5387 return (*t->o->build_id.after_write_object_contents) (abfd);
5389 return TRUE;
5392 bfd_boolean
5393 _bfd_elf_write_corefile_contents (bfd *abfd)
5395 /* Hopefully this can be done just like an object file. */
5396 return _bfd_elf_write_object_contents (abfd);
5399 /* Given a section, search the header to find them. */
5401 unsigned int
5402 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5404 const struct elf_backend_data *bed;
5405 unsigned int sec_index;
5407 if (elf_section_data (asect) != NULL
5408 && elf_section_data (asect)->this_idx != 0)
5409 return elf_section_data (asect)->this_idx;
5411 if (bfd_is_abs_section (asect))
5412 sec_index = SHN_ABS;
5413 else if (bfd_is_com_section (asect))
5414 sec_index = SHN_COMMON;
5415 else if (bfd_is_und_section (asect))
5416 sec_index = SHN_UNDEF;
5417 else
5418 sec_index = SHN_BAD;
5420 bed = get_elf_backend_data (abfd);
5421 if (bed->elf_backend_section_from_bfd_section)
5423 int retval = sec_index;
5425 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5426 return retval;
5429 if (sec_index == SHN_BAD)
5430 bfd_set_error (bfd_error_nonrepresentable_section);
5432 return sec_index;
5435 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5436 on error. */
5439 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5441 asymbol *asym_ptr = *asym_ptr_ptr;
5442 int idx;
5443 flagword flags = asym_ptr->flags;
5445 /* When gas creates relocations against local labels, it creates its
5446 own symbol for the section, but does put the symbol into the
5447 symbol chain, so udata is 0. When the linker is generating
5448 relocatable output, this section symbol may be for one of the
5449 input sections rather than the output section. */
5450 if (asym_ptr->udata.i == 0
5451 && (flags & BSF_SECTION_SYM)
5452 && asym_ptr->section)
5454 asection *sec;
5455 int indx;
5457 sec = asym_ptr->section;
5458 if (sec->owner != abfd && sec->output_section != NULL)
5459 sec = sec->output_section;
5460 if (sec->owner == abfd
5461 && (indx = sec->index) < elf_num_section_syms (abfd)
5462 && elf_section_syms (abfd)[indx] != NULL)
5463 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5466 idx = asym_ptr->udata.i;
5468 if (idx == 0)
5470 /* This case can occur when using --strip-symbol on a symbol
5471 which is used in a relocation entry. */
5472 (*_bfd_error_handler)
5473 (_("%B: symbol `%s' required but not present"),
5474 abfd, bfd_asymbol_name (asym_ptr));
5475 bfd_set_error (bfd_error_no_symbols);
5476 return -1;
5479 #if DEBUG & 4
5481 fprintf (stderr,
5482 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5483 (long) asym_ptr, asym_ptr->name, idx, (long) flags);
5484 fflush (stderr);
5486 #endif
5488 return idx;
5491 /* Rewrite program header information. */
5493 static bfd_boolean
5494 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5496 Elf_Internal_Ehdr *iehdr;
5497 struct elf_segment_map *map;
5498 struct elf_segment_map *map_first;
5499 struct elf_segment_map **pointer_to_map;
5500 Elf_Internal_Phdr *segment;
5501 asection *section;
5502 unsigned int i;
5503 unsigned int num_segments;
5504 bfd_boolean phdr_included = FALSE;
5505 bfd_boolean p_paddr_valid;
5506 bfd_vma maxpagesize;
5507 struct elf_segment_map *phdr_adjust_seg = NULL;
5508 unsigned int phdr_adjust_num = 0;
5509 const struct elf_backend_data *bed;
5511 bed = get_elf_backend_data (ibfd);
5512 iehdr = elf_elfheader (ibfd);
5514 map_first = NULL;
5515 pointer_to_map = &map_first;
5517 num_segments = elf_elfheader (ibfd)->e_phnum;
5518 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5520 /* Returns the end address of the segment + 1. */
5521 #define SEGMENT_END(segment, start) \
5522 (start + (segment->p_memsz > segment->p_filesz \
5523 ? segment->p_memsz : segment->p_filesz))
5525 #define SECTION_SIZE(section, segment) \
5526 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5527 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5528 ? section->size : 0)
5530 /* Returns TRUE if the given section is contained within
5531 the given segment. VMA addresses are compared. */
5532 #define IS_CONTAINED_BY_VMA(section, segment) \
5533 (section->vma >= segment->p_vaddr \
5534 && (section->vma + SECTION_SIZE (section, segment) \
5535 <= (SEGMENT_END (segment, segment->p_vaddr))))
5537 /* Returns TRUE if the given section is contained within
5538 the given segment. LMA addresses are compared. */
5539 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5540 (section->lma >= base \
5541 && (section->lma + SECTION_SIZE (section, segment) \
5542 <= SEGMENT_END (segment, base)))
5544 /* Handle PT_NOTE segment. */
5545 #define IS_NOTE(p, s) \
5546 (p->p_type == PT_NOTE \
5547 && elf_section_type (s) == SHT_NOTE \
5548 && (bfd_vma) s->filepos >= p->p_offset \
5549 && ((bfd_vma) s->filepos + s->size \
5550 <= p->p_offset + p->p_filesz))
5552 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5553 etc. */
5554 #define IS_COREFILE_NOTE(p, s) \
5555 (IS_NOTE (p, s) \
5556 && bfd_get_format (ibfd) == bfd_core \
5557 && s->vma == 0 \
5558 && s->lma == 0)
5560 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5561 linker, which generates a PT_INTERP section with p_vaddr and
5562 p_memsz set to 0. */
5563 #define IS_SOLARIS_PT_INTERP(p, s) \
5564 (p->p_vaddr == 0 \
5565 && p->p_paddr == 0 \
5566 && p->p_memsz == 0 \
5567 && p->p_filesz > 0 \
5568 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5569 && s->size > 0 \
5570 && (bfd_vma) s->filepos >= p->p_offset \
5571 && ((bfd_vma) s->filepos + s->size \
5572 <= p->p_offset + p->p_filesz))
5574 /* Decide if the given section should be included in the given segment.
5575 A section will be included if:
5576 1. It is within the address space of the segment -- we use the LMA
5577 if that is set for the segment and the VMA otherwise,
5578 2. It is an allocated section or a NOTE section in a PT_NOTE
5579 segment.
5580 3. There is an output section associated with it,
5581 4. The section has not already been allocated to a previous segment.
5582 5. PT_GNU_STACK segments do not include any sections.
5583 6. PT_TLS segment includes only SHF_TLS sections.
5584 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5585 8. PT_DYNAMIC should not contain empty sections at the beginning
5586 (with the possible exception of .dynamic). */
5587 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5588 ((((segment->p_paddr \
5589 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5590 : IS_CONTAINED_BY_VMA (section, segment)) \
5591 && (section->flags & SEC_ALLOC) != 0) \
5592 || IS_NOTE (segment, section)) \
5593 && segment->p_type != PT_GNU_STACK \
5594 && (segment->p_type != PT_TLS \
5595 || (section->flags & SEC_THREAD_LOCAL)) \
5596 && (segment->p_type == PT_LOAD \
5597 || segment->p_type == PT_TLS \
5598 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5599 && (segment->p_type != PT_DYNAMIC \
5600 || SECTION_SIZE (section, segment) > 0 \
5601 || (segment->p_paddr \
5602 ? segment->p_paddr != section->lma \
5603 : segment->p_vaddr != section->vma) \
5604 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5605 == 0)) \
5606 && !section->segment_mark)
5608 /* If the output section of a section in the input segment is NULL,
5609 it is removed from the corresponding output segment. */
5610 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5611 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5612 && section->output_section != NULL)
5614 /* Returns TRUE iff seg1 starts after the end of seg2. */
5615 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5616 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5618 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5619 their VMA address ranges and their LMA address ranges overlap.
5620 It is possible to have overlapping VMA ranges without overlapping LMA
5621 ranges. RedBoot images for example can have both .data and .bss mapped
5622 to the same VMA range, but with the .data section mapped to a different
5623 LMA. */
5624 #define SEGMENT_OVERLAPS(seg1, seg2) \
5625 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5626 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5627 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5628 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5630 /* Initialise the segment mark field. */
5631 for (section = ibfd->sections; section != NULL; section = section->next)
5632 section->segment_mark = FALSE;
5634 /* The Solaris linker creates program headers in which all the
5635 p_paddr fields are zero. When we try to objcopy or strip such a
5636 file, we get confused. Check for this case, and if we find it
5637 don't set the p_paddr_valid fields. */
5638 p_paddr_valid = FALSE;
5639 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5640 i < num_segments;
5641 i++, segment++)
5642 if (segment->p_paddr != 0)
5644 p_paddr_valid = TRUE;
5645 break;
5648 /* Scan through the segments specified in the program header
5649 of the input BFD. For this first scan we look for overlaps
5650 in the loadable segments. These can be created by weird
5651 parameters to objcopy. Also, fix some solaris weirdness. */
5652 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5653 i < num_segments;
5654 i++, segment++)
5656 unsigned int j;
5657 Elf_Internal_Phdr *segment2;
5659 if (segment->p_type == PT_INTERP)
5660 for (section = ibfd->sections; section; section = section->next)
5661 if (IS_SOLARIS_PT_INTERP (segment, section))
5663 /* Mininal change so that the normal section to segment
5664 assignment code will work. */
5665 segment->p_vaddr = section->vma;
5666 break;
5669 if (segment->p_type != PT_LOAD)
5671 /* Remove PT_GNU_RELRO segment. */
5672 if (segment->p_type == PT_GNU_RELRO)
5673 segment->p_type = PT_NULL;
5674 continue;
5677 /* Determine if this segment overlaps any previous segments. */
5678 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5680 bfd_signed_vma extra_length;
5682 if (segment2->p_type != PT_LOAD
5683 || !SEGMENT_OVERLAPS (segment, segment2))
5684 continue;
5686 /* Merge the two segments together. */
5687 if (segment2->p_vaddr < segment->p_vaddr)
5689 /* Extend SEGMENT2 to include SEGMENT and then delete
5690 SEGMENT. */
5691 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5692 - SEGMENT_END (segment2, segment2->p_vaddr));
5694 if (extra_length > 0)
5696 segment2->p_memsz += extra_length;
5697 segment2->p_filesz += extra_length;
5700 segment->p_type = PT_NULL;
5702 /* Since we have deleted P we must restart the outer loop. */
5703 i = 0;
5704 segment = elf_tdata (ibfd)->phdr;
5705 break;
5707 else
5709 /* Extend SEGMENT to include SEGMENT2 and then delete
5710 SEGMENT2. */
5711 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5712 - SEGMENT_END (segment, segment->p_vaddr));
5714 if (extra_length > 0)
5716 segment->p_memsz += extra_length;
5717 segment->p_filesz += extra_length;
5720 segment2->p_type = PT_NULL;
5725 /* The second scan attempts to assign sections to segments. */
5726 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5727 i < num_segments;
5728 i++, segment++)
5730 unsigned int section_count;
5731 asection **sections;
5732 asection *output_section;
5733 unsigned int isec;
5734 bfd_vma matching_lma;
5735 bfd_vma suggested_lma;
5736 unsigned int j;
5737 bfd_size_type amt;
5738 asection *first_section;
5739 bfd_boolean first_matching_lma;
5740 bfd_boolean first_suggested_lma;
5742 if (segment->p_type == PT_NULL)
5743 continue;
5745 first_section = NULL;
5746 /* Compute how many sections might be placed into this segment. */
5747 for (section = ibfd->sections, section_count = 0;
5748 section != NULL;
5749 section = section->next)
5751 /* Find the first section in the input segment, which may be
5752 removed from the corresponding output segment. */
5753 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5755 if (first_section == NULL)
5756 first_section = section;
5757 if (section->output_section != NULL)
5758 ++section_count;
5762 /* Allocate a segment map big enough to contain
5763 all of the sections we have selected. */
5764 amt = sizeof (struct elf_segment_map);
5765 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5766 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5767 if (map == NULL)
5768 return FALSE;
5770 /* Initialise the fields of the segment map. Default to
5771 using the physical address of the segment in the input BFD. */
5772 map->next = NULL;
5773 map->p_type = segment->p_type;
5774 map->p_flags = segment->p_flags;
5775 map->p_flags_valid = 1;
5777 /* If the first section in the input segment is removed, there is
5778 no need to preserve segment physical address in the corresponding
5779 output segment. */
5780 if (!first_section || first_section->output_section != NULL)
5782 map->p_paddr = segment->p_paddr;
5783 map->p_paddr_valid = p_paddr_valid;
5786 /* Determine if this segment contains the ELF file header
5787 and if it contains the program headers themselves. */
5788 map->includes_filehdr = (segment->p_offset == 0
5789 && segment->p_filesz >= iehdr->e_ehsize);
5790 map->includes_phdrs = 0;
5792 if (!phdr_included || segment->p_type != PT_LOAD)
5794 map->includes_phdrs =
5795 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5796 && (segment->p_offset + segment->p_filesz
5797 >= ((bfd_vma) iehdr->e_phoff
5798 + iehdr->e_phnum * iehdr->e_phentsize)));
5800 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5801 phdr_included = TRUE;
5804 if (section_count == 0)
5806 /* Special segments, such as the PT_PHDR segment, may contain
5807 no sections, but ordinary, loadable segments should contain
5808 something. They are allowed by the ELF spec however, so only
5809 a warning is produced. */
5810 if (segment->p_type == PT_LOAD)
5811 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5812 " detected, is this intentional ?\n"),
5813 ibfd);
5815 map->count = 0;
5816 *pointer_to_map = map;
5817 pointer_to_map = &map->next;
5819 continue;
5822 /* Now scan the sections in the input BFD again and attempt
5823 to add their corresponding output sections to the segment map.
5824 The problem here is how to handle an output section which has
5825 been moved (ie had its LMA changed). There are four possibilities:
5827 1. None of the sections have been moved.
5828 In this case we can continue to use the segment LMA from the
5829 input BFD.
5831 2. All of the sections have been moved by the same amount.
5832 In this case we can change the segment's LMA to match the LMA
5833 of the first section.
5835 3. Some of the sections have been moved, others have not.
5836 In this case those sections which have not been moved can be
5837 placed in the current segment which will have to have its size,
5838 and possibly its LMA changed, and a new segment or segments will
5839 have to be created to contain the other sections.
5841 4. The sections have been moved, but not by the same amount.
5842 In this case we can change the segment's LMA to match the LMA
5843 of the first section and we will have to create a new segment
5844 or segments to contain the other sections.
5846 In order to save time, we allocate an array to hold the section
5847 pointers that we are interested in. As these sections get assigned
5848 to a segment, they are removed from this array. */
5850 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5851 if (sections == NULL)
5852 return FALSE;
5854 /* Step One: Scan for segment vs section LMA conflicts.
5855 Also add the sections to the section array allocated above.
5856 Also add the sections to the current segment. In the common
5857 case, where the sections have not been moved, this means that
5858 we have completely filled the segment, and there is nothing
5859 more to do. */
5860 isec = 0;
5861 matching_lma = 0;
5862 suggested_lma = 0;
5863 first_matching_lma = TRUE;
5864 first_suggested_lma = TRUE;
5866 for (section = ibfd->sections;
5867 section != NULL;
5868 section = section->next)
5869 if (section == first_section)
5870 break;
5872 for (j = 0; section != NULL; section = section->next)
5874 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5876 output_section = section->output_section;
5878 sections[j++] = section;
5880 /* The Solaris native linker always sets p_paddr to 0.
5881 We try to catch that case here, and set it to the
5882 correct value. Note - some backends require that
5883 p_paddr be left as zero. */
5884 if (!p_paddr_valid
5885 && segment->p_vaddr != 0
5886 && !bed->want_p_paddr_set_to_zero
5887 && isec == 0
5888 && output_section->lma != 0
5889 && output_section->vma == (segment->p_vaddr
5890 + (map->includes_filehdr
5891 ? iehdr->e_ehsize
5892 : 0)
5893 + (map->includes_phdrs
5894 ? (iehdr->e_phnum
5895 * iehdr->e_phentsize)
5896 : 0)))
5897 map->p_paddr = segment->p_vaddr;
5899 /* Match up the physical address of the segment with the
5900 LMA address of the output section. */
5901 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5902 || IS_COREFILE_NOTE (segment, section)
5903 || (bed->want_p_paddr_set_to_zero
5904 && IS_CONTAINED_BY_VMA (output_section, segment)))
5906 if (first_matching_lma || output_section->lma < matching_lma)
5908 matching_lma = output_section->lma;
5909 first_matching_lma = FALSE;
5912 /* We assume that if the section fits within the segment
5913 then it does not overlap any other section within that
5914 segment. */
5915 map->sections[isec++] = output_section;
5917 else if (first_suggested_lma)
5919 suggested_lma = output_section->lma;
5920 first_suggested_lma = FALSE;
5923 if (j == section_count)
5924 break;
5928 BFD_ASSERT (j == section_count);
5930 /* Step Two: Adjust the physical address of the current segment,
5931 if necessary. */
5932 if (isec == section_count)
5934 /* All of the sections fitted within the segment as currently
5935 specified. This is the default case. Add the segment to
5936 the list of built segments and carry on to process the next
5937 program header in the input BFD. */
5938 map->count = section_count;
5939 *pointer_to_map = map;
5940 pointer_to_map = &map->next;
5942 if (p_paddr_valid
5943 && !bed->want_p_paddr_set_to_zero
5944 && matching_lma != map->p_paddr
5945 && !map->includes_filehdr
5946 && !map->includes_phdrs)
5947 /* There is some padding before the first section in the
5948 segment. So, we must account for that in the output
5949 segment's vma. */
5950 map->p_vaddr_offset = matching_lma - map->p_paddr;
5952 free (sections);
5953 continue;
5955 else
5957 if (!first_matching_lma)
5959 /* At least one section fits inside the current segment.
5960 Keep it, but modify its physical address to match the
5961 LMA of the first section that fitted. */
5962 map->p_paddr = matching_lma;
5964 else
5966 /* None of the sections fitted inside the current segment.
5967 Change the current segment's physical address to match
5968 the LMA of the first section. */
5969 map->p_paddr = suggested_lma;
5972 /* Offset the segment physical address from the lma
5973 to allow for space taken up by elf headers. */
5974 if (map->includes_filehdr)
5976 if (map->p_paddr >= iehdr->e_ehsize)
5977 map->p_paddr -= iehdr->e_ehsize;
5978 else
5980 map->includes_filehdr = FALSE;
5981 map->includes_phdrs = FALSE;
5985 if (map->includes_phdrs)
5987 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5989 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5991 /* iehdr->e_phnum is just an estimate of the number
5992 of program headers that we will need. Make a note
5993 here of the number we used and the segment we chose
5994 to hold these headers, so that we can adjust the
5995 offset when we know the correct value. */
5996 phdr_adjust_num = iehdr->e_phnum;
5997 phdr_adjust_seg = map;
5999 else
6000 map->includes_phdrs = FALSE;
6004 /* Step Three: Loop over the sections again, this time assigning
6005 those that fit to the current segment and removing them from the
6006 sections array; but making sure not to leave large gaps. Once all
6007 possible sections have been assigned to the current segment it is
6008 added to the list of built segments and if sections still remain
6009 to be assigned, a new segment is constructed before repeating
6010 the loop. */
6011 isec = 0;
6014 map->count = 0;
6015 suggested_lma = 0;
6016 first_suggested_lma = TRUE;
6018 /* Fill the current segment with sections that fit. */
6019 for (j = 0; j < section_count; j++)
6021 section = sections[j];
6023 if (section == NULL)
6024 continue;
6026 output_section = section->output_section;
6028 BFD_ASSERT (output_section != NULL);
6030 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6031 || IS_COREFILE_NOTE (segment, section))
6033 if (map->count == 0)
6035 /* If the first section in a segment does not start at
6036 the beginning of the segment, then something is
6037 wrong. */
6038 if (output_section->lma
6039 != (map->p_paddr
6040 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
6041 + (map->includes_phdrs
6042 ? iehdr->e_phnum * iehdr->e_phentsize
6043 : 0)))
6044 abort ();
6046 else
6048 asection *prev_sec;
6050 prev_sec = map->sections[map->count - 1];
6052 /* If the gap between the end of the previous section
6053 and the start of this section is more than
6054 maxpagesize then we need to start a new segment. */
6055 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
6056 maxpagesize)
6057 < BFD_ALIGN (output_section->lma, maxpagesize))
6058 || (prev_sec->lma + prev_sec->size
6059 > output_section->lma))
6061 if (first_suggested_lma)
6063 suggested_lma = output_section->lma;
6064 first_suggested_lma = FALSE;
6067 continue;
6071 map->sections[map->count++] = output_section;
6072 ++isec;
6073 sections[j] = NULL;
6074 section->segment_mark = TRUE;
6076 else if (first_suggested_lma)
6078 suggested_lma = output_section->lma;
6079 first_suggested_lma = FALSE;
6083 BFD_ASSERT (map->count > 0);
6085 /* Add the current segment to the list of built segments. */
6086 *pointer_to_map = map;
6087 pointer_to_map = &map->next;
6089 if (isec < section_count)
6091 /* We still have not allocated all of the sections to
6092 segments. Create a new segment here, initialise it
6093 and carry on looping. */
6094 amt = sizeof (struct elf_segment_map);
6095 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6096 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6097 if (map == NULL)
6099 free (sections);
6100 return FALSE;
6103 /* Initialise the fields of the segment map. Set the physical
6104 physical address to the LMA of the first section that has
6105 not yet been assigned. */
6106 map->next = NULL;
6107 map->p_type = segment->p_type;
6108 map->p_flags = segment->p_flags;
6109 map->p_flags_valid = 1;
6110 map->p_paddr = suggested_lma;
6111 map->p_paddr_valid = p_paddr_valid;
6112 map->includes_filehdr = 0;
6113 map->includes_phdrs = 0;
6116 while (isec < section_count);
6118 free (sections);
6121 elf_seg_map (obfd) = map_first;
6123 /* If we had to estimate the number of program headers that were
6124 going to be needed, then check our estimate now and adjust
6125 the offset if necessary. */
6126 if (phdr_adjust_seg != NULL)
6128 unsigned int count;
6130 for (count = 0, map = map_first; map != NULL; map = map->next)
6131 count++;
6133 if (count > phdr_adjust_num)
6134 phdr_adjust_seg->p_paddr
6135 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
6138 #undef SEGMENT_END
6139 #undef SECTION_SIZE
6140 #undef IS_CONTAINED_BY_VMA
6141 #undef IS_CONTAINED_BY_LMA
6142 #undef IS_NOTE
6143 #undef IS_COREFILE_NOTE
6144 #undef IS_SOLARIS_PT_INTERP
6145 #undef IS_SECTION_IN_INPUT_SEGMENT
6146 #undef INCLUDE_SECTION_IN_SEGMENT
6147 #undef SEGMENT_AFTER_SEGMENT
6148 #undef SEGMENT_OVERLAPS
6149 return TRUE;
6152 /* Copy ELF program header information. */
6154 static bfd_boolean
6155 copy_elf_program_header (bfd *ibfd, bfd *obfd)
6157 Elf_Internal_Ehdr *iehdr;
6158 struct elf_segment_map *map;
6159 struct elf_segment_map *map_first;
6160 struct elf_segment_map **pointer_to_map;
6161 Elf_Internal_Phdr *segment;
6162 unsigned int i;
6163 unsigned int num_segments;
6164 bfd_boolean phdr_included = FALSE;
6165 bfd_boolean p_paddr_valid;
6167 iehdr = elf_elfheader (ibfd);
6169 map_first = NULL;
6170 pointer_to_map = &map_first;
6172 /* If all the segment p_paddr fields are zero, don't set
6173 map->p_paddr_valid. */
6174 p_paddr_valid = FALSE;
6175 num_segments = elf_elfheader (ibfd)->e_phnum;
6176 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6177 i < num_segments;
6178 i++, segment++)
6179 if (segment->p_paddr != 0)
6181 p_paddr_valid = TRUE;
6182 break;
6185 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6186 i < num_segments;
6187 i++, segment++)
6189 asection *section;
6190 unsigned int section_count;
6191 bfd_size_type amt;
6192 Elf_Internal_Shdr *this_hdr;
6193 asection *first_section = NULL;
6194 asection *lowest_section;
6196 /* Compute how many sections are in this segment. */
6197 for (section = ibfd->sections, section_count = 0;
6198 section != NULL;
6199 section = section->next)
6201 this_hdr = &(elf_section_data(section)->this_hdr);
6202 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6204 if (first_section == NULL)
6205 first_section = section;
6206 section_count++;
6210 /* Allocate a segment map big enough to contain
6211 all of the sections we have selected. */
6212 amt = sizeof (struct elf_segment_map);
6213 if (section_count != 0)
6214 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6215 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6216 if (map == NULL)
6217 return FALSE;
6219 /* Initialize the fields of the output segment map with the
6220 input segment. */
6221 map->next = NULL;
6222 map->p_type = segment->p_type;
6223 map->p_flags = segment->p_flags;
6224 map->p_flags_valid = 1;
6225 map->p_paddr = segment->p_paddr;
6226 map->p_paddr_valid = p_paddr_valid;
6227 map->p_align = segment->p_align;
6228 map->p_align_valid = 1;
6229 map->p_vaddr_offset = 0;
6231 if (map->p_type == PT_GNU_RELRO
6232 || map->p_type == PT_GNU_STACK)
6234 /* The PT_GNU_RELRO segment may contain the first a few
6235 bytes in the .got.plt section even if the whole .got.plt
6236 section isn't in the PT_GNU_RELRO segment. We won't
6237 change the size of the PT_GNU_RELRO segment.
6238 Similarly, PT_GNU_STACK size is significant on uclinux
6239 systems. */
6240 map->p_size = segment->p_memsz;
6241 map->p_size_valid = 1;
6244 /* Determine if this segment contains the ELF file header
6245 and if it contains the program headers themselves. */
6246 map->includes_filehdr = (segment->p_offset == 0
6247 && segment->p_filesz >= iehdr->e_ehsize);
6249 map->includes_phdrs = 0;
6250 if (! phdr_included || segment->p_type != PT_LOAD)
6252 map->includes_phdrs =
6253 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6254 && (segment->p_offset + segment->p_filesz
6255 >= ((bfd_vma) iehdr->e_phoff
6256 + iehdr->e_phnum * iehdr->e_phentsize)));
6258 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6259 phdr_included = TRUE;
6262 lowest_section = NULL;
6263 if (section_count != 0)
6265 unsigned int isec = 0;
6267 for (section = first_section;
6268 section != NULL;
6269 section = section->next)
6271 this_hdr = &(elf_section_data(section)->this_hdr);
6272 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6274 map->sections[isec++] = section->output_section;
6275 if ((section->flags & SEC_ALLOC) != 0)
6277 bfd_vma seg_off;
6279 if (lowest_section == NULL
6280 || section->lma < lowest_section->lma)
6281 lowest_section = section;
6283 /* Section lmas are set up from PT_LOAD header
6284 p_paddr in _bfd_elf_make_section_from_shdr.
6285 If this header has a p_paddr that disagrees
6286 with the section lma, flag the p_paddr as
6287 invalid. */
6288 if ((section->flags & SEC_LOAD) != 0)
6289 seg_off = this_hdr->sh_offset - segment->p_offset;
6290 else
6291 seg_off = this_hdr->sh_addr - segment->p_vaddr;
6292 if (section->lma - segment->p_paddr != seg_off)
6293 map->p_paddr_valid = FALSE;
6295 if (isec == section_count)
6296 break;
6301 if (map->includes_filehdr && lowest_section != NULL)
6302 /* We need to keep the space used by the headers fixed. */
6303 map->header_size = lowest_section->vma - segment->p_vaddr;
6305 if (!map->includes_phdrs
6306 && !map->includes_filehdr
6307 && map->p_paddr_valid)
6308 /* There is some other padding before the first section. */
6309 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6310 - segment->p_paddr);
6312 map->count = section_count;
6313 *pointer_to_map = map;
6314 pointer_to_map = &map->next;
6317 elf_seg_map (obfd) = map_first;
6318 return TRUE;
6321 /* Copy private BFD data. This copies or rewrites ELF program header
6322 information. */
6324 static bfd_boolean
6325 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6327 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6328 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6329 return TRUE;
6331 if (elf_tdata (ibfd)->phdr == NULL)
6332 return TRUE;
6334 if (ibfd->xvec == obfd->xvec)
6336 /* Check to see if any sections in the input BFD
6337 covered by ELF program header have changed. */
6338 Elf_Internal_Phdr *segment;
6339 asection *section, *osec;
6340 unsigned int i, num_segments;
6341 Elf_Internal_Shdr *this_hdr;
6342 const struct elf_backend_data *bed;
6344 bed = get_elf_backend_data (ibfd);
6346 /* Regenerate the segment map if p_paddr is set to 0. */
6347 if (bed->want_p_paddr_set_to_zero)
6348 goto rewrite;
6350 /* Initialize the segment mark field. */
6351 for (section = obfd->sections; section != NULL;
6352 section = section->next)
6353 section->segment_mark = FALSE;
6355 num_segments = elf_elfheader (ibfd)->e_phnum;
6356 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6357 i < num_segments;
6358 i++, segment++)
6360 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6361 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6362 which severly confuses things, so always regenerate the segment
6363 map in this case. */
6364 if (segment->p_paddr == 0
6365 && segment->p_memsz == 0
6366 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6367 goto rewrite;
6369 for (section = ibfd->sections;
6370 section != NULL; section = section->next)
6372 /* We mark the output section so that we know it comes
6373 from the input BFD. */
6374 osec = section->output_section;
6375 if (osec)
6376 osec->segment_mark = TRUE;
6378 /* Check if this section is covered by the segment. */
6379 this_hdr = &(elf_section_data(section)->this_hdr);
6380 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6382 /* FIXME: Check if its output section is changed or
6383 removed. What else do we need to check? */
6384 if (osec == NULL
6385 || section->flags != osec->flags
6386 || section->lma != osec->lma
6387 || section->vma != osec->vma
6388 || section->size != osec->size
6389 || section->rawsize != osec->rawsize
6390 || section->alignment_power != osec->alignment_power)
6391 goto rewrite;
6396 /* Check to see if any output section do not come from the
6397 input BFD. */
6398 for (section = obfd->sections; section != NULL;
6399 section = section->next)
6401 if (section->segment_mark == FALSE)
6402 goto rewrite;
6403 else
6404 section->segment_mark = FALSE;
6407 return copy_elf_program_header (ibfd, obfd);
6410 rewrite:
6411 if (ibfd->xvec == obfd->xvec)
6413 /* When rewriting program header, set the output maxpagesize to
6414 the maximum alignment of input PT_LOAD segments. */
6415 Elf_Internal_Phdr *segment;
6416 unsigned int i;
6417 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
6418 bfd_vma maxpagesize = 0;
6420 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6421 i < num_segments;
6422 i++, segment++)
6423 if (segment->p_type == PT_LOAD
6424 && maxpagesize < segment->p_align)
6425 maxpagesize = segment->p_align;
6427 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
6428 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
6431 return rewrite_elf_program_header (ibfd, obfd);
6434 /* Initialize private output section information from input section. */
6436 bfd_boolean
6437 _bfd_elf_init_private_section_data (bfd *ibfd,
6438 asection *isec,
6439 bfd *obfd,
6440 asection *osec,
6441 struct bfd_link_info *link_info)
6444 Elf_Internal_Shdr *ihdr, *ohdr;
6445 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6447 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6448 || obfd->xvec->flavour != bfd_target_elf_flavour)
6449 return TRUE;
6451 BFD_ASSERT (elf_section_data (osec) != NULL);
6453 /* For objcopy and relocatable link, don't copy the output ELF
6454 section type from input if the output BFD section flags have been
6455 set to something different. For a final link allow some flags
6456 that the linker clears to differ. */
6457 if (elf_section_type (osec) == SHT_NULL
6458 && (osec->flags == isec->flags
6459 || (final_link
6460 && ((osec->flags ^ isec->flags)
6461 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6462 elf_section_type (osec) = elf_section_type (isec);
6464 /* FIXME: Is this correct for all OS/PROC specific flags? */
6465 elf_section_flags (osec) |= (elf_section_flags (isec)
6466 & (SHF_MASKOS | SHF_MASKPROC));
6468 /* Set things up for objcopy and relocatable link. The output
6469 SHT_GROUP section will have its elf_next_in_group pointing back
6470 to the input group members. Ignore linker created group section.
6471 See elfNN_ia64_object_p in elfxx-ia64.c. */
6472 if (!final_link)
6474 if (elf_sec_group (isec) == NULL
6475 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6477 if (elf_section_flags (isec) & SHF_GROUP)
6478 elf_section_flags (osec) |= SHF_GROUP;
6479 elf_next_in_group (osec) = elf_next_in_group (isec);
6480 elf_section_data (osec)->group = elf_section_data (isec)->group;
6484 ihdr = &elf_section_data (isec)->this_hdr;
6486 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6487 don't use the output section of the linked-to section since it
6488 may be NULL at this point. */
6489 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6491 ohdr = &elf_section_data (osec)->this_hdr;
6492 ohdr->sh_flags |= SHF_LINK_ORDER;
6493 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6496 osec->use_rela_p = isec->use_rela_p;
6498 return TRUE;
6501 /* Copy private section information. This copies over the entsize
6502 field, and sometimes the info field. */
6504 bfd_boolean
6505 _bfd_elf_copy_private_section_data (bfd *ibfd,
6506 asection *isec,
6507 bfd *obfd,
6508 asection *osec)
6510 Elf_Internal_Shdr *ihdr, *ohdr;
6512 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6513 || obfd->xvec->flavour != bfd_target_elf_flavour)
6514 return TRUE;
6516 ihdr = &elf_section_data (isec)->this_hdr;
6517 ohdr = &elf_section_data (osec)->this_hdr;
6519 ohdr->sh_entsize = ihdr->sh_entsize;
6521 if (ihdr->sh_type == SHT_SYMTAB
6522 || ihdr->sh_type == SHT_DYNSYM
6523 || ihdr->sh_type == SHT_GNU_verneed
6524 || ihdr->sh_type == SHT_GNU_verdef)
6525 ohdr->sh_info = ihdr->sh_info;
6527 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6528 NULL);
6531 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6532 necessary if we are removing either the SHT_GROUP section or any of
6533 the group member sections. DISCARDED is the value that a section's
6534 output_section has if the section will be discarded, NULL when this
6535 function is called from objcopy, bfd_abs_section_ptr when called
6536 from the linker. */
6538 bfd_boolean
6539 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6541 asection *isec;
6543 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6544 if (elf_section_type (isec) == SHT_GROUP)
6546 asection *first = elf_next_in_group (isec);
6547 asection *s = first;
6548 bfd_size_type removed = 0;
6550 while (s != NULL)
6552 /* If this member section is being output but the
6553 SHT_GROUP section is not, then clear the group info
6554 set up by _bfd_elf_copy_private_section_data. */
6555 if (s->output_section != discarded
6556 && isec->output_section == discarded)
6558 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6559 elf_group_name (s->output_section) = NULL;
6561 /* Conversely, if the member section is not being output
6562 but the SHT_GROUP section is, then adjust its size. */
6563 else if (s->output_section == discarded
6564 && isec->output_section != discarded)
6565 removed += 4;
6566 s = elf_next_in_group (s);
6567 if (s == first)
6568 break;
6570 if (removed != 0)
6572 if (discarded != NULL)
6574 /* If we've been called for ld -r, then we need to
6575 adjust the input section size. This function may
6576 be called multiple times, so save the original
6577 size. */
6578 if (isec->rawsize == 0)
6579 isec->rawsize = isec->size;
6580 isec->size = isec->rawsize - removed;
6582 else
6584 /* Adjust the output section size when called from
6585 objcopy. */
6586 isec->output_section->size -= removed;
6591 return TRUE;
6594 /* Copy private header information. */
6596 bfd_boolean
6597 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6599 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6600 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6601 return TRUE;
6603 /* Copy over private BFD data if it has not already been copied.
6604 This must be done here, rather than in the copy_private_bfd_data
6605 entry point, because the latter is called after the section
6606 contents have been set, which means that the program headers have
6607 already been worked out. */
6608 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
6610 if (! copy_private_bfd_data (ibfd, obfd))
6611 return FALSE;
6614 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6617 /* Copy private symbol information. If this symbol is in a section
6618 which we did not map into a BFD section, try to map the section
6619 index correctly. We use special macro definitions for the mapped
6620 section indices; these definitions are interpreted by the
6621 swap_out_syms function. */
6623 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6624 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6625 #define MAP_STRTAB (SHN_HIOS + 3)
6626 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6627 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6629 bfd_boolean
6630 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6631 asymbol *isymarg,
6632 bfd *obfd,
6633 asymbol *osymarg)
6635 elf_symbol_type *isym, *osym;
6637 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6638 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6639 return TRUE;
6641 isym = elf_symbol_from (ibfd, isymarg);
6642 osym = elf_symbol_from (obfd, osymarg);
6644 if (isym != NULL
6645 && isym->internal_elf_sym.st_shndx != 0
6646 && osym != NULL
6647 && bfd_is_abs_section (isym->symbol.section))
6649 unsigned int shndx;
6651 shndx = isym->internal_elf_sym.st_shndx;
6652 if (shndx == elf_onesymtab (ibfd))
6653 shndx = MAP_ONESYMTAB;
6654 else if (shndx == elf_dynsymtab (ibfd))
6655 shndx = MAP_DYNSYMTAB;
6656 else if (shndx == elf_strtab_sec (ibfd))
6657 shndx = MAP_STRTAB;
6658 else if (shndx == elf_shstrtab_sec (ibfd))
6659 shndx = MAP_SHSTRTAB;
6660 else if (shndx == elf_symtab_shndx (ibfd))
6661 shndx = MAP_SYM_SHNDX;
6662 osym->internal_elf_sym.st_shndx = shndx;
6665 return TRUE;
6668 /* Swap out the symbols. */
6670 static bfd_boolean
6671 swap_out_syms (bfd *abfd,
6672 struct bfd_strtab_hash **sttp,
6673 int relocatable_p)
6675 const struct elf_backend_data *bed;
6676 int symcount;
6677 asymbol **syms;
6678 struct bfd_strtab_hash *stt;
6679 Elf_Internal_Shdr *symtab_hdr;
6680 Elf_Internal_Shdr *symtab_shndx_hdr;
6681 Elf_Internal_Shdr *symstrtab_hdr;
6682 bfd_byte *outbound_syms;
6683 bfd_byte *outbound_shndx;
6684 int idx;
6685 unsigned int num_locals;
6686 bfd_size_type amt;
6687 bfd_boolean name_local_sections;
6689 if (!elf_map_symbols (abfd, &num_locals))
6690 return FALSE;
6692 /* Dump out the symtabs. */
6693 stt = _bfd_elf_stringtab_init ();
6694 if (stt == NULL)
6695 return FALSE;
6697 bed = get_elf_backend_data (abfd);
6698 symcount = bfd_get_symcount (abfd);
6699 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6700 symtab_hdr->sh_type = SHT_SYMTAB;
6701 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6702 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6703 symtab_hdr->sh_info = num_locals + 1;
6704 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6706 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6707 symstrtab_hdr->sh_type = SHT_STRTAB;
6709 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6710 bed->s->sizeof_sym);
6711 if (outbound_syms == NULL)
6713 _bfd_stringtab_free (stt);
6714 return FALSE;
6716 symtab_hdr->contents = outbound_syms;
6718 outbound_shndx = NULL;
6719 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6720 if (symtab_shndx_hdr->sh_name != 0)
6722 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6723 outbound_shndx = (bfd_byte *)
6724 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6725 if (outbound_shndx == NULL)
6727 _bfd_stringtab_free (stt);
6728 return FALSE;
6731 symtab_shndx_hdr->contents = outbound_shndx;
6732 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6733 symtab_shndx_hdr->sh_size = amt;
6734 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6735 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6738 /* Now generate the data (for "contents"). */
6740 /* Fill in zeroth symbol and swap it out. */
6741 Elf_Internal_Sym sym;
6742 sym.st_name = 0;
6743 sym.st_value = 0;
6744 sym.st_size = 0;
6745 sym.st_info = 0;
6746 sym.st_other = 0;
6747 sym.st_shndx = SHN_UNDEF;
6748 sym.st_target_internal = 0;
6749 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6750 outbound_syms += bed->s->sizeof_sym;
6751 if (outbound_shndx != NULL)
6752 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6755 name_local_sections
6756 = (bed->elf_backend_name_local_section_symbols
6757 && bed->elf_backend_name_local_section_symbols (abfd));
6759 syms = bfd_get_outsymbols (abfd);
6760 for (idx = 0; idx < symcount; idx++)
6762 Elf_Internal_Sym sym;
6763 bfd_vma value = syms[idx]->value;
6764 elf_symbol_type *type_ptr;
6765 flagword flags = syms[idx]->flags;
6766 int type;
6768 if (!name_local_sections
6769 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6771 /* Local section symbols have no name. */
6772 sym.st_name = 0;
6774 else
6776 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6777 syms[idx]->name,
6778 TRUE, FALSE);
6779 if (sym.st_name == (unsigned long) -1)
6781 _bfd_stringtab_free (stt);
6782 return FALSE;
6786 type_ptr = elf_symbol_from (abfd, syms[idx]);
6788 if ((flags & BSF_SECTION_SYM) == 0
6789 && bfd_is_com_section (syms[idx]->section))
6791 /* ELF common symbols put the alignment into the `value' field,
6792 and the size into the `size' field. This is backwards from
6793 how BFD handles it, so reverse it here. */
6794 sym.st_size = value;
6795 if (type_ptr == NULL
6796 || type_ptr->internal_elf_sym.st_value == 0)
6797 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6798 else
6799 sym.st_value = type_ptr->internal_elf_sym.st_value;
6800 sym.st_shndx = _bfd_elf_section_from_bfd_section
6801 (abfd, syms[idx]->section);
6803 else
6805 asection *sec = syms[idx]->section;
6806 unsigned int shndx;
6808 if (sec->output_section)
6810 value += sec->output_offset;
6811 sec = sec->output_section;
6814 /* Don't add in the section vma for relocatable output. */
6815 if (! relocatable_p)
6816 value += sec->vma;
6817 sym.st_value = value;
6818 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6820 if (bfd_is_abs_section (sec)
6821 && type_ptr != NULL
6822 && type_ptr->internal_elf_sym.st_shndx != 0)
6824 /* This symbol is in a real ELF section which we did
6825 not create as a BFD section. Undo the mapping done
6826 by copy_private_symbol_data. */
6827 shndx = type_ptr->internal_elf_sym.st_shndx;
6828 switch (shndx)
6830 case MAP_ONESYMTAB:
6831 shndx = elf_onesymtab (abfd);
6832 break;
6833 case MAP_DYNSYMTAB:
6834 shndx = elf_dynsymtab (abfd);
6835 break;
6836 case MAP_STRTAB:
6837 shndx = elf_strtab_sec (abfd);
6838 break;
6839 case MAP_SHSTRTAB:
6840 shndx = elf_shstrtab_sec (abfd);
6841 break;
6842 case MAP_SYM_SHNDX:
6843 shndx = elf_symtab_shndx (abfd);
6844 break;
6845 default:
6846 shndx = SHN_ABS;
6847 break;
6850 else
6852 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6854 if (shndx == SHN_BAD)
6856 asection *sec2;
6858 /* Writing this would be a hell of a lot easier if
6859 we had some decent documentation on bfd, and
6860 knew what to expect of the library, and what to
6861 demand of applications. For example, it
6862 appears that `objcopy' might not set the
6863 section of a symbol to be a section that is
6864 actually in the output file. */
6865 sec2 = bfd_get_section_by_name (abfd, sec->name);
6866 if (sec2 == NULL)
6868 _bfd_error_handler (_("\
6869 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6870 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6871 sec->name);
6872 bfd_set_error (bfd_error_invalid_operation);
6873 _bfd_stringtab_free (stt);
6874 return FALSE;
6877 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6878 BFD_ASSERT (shndx != SHN_BAD);
6882 sym.st_shndx = shndx;
6885 if ((flags & BSF_THREAD_LOCAL) != 0)
6886 type = STT_TLS;
6887 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6888 type = STT_GNU_IFUNC;
6889 else if ((flags & BSF_FUNCTION) != 0)
6890 type = STT_FUNC;
6891 else if ((flags & BSF_OBJECT) != 0)
6892 type = STT_OBJECT;
6893 else if ((flags & BSF_RELC) != 0)
6894 type = STT_RELC;
6895 else if ((flags & BSF_SRELC) != 0)
6896 type = STT_SRELC;
6897 else
6898 type = STT_NOTYPE;
6900 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6901 type = STT_TLS;
6903 /* Processor-specific types. */
6904 if (type_ptr != NULL
6905 && bed->elf_backend_get_symbol_type)
6906 type = ((*bed->elf_backend_get_symbol_type)
6907 (&type_ptr->internal_elf_sym, type));
6909 if (flags & BSF_SECTION_SYM)
6911 if (flags & BSF_GLOBAL)
6912 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6913 else
6914 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6916 else if (bfd_is_com_section (syms[idx]->section))
6918 #ifdef USE_STT_COMMON
6919 if (type == STT_OBJECT)
6920 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6921 else
6922 #endif
6923 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6925 else if (bfd_is_und_section (syms[idx]->section))
6926 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6927 ? STB_WEAK
6928 : STB_GLOBAL),
6929 type);
6930 else if (flags & BSF_FILE)
6931 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6932 else
6934 int bind = STB_LOCAL;
6936 if (flags & BSF_LOCAL)
6937 bind = STB_LOCAL;
6938 else if (flags & BSF_GNU_UNIQUE)
6939 bind = STB_GNU_UNIQUE;
6940 else if (flags & BSF_WEAK)
6941 bind = STB_WEAK;
6942 else if (flags & BSF_GLOBAL)
6943 bind = STB_GLOBAL;
6945 sym.st_info = ELF_ST_INFO (bind, type);
6948 if (type_ptr != NULL)
6950 sym.st_other = type_ptr->internal_elf_sym.st_other;
6951 sym.st_target_internal
6952 = type_ptr->internal_elf_sym.st_target_internal;
6954 else
6956 sym.st_other = 0;
6957 sym.st_target_internal = 0;
6960 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6961 outbound_syms += bed->s->sizeof_sym;
6962 if (outbound_shndx != NULL)
6963 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6966 *sttp = stt;
6967 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6968 symstrtab_hdr->sh_type = SHT_STRTAB;
6970 symstrtab_hdr->sh_flags = 0;
6971 symstrtab_hdr->sh_addr = 0;
6972 symstrtab_hdr->sh_entsize = 0;
6973 symstrtab_hdr->sh_link = 0;
6974 symstrtab_hdr->sh_info = 0;
6975 symstrtab_hdr->sh_addralign = 1;
6977 return TRUE;
6980 /* Return the number of bytes required to hold the symtab vector.
6982 Note that we base it on the count plus 1, since we will null terminate
6983 the vector allocated based on this size. However, the ELF symbol table
6984 always has a dummy entry as symbol #0, so it ends up even. */
6986 long
6987 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6989 long symcount;
6990 long symtab_size;
6991 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6993 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6994 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6995 if (symcount > 0)
6996 symtab_size -= sizeof (asymbol *);
6998 return symtab_size;
7001 long
7002 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
7004 long symcount;
7005 long symtab_size;
7006 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
7008 if (elf_dynsymtab (abfd) == 0)
7010 bfd_set_error (bfd_error_invalid_operation);
7011 return -1;
7014 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7015 symtab_size = (symcount + 1) * (sizeof (asymbol *));
7016 if (symcount > 0)
7017 symtab_size -= sizeof (asymbol *);
7019 return symtab_size;
7022 long
7023 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
7024 sec_ptr asect)
7026 return (asect->reloc_count + 1) * sizeof (arelent *);
7029 /* Canonicalize the relocs. */
7031 long
7032 _bfd_elf_canonicalize_reloc (bfd *abfd,
7033 sec_ptr section,
7034 arelent **relptr,
7035 asymbol **symbols)
7037 arelent *tblptr;
7038 unsigned int i;
7039 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7041 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
7042 return -1;
7044 tblptr = section->relocation;
7045 for (i = 0; i < section->reloc_count; i++)
7046 *relptr++ = tblptr++;
7048 *relptr = NULL;
7050 return section->reloc_count;
7053 long
7054 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
7056 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7057 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
7059 if (symcount >= 0)
7060 bfd_get_symcount (abfd) = symcount;
7061 return symcount;
7064 long
7065 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
7066 asymbol **allocation)
7068 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7069 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
7071 if (symcount >= 0)
7072 bfd_get_dynamic_symcount (abfd) = symcount;
7073 return symcount;
7076 /* Return the size required for the dynamic reloc entries. Any loadable
7077 section that was actually installed in the BFD, and has type SHT_REL
7078 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7079 dynamic reloc section. */
7081 long
7082 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
7084 long ret;
7085 asection *s;
7087 if (elf_dynsymtab (abfd) == 0)
7089 bfd_set_error (bfd_error_invalid_operation);
7090 return -1;
7093 ret = sizeof (arelent *);
7094 for (s = abfd->sections; s != NULL; s = s->next)
7095 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7096 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7097 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7098 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
7099 * sizeof (arelent *));
7101 return ret;
7104 /* Canonicalize the dynamic relocation entries. Note that we return the
7105 dynamic relocations as a single block, although they are actually
7106 associated with particular sections; the interface, which was
7107 designed for SunOS style shared libraries, expects that there is only
7108 one set of dynamic relocs. Any loadable section that was actually
7109 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7110 dynamic symbol table, is considered to be a dynamic reloc section. */
7112 long
7113 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
7114 arelent **storage,
7115 asymbol **syms)
7117 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7118 asection *s;
7119 long ret;
7121 if (elf_dynsymtab (abfd) == 0)
7123 bfd_set_error (bfd_error_invalid_operation);
7124 return -1;
7127 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7128 ret = 0;
7129 for (s = abfd->sections; s != NULL; s = s->next)
7131 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7132 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7133 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7135 arelent *p;
7136 long count, i;
7138 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
7139 return -1;
7140 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
7141 p = s->relocation;
7142 for (i = 0; i < count; i++)
7143 *storage++ = p++;
7144 ret += count;
7148 *storage = NULL;
7150 return ret;
7153 /* Read in the version information. */
7155 bfd_boolean
7156 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
7158 bfd_byte *contents = NULL;
7159 unsigned int freeidx = 0;
7161 if (elf_dynverref (abfd) != 0)
7163 Elf_Internal_Shdr *hdr;
7164 Elf_External_Verneed *everneed;
7165 Elf_Internal_Verneed *iverneed;
7166 unsigned int i;
7167 bfd_byte *contents_end;
7169 hdr = &elf_tdata (abfd)->dynverref_hdr;
7171 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
7172 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
7173 if (elf_tdata (abfd)->verref == NULL)
7174 goto error_return;
7176 elf_tdata (abfd)->cverrefs = hdr->sh_info;
7178 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7179 if (contents == NULL)
7181 error_return_verref:
7182 elf_tdata (abfd)->verref = NULL;
7183 elf_tdata (abfd)->cverrefs = 0;
7184 goto error_return;
7186 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7187 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7188 goto error_return_verref;
7190 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
7191 goto error_return_verref;
7193 BFD_ASSERT (sizeof (Elf_External_Verneed)
7194 == sizeof (Elf_External_Vernaux));
7195 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
7196 everneed = (Elf_External_Verneed *) contents;
7197 iverneed = elf_tdata (abfd)->verref;
7198 for (i = 0; i < hdr->sh_info; i++, iverneed++)
7200 Elf_External_Vernaux *evernaux;
7201 Elf_Internal_Vernaux *ivernaux;
7202 unsigned int j;
7204 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
7206 iverneed->vn_bfd = abfd;
7208 iverneed->vn_filename =
7209 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7210 iverneed->vn_file);
7211 if (iverneed->vn_filename == NULL)
7212 goto error_return_verref;
7214 if (iverneed->vn_cnt == 0)
7215 iverneed->vn_auxptr = NULL;
7216 else
7218 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
7219 bfd_alloc2 (abfd, iverneed->vn_cnt,
7220 sizeof (Elf_Internal_Vernaux));
7221 if (iverneed->vn_auxptr == NULL)
7222 goto error_return_verref;
7225 if (iverneed->vn_aux
7226 > (size_t) (contents_end - (bfd_byte *) everneed))
7227 goto error_return_verref;
7229 evernaux = ((Elf_External_Vernaux *)
7230 ((bfd_byte *) everneed + iverneed->vn_aux));
7231 ivernaux = iverneed->vn_auxptr;
7232 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
7234 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
7236 ivernaux->vna_nodename =
7237 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7238 ivernaux->vna_name);
7239 if (ivernaux->vna_nodename == NULL)
7240 goto error_return_verref;
7242 if (j + 1 < iverneed->vn_cnt)
7243 ivernaux->vna_nextptr = ivernaux + 1;
7244 else
7245 ivernaux->vna_nextptr = NULL;
7247 if (ivernaux->vna_next
7248 > (size_t) (contents_end - (bfd_byte *) evernaux))
7249 goto error_return_verref;
7251 evernaux = ((Elf_External_Vernaux *)
7252 ((bfd_byte *) evernaux + ivernaux->vna_next));
7254 if (ivernaux->vna_other > freeidx)
7255 freeidx = ivernaux->vna_other;
7258 if (i + 1 < hdr->sh_info)
7259 iverneed->vn_nextref = iverneed + 1;
7260 else
7261 iverneed->vn_nextref = NULL;
7263 if (iverneed->vn_next
7264 > (size_t) (contents_end - (bfd_byte *) everneed))
7265 goto error_return_verref;
7267 everneed = ((Elf_External_Verneed *)
7268 ((bfd_byte *) everneed + iverneed->vn_next));
7271 free (contents);
7272 contents = NULL;
7275 if (elf_dynverdef (abfd) != 0)
7277 Elf_Internal_Shdr *hdr;
7278 Elf_External_Verdef *everdef;
7279 Elf_Internal_Verdef *iverdef;
7280 Elf_Internal_Verdef *iverdefarr;
7281 Elf_Internal_Verdef iverdefmem;
7282 unsigned int i;
7283 unsigned int maxidx;
7284 bfd_byte *contents_end_def, *contents_end_aux;
7286 hdr = &elf_tdata (abfd)->dynverdef_hdr;
7288 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7289 if (contents == NULL)
7290 goto error_return;
7291 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7292 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7293 goto error_return;
7295 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
7296 goto error_return;
7298 BFD_ASSERT (sizeof (Elf_External_Verdef)
7299 >= sizeof (Elf_External_Verdaux));
7300 contents_end_def = contents + hdr->sh_size
7301 - sizeof (Elf_External_Verdef);
7302 contents_end_aux = contents + hdr->sh_size
7303 - sizeof (Elf_External_Verdaux);
7305 /* We know the number of entries in the section but not the maximum
7306 index. Therefore we have to run through all entries and find
7307 the maximum. */
7308 everdef = (Elf_External_Verdef *) contents;
7309 maxidx = 0;
7310 for (i = 0; i < hdr->sh_info; ++i)
7312 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7314 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7315 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
7317 if (iverdefmem.vd_next
7318 > (size_t) (contents_end_def - (bfd_byte *) everdef))
7319 goto error_return;
7321 everdef = ((Elf_External_Verdef *)
7322 ((bfd_byte *) everdef + iverdefmem.vd_next));
7325 if (default_imported_symver)
7327 if (freeidx > maxidx)
7328 maxidx = ++freeidx;
7329 else
7330 freeidx = ++maxidx;
7332 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7333 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7334 if (elf_tdata (abfd)->verdef == NULL)
7335 goto error_return;
7337 elf_tdata (abfd)->cverdefs = maxidx;
7339 everdef = (Elf_External_Verdef *) contents;
7340 iverdefarr = elf_tdata (abfd)->verdef;
7341 for (i = 0; i < hdr->sh_info; i++)
7343 Elf_External_Verdaux *everdaux;
7344 Elf_Internal_Verdaux *iverdaux;
7345 unsigned int j;
7347 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7349 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7351 error_return_verdef:
7352 elf_tdata (abfd)->verdef = NULL;
7353 elf_tdata (abfd)->cverdefs = 0;
7354 goto error_return;
7357 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7358 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
7360 iverdef->vd_bfd = abfd;
7362 if (iverdef->vd_cnt == 0)
7363 iverdef->vd_auxptr = NULL;
7364 else
7366 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7367 bfd_alloc2 (abfd, iverdef->vd_cnt,
7368 sizeof (Elf_Internal_Verdaux));
7369 if (iverdef->vd_auxptr == NULL)
7370 goto error_return_verdef;
7373 if (iverdef->vd_aux
7374 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7375 goto error_return_verdef;
7377 everdaux = ((Elf_External_Verdaux *)
7378 ((bfd_byte *) everdef + iverdef->vd_aux));
7379 iverdaux = iverdef->vd_auxptr;
7380 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7382 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7384 iverdaux->vda_nodename =
7385 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7386 iverdaux->vda_name);
7387 if (iverdaux->vda_nodename == NULL)
7388 goto error_return_verdef;
7390 if (j + 1 < iverdef->vd_cnt)
7391 iverdaux->vda_nextptr = iverdaux + 1;
7392 else
7393 iverdaux->vda_nextptr = NULL;
7395 if (iverdaux->vda_next
7396 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7397 goto error_return_verdef;
7399 everdaux = ((Elf_External_Verdaux *)
7400 ((bfd_byte *) everdaux + iverdaux->vda_next));
7403 if (iverdef->vd_cnt)
7404 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7406 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7407 iverdef->vd_nextdef = iverdef + 1;
7408 else
7409 iverdef->vd_nextdef = NULL;
7411 everdef = ((Elf_External_Verdef *)
7412 ((bfd_byte *) everdef + iverdef->vd_next));
7415 free (contents);
7416 contents = NULL;
7418 else if (default_imported_symver)
7420 if (freeidx < 3)
7421 freeidx = 3;
7422 else
7423 freeidx++;
7425 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7426 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7427 if (elf_tdata (abfd)->verdef == NULL)
7428 goto error_return;
7430 elf_tdata (abfd)->cverdefs = freeidx;
7433 /* Create a default version based on the soname. */
7434 if (default_imported_symver)
7436 Elf_Internal_Verdef *iverdef;
7437 Elf_Internal_Verdaux *iverdaux;
7439 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
7441 iverdef->vd_version = VER_DEF_CURRENT;
7442 iverdef->vd_flags = 0;
7443 iverdef->vd_ndx = freeidx;
7444 iverdef->vd_cnt = 1;
7446 iverdef->vd_bfd = abfd;
7448 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7449 if (iverdef->vd_nodename == NULL)
7450 goto error_return_verdef;
7451 iverdef->vd_nextdef = NULL;
7452 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7453 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
7454 if (iverdef->vd_auxptr == NULL)
7455 goto error_return_verdef;
7457 iverdaux = iverdef->vd_auxptr;
7458 iverdaux->vda_nodename = iverdef->vd_nodename;
7459 iverdaux->vda_nextptr = NULL;
7462 return TRUE;
7464 error_return:
7465 if (contents != NULL)
7466 free (contents);
7467 return FALSE;
7470 asymbol *
7471 _bfd_elf_make_empty_symbol (bfd *abfd)
7473 elf_symbol_type *newsym;
7474 bfd_size_type amt = sizeof (elf_symbol_type);
7476 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
7477 if (!newsym)
7478 return NULL;
7479 else
7481 newsym->symbol.the_bfd = abfd;
7482 return &newsym->symbol;
7486 void
7487 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7488 asymbol *symbol,
7489 symbol_info *ret)
7491 bfd_symbol_info (symbol, ret);
7494 /* Return whether a symbol name implies a local symbol. Most targets
7495 use this function for the is_local_label_name entry point, but some
7496 override it. */
7498 bfd_boolean
7499 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7500 const char *name)
7502 /* Normal local symbols start with ``.L''. */
7503 if (name[0] == '.' && name[1] == 'L')
7504 return TRUE;
7506 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7507 DWARF debugging symbols starting with ``..''. */
7508 if (name[0] == '.' && name[1] == '.')
7509 return TRUE;
7511 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7512 emitting DWARF debugging output. I suspect this is actually a
7513 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7514 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7515 underscore to be emitted on some ELF targets). For ease of use,
7516 we treat such symbols as local. */
7517 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7518 return TRUE;
7520 return FALSE;
7523 alent *
7524 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7525 asymbol *symbol ATTRIBUTE_UNUSED)
7527 abort ();
7528 return NULL;
7531 bfd_boolean
7532 _bfd_elf_set_arch_mach (bfd *abfd,
7533 enum bfd_architecture arch,
7534 unsigned long machine)
7536 /* If this isn't the right architecture for this backend, and this
7537 isn't the generic backend, fail. */
7538 if (arch != get_elf_backend_data (abfd)->arch
7539 && arch != bfd_arch_unknown
7540 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7541 return FALSE;
7543 return bfd_default_set_arch_mach (abfd, arch, machine);
7546 /* Find the function to a particular section and offset,
7547 for error reporting. */
7549 static bfd_boolean
7550 elf_find_function (bfd *abfd,
7551 asection *section,
7552 asymbol **symbols,
7553 bfd_vma offset,
7554 const char **filename_ptr,
7555 const char **functionname_ptr)
7557 struct elf_find_function_cache
7559 asection *last_section;
7560 asymbol *func;
7561 const char *filename;
7562 bfd_size_type func_size;
7563 } *cache;
7565 if (symbols == NULL)
7566 return FALSE;
7568 cache = elf_tdata (abfd)->elf_find_function_cache;
7569 if (cache == NULL)
7571 cache = bfd_zalloc (abfd, sizeof (*cache));
7572 elf_tdata (abfd)->elf_find_function_cache = cache;
7573 if (cache == NULL)
7574 return FALSE;
7576 if (cache->last_section != section
7577 || cache->func == NULL
7578 || offset < cache->func->value
7579 || offset >= cache->func->value + cache->func_size)
7581 asymbol *file;
7582 bfd_vma low_func;
7583 asymbol **p;
7584 /* ??? Given multiple file symbols, it is impossible to reliably
7585 choose the right file name for global symbols. File symbols are
7586 local symbols, and thus all file symbols must sort before any
7587 global symbols. The ELF spec may be interpreted to say that a
7588 file symbol must sort before other local symbols, but currently
7589 ld -r doesn't do this. So, for ld -r output, it is possible to
7590 make a better choice of file name for local symbols by ignoring
7591 file symbols appearing after a given local symbol. */
7592 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7593 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7595 file = NULL;
7596 low_func = 0;
7597 state = nothing_seen;
7598 cache->filename = NULL;
7599 cache->func = NULL;
7600 cache->func_size = 0;
7601 cache->last_section = section;
7603 for (p = symbols; *p != NULL; p++)
7605 asymbol *sym = *p;
7606 bfd_vma code_off;
7607 bfd_size_type size;
7609 if ((sym->flags & BSF_FILE) != 0)
7611 file = sym;
7612 if (state == symbol_seen)
7613 state = file_after_symbol_seen;
7614 continue;
7617 size = bed->maybe_function_sym (sym, section, &code_off);
7618 if (size != 0
7619 && code_off <= offset
7620 && (code_off > low_func
7621 || (code_off == low_func
7622 && size > cache->func_size)))
7624 cache->func = sym;
7625 cache->func_size = size;
7626 cache->filename = NULL;
7627 low_func = code_off;
7628 if (file != NULL
7629 && ((sym->flags & BSF_LOCAL) != 0
7630 || state != file_after_symbol_seen))
7631 cache->filename = bfd_asymbol_name (file);
7633 if (state == nothing_seen)
7634 state = symbol_seen;
7638 if (cache->func == NULL)
7639 return FALSE;
7641 if (filename_ptr)
7642 *filename_ptr = cache->filename;
7643 if (functionname_ptr)
7644 *functionname_ptr = bfd_asymbol_name (cache->func);
7646 return TRUE;
7649 /* Find the nearest line to a particular section and offset,
7650 for error reporting. */
7652 bfd_boolean
7653 _bfd_elf_find_nearest_line (bfd *abfd,
7654 asection *section,
7655 asymbol **symbols,
7656 bfd_vma offset,
7657 const char **filename_ptr,
7658 const char **functionname_ptr,
7659 unsigned int *line_ptr)
7661 return _bfd_elf_find_nearest_line_discriminator (abfd, section, symbols,
7662 offset, filename_ptr,
7663 functionname_ptr,
7664 line_ptr,
7665 NULL);
7668 bfd_boolean
7669 _bfd_elf_find_nearest_line_discriminator (bfd *abfd,
7670 asection *section,
7671 asymbol **symbols,
7672 bfd_vma offset,
7673 const char **filename_ptr,
7674 const char **functionname_ptr,
7675 unsigned int *line_ptr,
7676 unsigned int *discriminator_ptr)
7678 bfd_boolean found;
7680 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7681 filename_ptr, functionname_ptr,
7682 line_ptr))
7684 if (!*functionname_ptr)
7685 elf_find_function (abfd, section, symbols, offset,
7686 *filename_ptr ? NULL : filename_ptr,
7687 functionname_ptr);
7689 return TRUE;
7692 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
7693 section, symbols, offset,
7694 filename_ptr, functionname_ptr,
7695 line_ptr, discriminator_ptr, 0,
7696 &elf_tdata (abfd)->dwarf2_find_line_info))
7698 if (!*functionname_ptr)
7699 elf_find_function (abfd, section, symbols, offset,
7700 *filename_ptr ? NULL : filename_ptr,
7701 functionname_ptr);
7703 return TRUE;
7706 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7707 &found, filename_ptr,
7708 functionname_ptr, line_ptr,
7709 &elf_tdata (abfd)->line_info))
7710 return FALSE;
7711 if (found && (*functionname_ptr || *line_ptr))
7712 return TRUE;
7714 if (symbols == NULL)
7715 return FALSE;
7717 if (! elf_find_function (abfd, section, symbols, offset,
7718 filename_ptr, functionname_ptr))
7719 return FALSE;
7721 *line_ptr = 0;
7722 return TRUE;
7725 /* Find the line for a symbol. */
7727 bfd_boolean
7728 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7729 const char **filename_ptr, unsigned int *line_ptr)
7731 return _bfd_elf_find_line_discriminator (abfd, symbols, symbol,
7732 filename_ptr, line_ptr,
7733 NULL);
7736 bfd_boolean
7737 _bfd_elf_find_line_discriminator (bfd *abfd, asymbol **symbols, asymbol *symbol,
7738 const char **filename_ptr,
7739 unsigned int *line_ptr,
7740 unsigned int *discriminator_ptr)
7742 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7743 filename_ptr, line_ptr, discriminator_ptr, 0,
7744 &elf_tdata (abfd)->dwarf2_find_line_info);
7747 /* After a call to bfd_find_nearest_line, successive calls to
7748 bfd_find_inliner_info can be used to get source information about
7749 each level of function inlining that terminated at the address
7750 passed to bfd_find_nearest_line. Currently this is only supported
7751 for DWARF2 with appropriate DWARF3 extensions. */
7753 bfd_boolean
7754 _bfd_elf_find_inliner_info (bfd *abfd,
7755 const char **filename_ptr,
7756 const char **functionname_ptr,
7757 unsigned int *line_ptr)
7759 bfd_boolean found;
7760 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7761 functionname_ptr, line_ptr,
7762 & elf_tdata (abfd)->dwarf2_find_line_info);
7763 return found;
7767 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7769 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7770 int ret = bed->s->sizeof_ehdr;
7772 if (!info->relocatable)
7774 bfd_size_type phdr_size = elf_program_header_size (abfd);
7776 if (phdr_size == (bfd_size_type) -1)
7778 struct elf_segment_map *m;
7780 phdr_size = 0;
7781 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
7782 phdr_size += bed->s->sizeof_phdr;
7784 if (phdr_size == 0)
7785 phdr_size = get_program_header_size (abfd, info);
7788 elf_program_header_size (abfd) = phdr_size;
7789 ret += phdr_size;
7792 return ret;
7795 bfd_boolean
7796 _bfd_elf_set_section_contents (bfd *abfd,
7797 sec_ptr section,
7798 const void *location,
7799 file_ptr offset,
7800 bfd_size_type count)
7802 Elf_Internal_Shdr *hdr;
7803 bfd_signed_vma pos;
7805 if (! abfd->output_has_begun
7806 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7807 return FALSE;
7809 hdr = &elf_section_data (section)->this_hdr;
7810 pos = hdr->sh_offset + offset;
7811 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7812 || bfd_bwrite (location, count, abfd) != count)
7813 return FALSE;
7815 return TRUE;
7818 void
7819 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7820 arelent *cache_ptr ATTRIBUTE_UNUSED,
7821 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7823 abort ();
7826 /* Try to convert a non-ELF reloc into an ELF one. */
7828 bfd_boolean
7829 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7831 /* Check whether we really have an ELF howto. */
7833 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7835 bfd_reloc_code_real_type code;
7836 reloc_howto_type *howto;
7838 /* Alien reloc: Try to determine its type to replace it with an
7839 equivalent ELF reloc. */
7841 if (areloc->howto->pc_relative)
7843 switch (areloc->howto->bitsize)
7845 case 8:
7846 code = BFD_RELOC_8_PCREL;
7847 break;
7848 case 12:
7849 code = BFD_RELOC_12_PCREL;
7850 break;
7851 case 16:
7852 code = BFD_RELOC_16_PCREL;
7853 break;
7854 case 24:
7855 code = BFD_RELOC_24_PCREL;
7856 break;
7857 case 32:
7858 code = BFD_RELOC_32_PCREL;
7859 break;
7860 case 64:
7861 code = BFD_RELOC_64_PCREL;
7862 break;
7863 default:
7864 goto fail;
7867 howto = bfd_reloc_type_lookup (abfd, code);
7869 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7871 if (howto->pcrel_offset)
7872 areloc->addend += areloc->address;
7873 else
7874 areloc->addend -= areloc->address; /* addend is unsigned!! */
7877 else
7879 switch (areloc->howto->bitsize)
7881 case 8:
7882 code = BFD_RELOC_8;
7883 break;
7884 case 14:
7885 code = BFD_RELOC_14;
7886 break;
7887 case 16:
7888 code = BFD_RELOC_16;
7889 break;
7890 case 26:
7891 code = BFD_RELOC_26;
7892 break;
7893 case 32:
7894 code = BFD_RELOC_32;
7895 break;
7896 case 64:
7897 code = BFD_RELOC_64;
7898 break;
7899 default:
7900 goto fail;
7903 howto = bfd_reloc_type_lookup (abfd, code);
7906 if (howto)
7907 areloc->howto = howto;
7908 else
7909 goto fail;
7912 return TRUE;
7914 fail:
7915 (*_bfd_error_handler)
7916 (_("%B: unsupported relocation type %s"),
7917 abfd, areloc->howto->name);
7918 bfd_set_error (bfd_error_bad_value);
7919 return FALSE;
7922 bfd_boolean
7923 _bfd_elf_close_and_cleanup (bfd *abfd)
7925 struct elf_obj_tdata *tdata = elf_tdata (abfd);
7926 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
7928 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
7929 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7930 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
7933 return _bfd_generic_close_and_cleanup (abfd);
7936 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7937 in the relocation's offset. Thus we cannot allow any sort of sanity
7938 range-checking to interfere. There is nothing else to do in processing
7939 this reloc. */
7941 bfd_reloc_status_type
7942 _bfd_elf_rel_vtable_reloc_fn
7943 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7944 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7945 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7946 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7948 return bfd_reloc_ok;
7951 /* Elf core file support. Much of this only works on native
7952 toolchains, since we rely on knowing the
7953 machine-dependent procfs structure in order to pick
7954 out details about the corefile. */
7956 #ifdef HAVE_SYS_PROCFS_H
7957 /* Needed for new procfs interface on sparc-solaris. */
7958 # define _STRUCTURED_PROC 1
7959 # include <sys/procfs.h>
7960 #endif
7962 /* Return a PID that identifies a "thread" for threaded cores, or the
7963 PID of the main process for non-threaded cores. */
7965 static int
7966 elfcore_make_pid (bfd *abfd)
7968 int pid;
7970 pid = elf_tdata (abfd)->core->lwpid;
7971 if (pid == 0)
7972 pid = elf_tdata (abfd)->core->pid;
7974 return pid;
7977 /* If there isn't a section called NAME, make one, using
7978 data from SECT. Note, this function will generate a
7979 reference to NAME, so you shouldn't deallocate or
7980 overwrite it. */
7982 static bfd_boolean
7983 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7985 asection *sect2;
7987 if (bfd_get_section_by_name (abfd, name) != NULL)
7988 return TRUE;
7990 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7991 if (sect2 == NULL)
7992 return FALSE;
7994 sect2->size = sect->size;
7995 sect2->filepos = sect->filepos;
7996 sect2->alignment_power = sect->alignment_power;
7997 return TRUE;
8000 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8001 actually creates up to two pseudosections:
8002 - For the single-threaded case, a section named NAME, unless
8003 such a section already exists.
8004 - For the multi-threaded case, a section named "NAME/PID", where
8005 PID is elfcore_make_pid (abfd).
8006 Both pseudosections have identical contents. */
8007 bfd_boolean
8008 _bfd_elfcore_make_pseudosection (bfd *abfd,
8009 char *name,
8010 size_t size,
8011 ufile_ptr filepos)
8013 char buf[100];
8014 char *threaded_name;
8015 size_t len;
8016 asection *sect;
8018 /* Build the section name. */
8020 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
8021 len = strlen (buf) + 1;
8022 threaded_name = (char *) bfd_alloc (abfd, len);
8023 if (threaded_name == NULL)
8024 return FALSE;
8025 memcpy (threaded_name, buf, len);
8027 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
8028 SEC_HAS_CONTENTS);
8029 if (sect == NULL)
8030 return FALSE;
8031 sect->size = size;
8032 sect->filepos = filepos;
8033 sect->alignment_power = 2;
8035 return elfcore_maybe_make_sect (abfd, name, sect);
8038 /* prstatus_t exists on:
8039 solaris 2.5+
8040 linux 2.[01] + glibc
8041 unixware 4.2
8044 #if defined (HAVE_PRSTATUS_T)
8046 static bfd_boolean
8047 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
8049 size_t size;
8050 int offset;
8052 if (note->descsz == sizeof (prstatus_t))
8054 prstatus_t prstat;
8056 size = sizeof (prstat.pr_reg);
8057 offset = offsetof (prstatus_t, pr_reg);
8058 memcpy (&prstat, note->descdata, sizeof (prstat));
8060 /* Do not overwrite the core signal if it
8061 has already been set by another thread. */
8062 if (elf_tdata (abfd)->core->signal == 0)
8063 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8064 if (elf_tdata (abfd)->core->pid == 0)
8065 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8067 /* pr_who exists on:
8068 solaris 2.5+
8069 unixware 4.2
8070 pr_who doesn't exist on:
8071 linux 2.[01]
8073 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8074 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8075 #else
8076 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8077 #endif
8079 #if defined (HAVE_PRSTATUS32_T)
8080 else if (note->descsz == sizeof (prstatus32_t))
8082 /* 64-bit host, 32-bit corefile */
8083 prstatus32_t prstat;
8085 size = sizeof (prstat.pr_reg);
8086 offset = offsetof (prstatus32_t, pr_reg);
8087 memcpy (&prstat, note->descdata, sizeof (prstat));
8089 /* Do not overwrite the core signal if it
8090 has already been set by another thread. */
8091 if (elf_tdata (abfd)->core->signal == 0)
8092 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8093 if (elf_tdata (abfd)->core->pid == 0)
8094 elf_tdata (abfd)->core->pid = prstat.pr_pid;
8096 /* pr_who exists on:
8097 solaris 2.5+
8098 unixware 4.2
8099 pr_who doesn't exist on:
8100 linux 2.[01]
8102 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8103 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8104 #else
8105 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8106 #endif
8108 #endif /* HAVE_PRSTATUS32_T */
8109 else
8111 /* Fail - we don't know how to handle any other
8112 note size (ie. data object type). */
8113 return TRUE;
8116 /* Make a ".reg/999" section and a ".reg" section. */
8117 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
8118 size, note->descpos + offset);
8120 #endif /* defined (HAVE_PRSTATUS_T) */
8122 /* Create a pseudosection containing the exact contents of NOTE. */
8123 static bfd_boolean
8124 elfcore_make_note_pseudosection (bfd *abfd,
8125 char *name,
8126 Elf_Internal_Note *note)
8128 return _bfd_elfcore_make_pseudosection (abfd, name,
8129 note->descsz, note->descpos);
8132 /* There isn't a consistent prfpregset_t across platforms,
8133 but it doesn't matter, because we don't have to pick this
8134 data structure apart. */
8136 static bfd_boolean
8137 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
8139 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8142 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8143 type of NT_PRXFPREG. Just include the whole note's contents
8144 literally. */
8146 static bfd_boolean
8147 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
8149 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8152 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8153 with a note type of NT_X86_XSTATE. Just include the whole note's
8154 contents literally. */
8156 static bfd_boolean
8157 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
8159 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
8162 static bfd_boolean
8163 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
8165 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
8168 static bfd_boolean
8169 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
8171 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
8174 static bfd_boolean
8175 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
8177 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
8180 static bfd_boolean
8181 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
8183 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
8186 static bfd_boolean
8187 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
8189 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
8192 static bfd_boolean
8193 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
8195 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
8198 static bfd_boolean
8199 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
8201 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
8204 static bfd_boolean
8205 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
8207 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
8210 static bfd_boolean
8211 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
8213 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
8216 static bfd_boolean
8217 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
8219 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
8222 static bfd_boolean
8223 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
8225 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
8228 static bfd_boolean
8229 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
8231 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
8234 static bfd_boolean
8235 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
8237 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
8240 static bfd_boolean
8241 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
8243 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
8246 static bfd_boolean
8247 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
8249 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
8252 #if defined (HAVE_PRPSINFO_T)
8253 typedef prpsinfo_t elfcore_psinfo_t;
8254 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8255 typedef prpsinfo32_t elfcore_psinfo32_t;
8256 #endif
8257 #endif
8259 #if defined (HAVE_PSINFO_T)
8260 typedef psinfo_t elfcore_psinfo_t;
8261 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8262 typedef psinfo32_t elfcore_psinfo32_t;
8263 #endif
8264 #endif
8266 /* return a malloc'ed copy of a string at START which is at
8267 most MAX bytes long, possibly without a terminating '\0'.
8268 the copy will always have a terminating '\0'. */
8270 char *
8271 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
8273 char *dups;
8274 char *end = (char *) memchr (start, '\0', max);
8275 size_t len;
8277 if (end == NULL)
8278 len = max;
8279 else
8280 len = end - start;
8282 dups = (char *) bfd_alloc (abfd, len + 1);
8283 if (dups == NULL)
8284 return NULL;
8286 memcpy (dups, start, len);
8287 dups[len] = '\0';
8289 return dups;
8292 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8293 static bfd_boolean
8294 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
8296 if (note->descsz == sizeof (elfcore_psinfo_t))
8298 elfcore_psinfo_t psinfo;
8300 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8302 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8303 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8304 #endif
8305 elf_tdata (abfd)->core->program
8306 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8307 sizeof (psinfo.pr_fname));
8309 elf_tdata (abfd)->core->command
8310 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8311 sizeof (psinfo.pr_psargs));
8313 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8314 else if (note->descsz == sizeof (elfcore_psinfo32_t))
8316 /* 64-bit host, 32-bit corefile */
8317 elfcore_psinfo32_t psinfo;
8319 memcpy (&psinfo, note->descdata, sizeof (psinfo));
8321 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8322 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8323 #endif
8324 elf_tdata (abfd)->core->program
8325 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8326 sizeof (psinfo.pr_fname));
8328 elf_tdata (abfd)->core->command
8329 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8330 sizeof (psinfo.pr_psargs));
8332 #endif
8334 else
8336 /* Fail - we don't know how to handle any other
8337 note size (ie. data object type). */
8338 return TRUE;
8341 /* Note that for some reason, a spurious space is tacked
8342 onto the end of the args in some (at least one anyway)
8343 implementations, so strip it off if it exists. */
8346 char *command = elf_tdata (abfd)->core->command;
8347 int n = strlen (command);
8349 if (0 < n && command[n - 1] == ' ')
8350 command[n - 1] = '\0';
8353 return TRUE;
8355 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8357 #if defined (HAVE_PSTATUS_T)
8358 static bfd_boolean
8359 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8361 if (note->descsz == sizeof (pstatus_t)
8362 #if defined (HAVE_PXSTATUS_T)
8363 || note->descsz == sizeof (pxstatus_t)
8364 #endif
8367 pstatus_t pstat;
8369 memcpy (&pstat, note->descdata, sizeof (pstat));
8371 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8373 #if defined (HAVE_PSTATUS32_T)
8374 else if (note->descsz == sizeof (pstatus32_t))
8376 /* 64-bit host, 32-bit corefile */
8377 pstatus32_t pstat;
8379 memcpy (&pstat, note->descdata, sizeof (pstat));
8381 elf_tdata (abfd)->core->pid = pstat.pr_pid;
8383 #endif
8384 /* Could grab some more details from the "representative"
8385 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8386 NT_LWPSTATUS note, presumably. */
8388 return TRUE;
8390 #endif /* defined (HAVE_PSTATUS_T) */
8392 #if defined (HAVE_LWPSTATUS_T)
8393 static bfd_boolean
8394 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8396 lwpstatus_t lwpstat;
8397 char buf[100];
8398 char *name;
8399 size_t len;
8400 asection *sect;
8402 if (note->descsz != sizeof (lwpstat)
8403 #if defined (HAVE_LWPXSTATUS_T)
8404 && note->descsz != sizeof (lwpxstatus_t)
8405 #endif
8407 return TRUE;
8409 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8411 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
8412 /* Do not overwrite the core signal if it has already been set by
8413 another thread. */
8414 if (elf_tdata (abfd)->core->signal == 0)
8415 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
8417 /* Make a ".reg/999" section. */
8419 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8420 len = strlen (buf) + 1;
8421 name = bfd_alloc (abfd, len);
8422 if (name == NULL)
8423 return FALSE;
8424 memcpy (name, buf, len);
8426 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8427 if (sect == NULL)
8428 return FALSE;
8430 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8431 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8432 sect->filepos = note->descpos
8433 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8434 #endif
8436 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8437 sect->size = sizeof (lwpstat.pr_reg);
8438 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8439 #endif
8441 sect->alignment_power = 2;
8443 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8444 return FALSE;
8446 /* Make a ".reg2/999" section */
8448 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8449 len = strlen (buf) + 1;
8450 name = bfd_alloc (abfd, len);
8451 if (name == NULL)
8452 return FALSE;
8453 memcpy (name, buf, len);
8455 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8456 if (sect == NULL)
8457 return FALSE;
8459 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8460 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8461 sect->filepos = note->descpos
8462 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8463 #endif
8465 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8466 sect->size = sizeof (lwpstat.pr_fpreg);
8467 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8468 #endif
8470 sect->alignment_power = 2;
8472 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8474 #endif /* defined (HAVE_LWPSTATUS_T) */
8476 static bfd_boolean
8477 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8479 char buf[30];
8480 char *name;
8481 size_t len;
8482 asection *sect;
8483 int type;
8484 int is_active_thread;
8485 bfd_vma base_addr;
8487 if (note->descsz < 728)
8488 return TRUE;
8490 if (! CONST_STRNEQ (note->namedata, "win32"))
8491 return TRUE;
8493 type = bfd_get_32 (abfd, note->descdata);
8495 switch (type)
8497 case 1 /* NOTE_INFO_PROCESS */:
8498 /* FIXME: need to add ->core->command. */
8499 /* process_info.pid */
8500 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
8501 /* process_info.signal */
8502 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
8503 break;
8505 case 2 /* NOTE_INFO_THREAD */:
8506 /* Make a ".reg/999" section. */
8507 /* thread_info.tid */
8508 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8510 len = strlen (buf) + 1;
8511 name = (char *) bfd_alloc (abfd, len);
8512 if (name == NULL)
8513 return FALSE;
8515 memcpy (name, buf, len);
8517 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8518 if (sect == NULL)
8519 return FALSE;
8521 /* sizeof (thread_info.thread_context) */
8522 sect->size = 716;
8523 /* offsetof (thread_info.thread_context) */
8524 sect->filepos = note->descpos + 12;
8525 sect->alignment_power = 2;
8527 /* thread_info.is_active_thread */
8528 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8530 if (is_active_thread)
8531 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8532 return FALSE;
8533 break;
8535 case 3 /* NOTE_INFO_MODULE */:
8536 /* Make a ".module/xxxxxxxx" section. */
8537 /* module_info.base_address */
8538 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8539 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8541 len = strlen (buf) + 1;
8542 name = (char *) bfd_alloc (abfd, len);
8543 if (name == NULL)
8544 return FALSE;
8546 memcpy (name, buf, len);
8548 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8550 if (sect == NULL)
8551 return FALSE;
8553 sect->size = note->descsz;
8554 sect->filepos = note->descpos;
8555 sect->alignment_power = 2;
8556 break;
8558 default:
8559 return TRUE;
8562 return TRUE;
8565 static bfd_boolean
8566 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8568 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8570 switch (note->type)
8572 default:
8573 return TRUE;
8575 case NT_PRSTATUS:
8576 if (bed->elf_backend_grok_prstatus)
8577 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8578 return TRUE;
8579 #if defined (HAVE_PRSTATUS_T)
8580 return elfcore_grok_prstatus (abfd, note);
8581 #else
8582 return TRUE;
8583 #endif
8585 #if defined (HAVE_PSTATUS_T)
8586 case NT_PSTATUS:
8587 return elfcore_grok_pstatus (abfd, note);
8588 #endif
8590 #if defined (HAVE_LWPSTATUS_T)
8591 case NT_LWPSTATUS:
8592 return elfcore_grok_lwpstatus (abfd, note);
8593 #endif
8595 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8596 return elfcore_grok_prfpreg (abfd, note);
8598 case NT_WIN32PSTATUS:
8599 return elfcore_grok_win32pstatus (abfd, note);
8601 case NT_PRXFPREG: /* Linux SSE extension */
8602 if (note->namesz == 6
8603 && strcmp (note->namedata, "LINUX") == 0)
8604 return elfcore_grok_prxfpreg (abfd, note);
8605 else
8606 return TRUE;
8608 case NT_X86_XSTATE: /* Linux XSAVE extension */
8609 if (note->namesz == 6
8610 && strcmp (note->namedata, "LINUX") == 0)
8611 return elfcore_grok_xstatereg (abfd, note);
8612 else
8613 return TRUE;
8615 case NT_PPC_VMX:
8616 if (note->namesz == 6
8617 && strcmp (note->namedata, "LINUX") == 0)
8618 return elfcore_grok_ppc_vmx (abfd, note);
8619 else
8620 return TRUE;
8622 case NT_PPC_VSX:
8623 if (note->namesz == 6
8624 && strcmp (note->namedata, "LINUX") == 0)
8625 return elfcore_grok_ppc_vsx (abfd, note);
8626 else
8627 return TRUE;
8629 case NT_S390_HIGH_GPRS:
8630 if (note->namesz == 6
8631 && strcmp (note->namedata, "LINUX") == 0)
8632 return elfcore_grok_s390_high_gprs (abfd, note);
8633 else
8634 return TRUE;
8636 case NT_S390_TIMER:
8637 if (note->namesz == 6
8638 && strcmp (note->namedata, "LINUX") == 0)
8639 return elfcore_grok_s390_timer (abfd, note);
8640 else
8641 return TRUE;
8643 case NT_S390_TODCMP:
8644 if (note->namesz == 6
8645 && strcmp (note->namedata, "LINUX") == 0)
8646 return elfcore_grok_s390_todcmp (abfd, note);
8647 else
8648 return TRUE;
8650 case NT_S390_TODPREG:
8651 if (note->namesz == 6
8652 && strcmp (note->namedata, "LINUX") == 0)
8653 return elfcore_grok_s390_todpreg (abfd, note);
8654 else
8655 return TRUE;
8657 case NT_S390_CTRS:
8658 if (note->namesz == 6
8659 && strcmp (note->namedata, "LINUX") == 0)
8660 return elfcore_grok_s390_ctrs (abfd, note);
8661 else
8662 return TRUE;
8664 case NT_S390_PREFIX:
8665 if (note->namesz == 6
8666 && strcmp (note->namedata, "LINUX") == 0)
8667 return elfcore_grok_s390_prefix (abfd, note);
8668 else
8669 return TRUE;
8671 case NT_S390_LAST_BREAK:
8672 if (note->namesz == 6
8673 && strcmp (note->namedata, "LINUX") == 0)
8674 return elfcore_grok_s390_last_break (abfd, note);
8675 else
8676 return TRUE;
8678 case NT_S390_SYSTEM_CALL:
8679 if (note->namesz == 6
8680 && strcmp (note->namedata, "LINUX") == 0)
8681 return elfcore_grok_s390_system_call (abfd, note);
8682 else
8683 return TRUE;
8685 case NT_S390_TDB:
8686 if (note->namesz == 6
8687 && strcmp (note->namedata, "LINUX") == 0)
8688 return elfcore_grok_s390_tdb (abfd, note);
8689 else
8690 return TRUE;
8692 case NT_ARM_VFP:
8693 if (note->namesz == 6
8694 && strcmp (note->namedata, "LINUX") == 0)
8695 return elfcore_grok_arm_vfp (abfd, note);
8696 else
8697 return TRUE;
8699 case NT_ARM_TLS:
8700 if (note->namesz == 6
8701 && strcmp (note->namedata, "LINUX") == 0)
8702 return elfcore_grok_aarch_tls (abfd, note);
8703 else
8704 return TRUE;
8706 case NT_ARM_HW_BREAK:
8707 if (note->namesz == 6
8708 && strcmp (note->namedata, "LINUX") == 0)
8709 return elfcore_grok_aarch_hw_break (abfd, note);
8710 else
8711 return TRUE;
8713 case NT_ARM_HW_WATCH:
8714 if (note->namesz == 6
8715 && strcmp (note->namedata, "LINUX") == 0)
8716 return elfcore_grok_aarch_hw_watch (abfd, note);
8717 else
8718 return TRUE;
8720 case NT_PRPSINFO:
8721 case NT_PSINFO:
8722 if (bed->elf_backend_grok_psinfo)
8723 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8724 return TRUE;
8725 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8726 return elfcore_grok_psinfo (abfd, note);
8727 #else
8728 return TRUE;
8729 #endif
8731 case NT_AUXV:
8733 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8734 SEC_HAS_CONTENTS);
8736 if (sect == NULL)
8737 return FALSE;
8738 sect->size = note->descsz;
8739 sect->filepos = note->descpos;
8740 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8742 return TRUE;
8745 case NT_FILE:
8746 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
8747 note);
8749 case NT_SIGINFO:
8750 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
8751 note);
8755 static bfd_boolean
8756 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8758 struct elf_obj_tdata *t;
8760 if (note->descsz == 0)
8761 return FALSE;
8763 t = elf_tdata (abfd);
8764 t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz);
8765 if (t->build_id == NULL)
8766 return FALSE;
8768 t->build_id->size = note->descsz;
8769 memcpy (t->build_id->data, note->descdata, note->descsz);
8771 return TRUE;
8774 static bfd_boolean
8775 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8777 switch (note->type)
8779 default:
8780 return TRUE;
8782 case NT_GNU_BUILD_ID:
8783 return elfobj_grok_gnu_build_id (abfd, note);
8787 static bfd_boolean
8788 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
8790 struct sdt_note *cur =
8791 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
8792 + note->descsz);
8794 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
8795 cur->size = (bfd_size_type) note->descsz;
8796 memcpy (cur->data, note->descdata, note->descsz);
8798 elf_tdata (abfd)->sdt_note_head = cur;
8800 return TRUE;
8803 static bfd_boolean
8804 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
8806 switch (note->type)
8808 case NT_STAPSDT:
8809 return elfobj_grok_stapsdt_note_1 (abfd, note);
8811 default:
8812 return TRUE;
8816 static bfd_boolean
8817 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8819 char *cp;
8821 cp = strchr (note->namedata, '@');
8822 if (cp != NULL)
8824 *lwpidp = atoi(cp + 1);
8825 return TRUE;
8827 return FALSE;
8830 static bfd_boolean
8831 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8833 /* Signal number at offset 0x08. */
8834 elf_tdata (abfd)->core->signal
8835 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8837 /* Process ID at offset 0x50. */
8838 elf_tdata (abfd)->core->pid
8839 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8841 /* Command name at 0x7c (max 32 bytes, including nul). */
8842 elf_tdata (abfd)->core->command
8843 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8845 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8846 note);
8849 static bfd_boolean
8850 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8852 int lwp;
8854 if (elfcore_netbsd_get_lwpid (note, &lwp))
8855 elf_tdata (abfd)->core->lwpid = lwp;
8857 if (note->type == NT_NETBSDCORE_PROCINFO)
8859 /* NetBSD-specific core "procinfo". Note that we expect to
8860 find this note before any of the others, which is fine,
8861 since the kernel writes this note out first when it
8862 creates a core file. */
8864 return elfcore_grok_netbsd_procinfo (abfd, note);
8867 /* As of Jan 2002 there are no other machine-independent notes
8868 defined for NetBSD core files. If the note type is less
8869 than the start of the machine-dependent note types, we don't
8870 understand it. */
8872 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8873 return TRUE;
8876 switch (bfd_get_arch (abfd))
8878 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8879 PT_GETFPREGS == mach+2. */
8881 case bfd_arch_alpha:
8882 case bfd_arch_sparc:
8883 switch (note->type)
8885 case NT_NETBSDCORE_FIRSTMACH+0:
8886 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8888 case NT_NETBSDCORE_FIRSTMACH+2:
8889 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8891 default:
8892 return TRUE;
8895 /* On all other arch's, PT_GETREGS == mach+1 and
8896 PT_GETFPREGS == mach+3. */
8898 default:
8899 switch (note->type)
8901 case NT_NETBSDCORE_FIRSTMACH+1:
8902 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8904 case NT_NETBSDCORE_FIRSTMACH+3:
8905 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8907 default:
8908 return TRUE;
8911 /* NOTREACHED */
8914 static bfd_boolean
8915 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8917 /* Signal number at offset 0x08. */
8918 elf_tdata (abfd)->core->signal
8919 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8921 /* Process ID at offset 0x20. */
8922 elf_tdata (abfd)->core->pid
8923 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8925 /* Command name at 0x48 (max 32 bytes, including nul). */
8926 elf_tdata (abfd)->core->command
8927 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8929 return TRUE;
8932 static bfd_boolean
8933 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8935 if (note->type == NT_OPENBSD_PROCINFO)
8936 return elfcore_grok_openbsd_procinfo (abfd, note);
8938 if (note->type == NT_OPENBSD_REGS)
8939 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8941 if (note->type == NT_OPENBSD_FPREGS)
8942 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8944 if (note->type == NT_OPENBSD_XFPREGS)
8945 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8947 if (note->type == NT_OPENBSD_AUXV)
8949 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8950 SEC_HAS_CONTENTS);
8952 if (sect == NULL)
8953 return FALSE;
8954 sect->size = note->descsz;
8955 sect->filepos = note->descpos;
8956 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8958 return TRUE;
8961 if (note->type == NT_OPENBSD_WCOOKIE)
8963 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8964 SEC_HAS_CONTENTS);
8966 if (sect == NULL)
8967 return FALSE;
8968 sect->size = note->descsz;
8969 sect->filepos = note->descpos;
8970 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8972 return TRUE;
8975 return TRUE;
8978 static bfd_boolean
8979 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8981 void *ddata = note->descdata;
8982 char buf[100];
8983 char *name;
8984 asection *sect;
8985 short sig;
8986 unsigned flags;
8988 /* nto_procfs_status 'pid' field is at offset 0. */
8989 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8991 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8992 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8994 /* nto_procfs_status 'flags' field is at offset 8. */
8995 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8997 /* nto_procfs_status 'what' field is at offset 14. */
8998 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
9000 elf_tdata (abfd)->core->signal = sig;
9001 elf_tdata (abfd)->core->lwpid = *tid;
9004 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9005 do not come from signals so we make sure we set the current
9006 thread just in case. */
9007 if (flags & 0x00000080)
9008 elf_tdata (abfd)->core->lwpid = *tid;
9010 /* Make a ".qnx_core_status/%d" section. */
9011 sprintf (buf, ".qnx_core_status/%ld", *tid);
9013 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9014 if (name == NULL)
9015 return FALSE;
9016 strcpy (name, buf);
9018 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9019 if (sect == NULL)
9020 return FALSE;
9022 sect->size = note->descsz;
9023 sect->filepos = note->descpos;
9024 sect->alignment_power = 2;
9026 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
9029 static bfd_boolean
9030 elfcore_grok_nto_regs (bfd *abfd,
9031 Elf_Internal_Note *note,
9032 long tid,
9033 char *base)
9035 char buf[100];
9036 char *name;
9037 asection *sect;
9039 /* Make a "(base)/%d" section. */
9040 sprintf (buf, "%s/%ld", base, tid);
9042 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9043 if (name == NULL)
9044 return FALSE;
9045 strcpy (name, buf);
9047 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9048 if (sect == NULL)
9049 return FALSE;
9051 sect->size = note->descsz;
9052 sect->filepos = note->descpos;
9053 sect->alignment_power = 2;
9055 /* This is the current thread. */
9056 if (elf_tdata (abfd)->core->lwpid == tid)
9057 return elfcore_maybe_make_sect (abfd, base, sect);
9059 return TRUE;
9062 #define BFD_QNT_CORE_INFO 7
9063 #define BFD_QNT_CORE_STATUS 8
9064 #define BFD_QNT_CORE_GREG 9
9065 #define BFD_QNT_CORE_FPREG 10
9067 static bfd_boolean
9068 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
9070 /* Every GREG section has a STATUS section before it. Store the
9071 tid from the previous call to pass down to the next gregs
9072 function. */
9073 static long tid = 1;
9075 switch (note->type)
9077 case BFD_QNT_CORE_INFO:
9078 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
9079 case BFD_QNT_CORE_STATUS:
9080 return elfcore_grok_nto_status (abfd, note, &tid);
9081 case BFD_QNT_CORE_GREG:
9082 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
9083 case BFD_QNT_CORE_FPREG:
9084 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
9085 default:
9086 return TRUE;
9090 static bfd_boolean
9091 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
9093 char *name;
9094 asection *sect;
9095 size_t len;
9097 /* Use note name as section name. */
9098 len = note->namesz;
9099 name = (char *) bfd_alloc (abfd, len);
9100 if (name == NULL)
9101 return FALSE;
9102 memcpy (name, note->namedata, len);
9103 name[len - 1] = '\0';
9105 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9106 if (sect == NULL)
9107 return FALSE;
9109 sect->size = note->descsz;
9110 sect->filepos = note->descpos;
9111 sect->alignment_power = 1;
9113 return TRUE;
9116 /* Function: elfcore_write_note
9118 Inputs:
9119 buffer to hold note, and current size of buffer
9120 name of note
9121 type of note
9122 data for note
9123 size of data for note
9125 Writes note to end of buffer. ELF64 notes are written exactly as
9126 for ELF32, despite the current (as of 2006) ELF gabi specifying
9127 that they ought to have 8-byte namesz and descsz field, and have
9128 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9130 Return:
9131 Pointer to realloc'd buffer, *BUFSIZ updated. */
9133 char *
9134 elfcore_write_note (bfd *abfd,
9135 char *buf,
9136 int *bufsiz,
9137 const char *name,
9138 int type,
9139 const void *input,
9140 int size)
9142 Elf_External_Note *xnp;
9143 size_t namesz;
9144 size_t newspace;
9145 char *dest;
9147 namesz = 0;
9148 if (name != NULL)
9149 namesz = strlen (name) + 1;
9151 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
9153 buf = (char *) realloc (buf, *bufsiz + newspace);
9154 if (buf == NULL)
9155 return buf;
9156 dest = buf + *bufsiz;
9157 *bufsiz += newspace;
9158 xnp = (Elf_External_Note *) dest;
9159 H_PUT_32 (abfd, namesz, xnp->namesz);
9160 H_PUT_32 (abfd, size, xnp->descsz);
9161 H_PUT_32 (abfd, type, xnp->type);
9162 dest = xnp->name;
9163 if (name != NULL)
9165 memcpy (dest, name, namesz);
9166 dest += namesz;
9167 while (namesz & 3)
9169 *dest++ = '\0';
9170 ++namesz;
9173 memcpy (dest, input, size);
9174 dest += size;
9175 while (size & 3)
9177 *dest++ = '\0';
9178 ++size;
9180 return buf;
9183 char *
9184 elfcore_write_prpsinfo (bfd *abfd,
9185 char *buf,
9186 int *bufsiz,
9187 const char *fname,
9188 const char *psargs)
9190 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9192 if (bed->elf_backend_write_core_note != NULL)
9194 char *ret;
9195 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9196 NT_PRPSINFO, fname, psargs);
9197 if (ret != NULL)
9198 return ret;
9201 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9202 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9203 if (bed->s->elfclass == ELFCLASS32)
9205 #if defined (HAVE_PSINFO32_T)
9206 psinfo32_t data;
9207 int note_type = NT_PSINFO;
9208 #else
9209 prpsinfo32_t data;
9210 int note_type = NT_PRPSINFO;
9211 #endif
9213 memset (&data, 0, sizeof (data));
9214 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9215 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9216 return elfcore_write_note (abfd, buf, bufsiz,
9217 "CORE", note_type, &data, sizeof (data));
9219 else
9220 #endif
9222 #if defined (HAVE_PSINFO_T)
9223 psinfo_t data;
9224 int note_type = NT_PSINFO;
9225 #else
9226 prpsinfo_t data;
9227 int note_type = NT_PRPSINFO;
9228 #endif
9230 memset (&data, 0, sizeof (data));
9231 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9232 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9233 return elfcore_write_note (abfd, buf, bufsiz,
9234 "CORE", note_type, &data, sizeof (data));
9236 #endif /* PSINFO_T or PRPSINFO_T */
9238 free (buf);
9239 return NULL;
9242 char *
9243 elfcore_write_linux_prpsinfo32
9244 (bfd *abfd, char *buf, int *bufsiz,
9245 const struct elf_internal_linux_prpsinfo *prpsinfo)
9247 struct elf_external_linux_prpsinfo32 data;
9249 memset (&data, 0, sizeof (data));
9250 LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
9252 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
9253 &data, sizeof (data));
9256 char *
9257 elfcore_write_linux_prpsinfo64
9258 (bfd *abfd, char *buf, int *bufsiz,
9259 const struct elf_internal_linux_prpsinfo *prpsinfo)
9261 struct elf_external_linux_prpsinfo64 data;
9263 memset (&data, 0, sizeof (data));
9264 LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
9266 return elfcore_write_note (abfd, buf, bufsiz,
9267 "CORE", NT_PRPSINFO, &data, sizeof (data));
9270 char *
9271 elfcore_write_prstatus (bfd *abfd,
9272 char *buf,
9273 int *bufsiz,
9274 long pid,
9275 int cursig,
9276 const void *gregs)
9278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9280 if (bed->elf_backend_write_core_note != NULL)
9282 char *ret;
9283 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9284 NT_PRSTATUS,
9285 pid, cursig, gregs);
9286 if (ret != NULL)
9287 return ret;
9290 #if defined (HAVE_PRSTATUS_T)
9291 #if defined (HAVE_PRSTATUS32_T)
9292 if (bed->s->elfclass == ELFCLASS32)
9294 prstatus32_t prstat;
9296 memset (&prstat, 0, sizeof (prstat));
9297 prstat.pr_pid = pid;
9298 prstat.pr_cursig = cursig;
9299 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9300 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9301 NT_PRSTATUS, &prstat, sizeof (prstat));
9303 else
9304 #endif
9306 prstatus_t prstat;
9308 memset (&prstat, 0, sizeof (prstat));
9309 prstat.pr_pid = pid;
9310 prstat.pr_cursig = cursig;
9311 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9312 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9313 NT_PRSTATUS, &prstat, sizeof (prstat));
9315 #endif /* HAVE_PRSTATUS_T */
9317 free (buf);
9318 return NULL;
9321 #if defined (HAVE_LWPSTATUS_T)
9322 char *
9323 elfcore_write_lwpstatus (bfd *abfd,
9324 char *buf,
9325 int *bufsiz,
9326 long pid,
9327 int cursig,
9328 const void *gregs)
9330 lwpstatus_t lwpstat;
9331 const char *note_name = "CORE";
9333 memset (&lwpstat, 0, sizeof (lwpstat));
9334 lwpstat.pr_lwpid = pid >> 16;
9335 lwpstat.pr_cursig = cursig;
9336 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9337 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
9338 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9339 #if !defined(gregs)
9340 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
9341 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
9342 #else
9343 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
9344 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
9345 #endif
9346 #endif
9347 return elfcore_write_note (abfd, buf, bufsiz, note_name,
9348 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
9350 #endif /* HAVE_LWPSTATUS_T */
9352 #if defined (HAVE_PSTATUS_T)
9353 char *
9354 elfcore_write_pstatus (bfd *abfd,
9355 char *buf,
9356 int *bufsiz,
9357 long pid,
9358 int cursig ATTRIBUTE_UNUSED,
9359 const void *gregs ATTRIBUTE_UNUSED)
9361 const char *note_name = "CORE";
9362 #if defined (HAVE_PSTATUS32_T)
9363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9365 if (bed->s->elfclass == ELFCLASS32)
9367 pstatus32_t pstat;
9369 memset (&pstat, 0, sizeof (pstat));
9370 pstat.pr_pid = pid & 0xffff;
9371 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9372 NT_PSTATUS, &pstat, sizeof (pstat));
9373 return buf;
9375 else
9376 #endif
9378 pstatus_t pstat;
9380 memset (&pstat, 0, sizeof (pstat));
9381 pstat.pr_pid = pid & 0xffff;
9382 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9383 NT_PSTATUS, &pstat, sizeof (pstat));
9384 return buf;
9387 #endif /* HAVE_PSTATUS_T */
9389 char *
9390 elfcore_write_prfpreg (bfd *abfd,
9391 char *buf,
9392 int *bufsiz,
9393 const void *fpregs,
9394 int size)
9396 const char *note_name = "CORE";
9397 return elfcore_write_note (abfd, buf, bufsiz,
9398 note_name, NT_FPREGSET, fpregs, size);
9401 char *
9402 elfcore_write_prxfpreg (bfd *abfd,
9403 char *buf,
9404 int *bufsiz,
9405 const void *xfpregs,
9406 int size)
9408 char *note_name = "LINUX";
9409 return elfcore_write_note (abfd, buf, bufsiz,
9410 note_name, NT_PRXFPREG, xfpregs, size);
9413 char *
9414 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
9415 const void *xfpregs, int size)
9417 char *note_name = "LINUX";
9418 return elfcore_write_note (abfd, buf, bufsiz,
9419 note_name, NT_X86_XSTATE, xfpregs, size);
9422 char *
9423 elfcore_write_ppc_vmx (bfd *abfd,
9424 char *buf,
9425 int *bufsiz,
9426 const void *ppc_vmx,
9427 int size)
9429 char *note_name = "LINUX";
9430 return elfcore_write_note (abfd, buf, bufsiz,
9431 note_name, NT_PPC_VMX, ppc_vmx, size);
9434 char *
9435 elfcore_write_ppc_vsx (bfd *abfd,
9436 char *buf,
9437 int *bufsiz,
9438 const void *ppc_vsx,
9439 int size)
9441 char *note_name = "LINUX";
9442 return elfcore_write_note (abfd, buf, bufsiz,
9443 note_name, NT_PPC_VSX, ppc_vsx, size);
9446 static char *
9447 elfcore_write_s390_high_gprs (bfd *abfd,
9448 char *buf,
9449 int *bufsiz,
9450 const void *s390_high_gprs,
9451 int size)
9453 char *note_name = "LINUX";
9454 return elfcore_write_note (abfd, buf, bufsiz,
9455 note_name, NT_S390_HIGH_GPRS,
9456 s390_high_gprs, size);
9459 char *
9460 elfcore_write_s390_timer (bfd *abfd,
9461 char *buf,
9462 int *bufsiz,
9463 const void *s390_timer,
9464 int size)
9466 char *note_name = "LINUX";
9467 return elfcore_write_note (abfd, buf, bufsiz,
9468 note_name, NT_S390_TIMER, s390_timer, size);
9471 char *
9472 elfcore_write_s390_todcmp (bfd *abfd,
9473 char *buf,
9474 int *bufsiz,
9475 const void *s390_todcmp,
9476 int size)
9478 char *note_name = "LINUX";
9479 return elfcore_write_note (abfd, buf, bufsiz,
9480 note_name, NT_S390_TODCMP, s390_todcmp, size);
9483 char *
9484 elfcore_write_s390_todpreg (bfd *abfd,
9485 char *buf,
9486 int *bufsiz,
9487 const void *s390_todpreg,
9488 int size)
9490 char *note_name = "LINUX";
9491 return elfcore_write_note (abfd, buf, bufsiz,
9492 note_name, NT_S390_TODPREG, s390_todpreg, size);
9495 char *
9496 elfcore_write_s390_ctrs (bfd *abfd,
9497 char *buf,
9498 int *bufsiz,
9499 const void *s390_ctrs,
9500 int size)
9502 char *note_name = "LINUX";
9503 return elfcore_write_note (abfd, buf, bufsiz,
9504 note_name, NT_S390_CTRS, s390_ctrs, size);
9507 char *
9508 elfcore_write_s390_prefix (bfd *abfd,
9509 char *buf,
9510 int *bufsiz,
9511 const void *s390_prefix,
9512 int size)
9514 char *note_name = "LINUX";
9515 return elfcore_write_note (abfd, buf, bufsiz,
9516 note_name, NT_S390_PREFIX, s390_prefix, size);
9519 char *
9520 elfcore_write_s390_last_break (bfd *abfd,
9521 char *buf,
9522 int *bufsiz,
9523 const void *s390_last_break,
9524 int size)
9526 char *note_name = "LINUX";
9527 return elfcore_write_note (abfd, buf, bufsiz,
9528 note_name, NT_S390_LAST_BREAK,
9529 s390_last_break, size);
9532 char *
9533 elfcore_write_s390_system_call (bfd *abfd,
9534 char *buf,
9535 int *bufsiz,
9536 const void *s390_system_call,
9537 int size)
9539 char *note_name = "LINUX";
9540 return elfcore_write_note (abfd, buf, bufsiz,
9541 note_name, NT_S390_SYSTEM_CALL,
9542 s390_system_call, size);
9545 char *
9546 elfcore_write_s390_tdb (bfd *abfd,
9547 char *buf,
9548 int *bufsiz,
9549 const void *s390_tdb,
9550 int size)
9552 char *note_name = "LINUX";
9553 return elfcore_write_note (abfd, buf, bufsiz,
9554 note_name, NT_S390_TDB, s390_tdb, size);
9557 char *
9558 elfcore_write_arm_vfp (bfd *abfd,
9559 char *buf,
9560 int *bufsiz,
9561 const void *arm_vfp,
9562 int size)
9564 char *note_name = "LINUX";
9565 return elfcore_write_note (abfd, buf, bufsiz,
9566 note_name, NT_ARM_VFP, arm_vfp, size);
9569 char *
9570 elfcore_write_aarch_tls (bfd *abfd,
9571 char *buf,
9572 int *bufsiz,
9573 const void *aarch_tls,
9574 int size)
9576 char *note_name = "LINUX";
9577 return elfcore_write_note (abfd, buf, bufsiz,
9578 note_name, NT_ARM_TLS, aarch_tls, size);
9581 char *
9582 elfcore_write_aarch_hw_break (bfd *abfd,
9583 char *buf,
9584 int *bufsiz,
9585 const void *aarch_hw_break,
9586 int size)
9588 char *note_name = "LINUX";
9589 return elfcore_write_note (abfd, buf, bufsiz,
9590 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
9593 char *
9594 elfcore_write_aarch_hw_watch (bfd *abfd,
9595 char *buf,
9596 int *bufsiz,
9597 const void *aarch_hw_watch,
9598 int size)
9600 char *note_name = "LINUX";
9601 return elfcore_write_note (abfd, buf, bufsiz,
9602 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
9605 char *
9606 elfcore_write_register_note (bfd *abfd,
9607 char *buf,
9608 int *bufsiz,
9609 const char *section,
9610 const void *data,
9611 int size)
9613 if (strcmp (section, ".reg2") == 0)
9614 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
9615 if (strcmp (section, ".reg-xfp") == 0)
9616 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
9617 if (strcmp (section, ".reg-xstate") == 0)
9618 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
9619 if (strcmp (section, ".reg-ppc-vmx") == 0)
9620 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
9621 if (strcmp (section, ".reg-ppc-vsx") == 0)
9622 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
9623 if (strcmp (section, ".reg-s390-high-gprs") == 0)
9624 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
9625 if (strcmp (section, ".reg-s390-timer") == 0)
9626 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
9627 if (strcmp (section, ".reg-s390-todcmp") == 0)
9628 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
9629 if (strcmp (section, ".reg-s390-todpreg") == 0)
9630 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
9631 if (strcmp (section, ".reg-s390-ctrs") == 0)
9632 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
9633 if (strcmp (section, ".reg-s390-prefix") == 0)
9634 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
9635 if (strcmp (section, ".reg-s390-last-break") == 0)
9636 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
9637 if (strcmp (section, ".reg-s390-system-call") == 0)
9638 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
9639 if (strcmp (section, ".reg-s390-tdb") == 0)
9640 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
9641 if (strcmp (section, ".reg-arm-vfp") == 0)
9642 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
9643 if (strcmp (section, ".reg-aarch-tls") == 0)
9644 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
9645 if (strcmp (section, ".reg-aarch-hw-break") == 0)
9646 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
9647 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
9648 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
9649 return NULL;
9652 static bfd_boolean
9653 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
9655 char *p;
9657 p = buf;
9658 while (p < buf + size)
9660 /* FIXME: bad alignment assumption. */
9661 Elf_External_Note *xnp = (Elf_External_Note *) p;
9662 Elf_Internal_Note in;
9664 if (offsetof (Elf_External_Note, name) > buf - p + size)
9665 return FALSE;
9667 in.type = H_GET_32 (abfd, xnp->type);
9669 in.namesz = H_GET_32 (abfd, xnp->namesz);
9670 in.namedata = xnp->name;
9671 if (in.namesz > buf - in.namedata + size)
9672 return FALSE;
9674 in.descsz = H_GET_32 (abfd, xnp->descsz);
9675 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
9676 in.descpos = offset + (in.descdata - buf);
9677 if (in.descsz != 0
9678 && (in.descdata >= buf + size
9679 || in.descsz > buf - in.descdata + size))
9680 return FALSE;
9682 switch (bfd_get_format (abfd))
9684 default:
9685 return TRUE;
9687 case bfd_core:
9688 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
9690 if (! elfcore_grok_netbsd_note (abfd, &in))
9691 return FALSE;
9693 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
9695 if (! elfcore_grok_openbsd_note (abfd, &in))
9696 return FALSE;
9698 else if (CONST_STRNEQ (in.namedata, "QNX"))
9700 if (! elfcore_grok_nto_note (abfd, &in))
9701 return FALSE;
9703 else if (CONST_STRNEQ (in.namedata, "SPU/"))
9705 if (! elfcore_grok_spu_note (abfd, &in))
9706 return FALSE;
9708 else
9710 if (! elfcore_grok_note (abfd, &in))
9711 return FALSE;
9713 break;
9715 case bfd_object:
9716 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9718 if (! elfobj_grok_gnu_note (abfd, &in))
9719 return FALSE;
9721 else if (in.namesz == sizeof "stapsdt"
9722 && strcmp (in.namedata, "stapsdt") == 0)
9724 if (! elfobj_grok_stapsdt_note (abfd, &in))
9725 return FALSE;
9727 break;
9730 p = in.descdata + BFD_ALIGN (in.descsz, 4);
9733 return TRUE;
9736 static bfd_boolean
9737 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
9739 char *buf;
9741 if (size <= 0)
9742 return TRUE;
9744 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9745 return FALSE;
9747 buf = (char *) bfd_malloc (size);
9748 if (buf == NULL)
9749 return FALSE;
9751 if (bfd_bread (buf, size, abfd) != size
9752 || !elf_parse_notes (abfd, buf, size, offset))
9754 free (buf);
9755 return FALSE;
9758 free (buf);
9759 return TRUE;
9762 /* Providing external access to the ELF program header table. */
9764 /* Return an upper bound on the number of bytes required to store a
9765 copy of ABFD's program header table entries. Return -1 if an error
9766 occurs; bfd_get_error will return an appropriate code. */
9768 long
9769 bfd_get_elf_phdr_upper_bound (bfd *abfd)
9771 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9773 bfd_set_error (bfd_error_wrong_format);
9774 return -1;
9777 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9780 /* Copy ABFD's program header table entries to *PHDRS. The entries
9781 will be stored as an array of Elf_Internal_Phdr structures, as
9782 defined in include/elf/internal.h. To find out how large the
9783 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9785 Return the number of program header table entries read, or -1 if an
9786 error occurs; bfd_get_error will return an appropriate code. */
9789 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9791 int num_phdrs;
9793 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9795 bfd_set_error (bfd_error_wrong_format);
9796 return -1;
9799 num_phdrs = elf_elfheader (abfd)->e_phnum;
9800 memcpy (phdrs, elf_tdata (abfd)->phdr,
9801 num_phdrs * sizeof (Elf_Internal_Phdr));
9803 return num_phdrs;
9806 enum elf_reloc_type_class
9807 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
9808 const asection *rel_sec ATTRIBUTE_UNUSED,
9809 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9811 return reloc_class_normal;
9814 /* For RELA architectures, return the relocation value for a
9815 relocation against a local symbol. */
9817 bfd_vma
9818 _bfd_elf_rela_local_sym (bfd *abfd,
9819 Elf_Internal_Sym *sym,
9820 asection **psec,
9821 Elf_Internal_Rela *rel)
9823 asection *sec = *psec;
9824 bfd_vma relocation;
9826 relocation = (sec->output_section->vma
9827 + sec->output_offset
9828 + sym->st_value);
9829 if ((sec->flags & SEC_MERGE)
9830 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9831 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
9833 rel->r_addend =
9834 _bfd_merged_section_offset (abfd, psec,
9835 elf_section_data (sec)->sec_info,
9836 sym->st_value + rel->r_addend);
9837 if (sec != *psec)
9839 /* If we have changed the section, and our original section is
9840 marked with SEC_EXCLUDE, it means that the original
9841 SEC_MERGE section has been completely subsumed in some
9842 other SEC_MERGE section. In this case, we need to leave
9843 some info around for --emit-relocs. */
9844 if ((sec->flags & SEC_EXCLUDE) != 0)
9845 sec->kept_section = *psec;
9846 sec = *psec;
9848 rel->r_addend -= relocation;
9849 rel->r_addend += sec->output_section->vma + sec->output_offset;
9851 return relocation;
9854 bfd_vma
9855 _bfd_elf_rel_local_sym (bfd *abfd,
9856 Elf_Internal_Sym *sym,
9857 asection **psec,
9858 bfd_vma addend)
9860 asection *sec = *psec;
9862 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
9863 return sym->st_value + addend;
9865 return _bfd_merged_section_offset (abfd, psec,
9866 elf_section_data (sec)->sec_info,
9867 sym->st_value + addend);
9870 bfd_vma
9871 _bfd_elf_section_offset (bfd *abfd,
9872 struct bfd_link_info *info,
9873 asection *sec,
9874 bfd_vma offset)
9876 switch (sec->sec_info_type)
9878 case SEC_INFO_TYPE_STABS:
9879 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9880 offset);
9881 case SEC_INFO_TYPE_EH_FRAME:
9882 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9883 default:
9884 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
9886 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9887 bfd_size_type address_size = bed->s->arch_size / 8;
9888 offset = sec->size - offset - address_size;
9890 return offset;
9894 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9895 reconstruct an ELF file by reading the segments out of remote memory
9896 based on the ELF file header at EHDR_VMA and the ELF program headers it
9897 points to. If not null, *LOADBASEP is filled in with the difference
9898 between the VMAs from which the segments were read, and the VMAs the
9899 file headers (and hence BFD's idea of each section's VMA) put them at.
9901 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9902 remote memory at target address VMA into the local buffer at MYADDR; it
9903 should return zero on success or an `errno' code on failure. TEMPL must
9904 be a BFD for an ELF target with the word size and byte order found in
9905 the remote memory. */
9907 bfd *
9908 bfd_elf_bfd_from_remote_memory
9909 (bfd *templ,
9910 bfd_vma ehdr_vma,
9911 bfd_vma *loadbasep,
9912 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
9914 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9915 (templ, ehdr_vma, loadbasep, target_read_memory);
9918 long
9919 _bfd_elf_get_synthetic_symtab (bfd *abfd,
9920 long symcount ATTRIBUTE_UNUSED,
9921 asymbol **syms ATTRIBUTE_UNUSED,
9922 long dynsymcount,
9923 asymbol **dynsyms,
9924 asymbol **ret)
9926 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9927 asection *relplt;
9928 asymbol *s;
9929 const char *relplt_name;
9930 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
9931 arelent *p;
9932 long count, i, n;
9933 size_t size;
9934 Elf_Internal_Shdr *hdr;
9935 char *names;
9936 asection *plt;
9938 *ret = NULL;
9940 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
9941 return 0;
9943 if (dynsymcount <= 0)
9944 return 0;
9946 if (!bed->plt_sym_val)
9947 return 0;
9949 relplt_name = bed->relplt_name;
9950 if (relplt_name == NULL)
9951 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
9952 relplt = bfd_get_section_by_name (abfd, relplt_name);
9953 if (relplt == NULL)
9954 return 0;
9956 hdr = &elf_section_data (relplt)->this_hdr;
9957 if (hdr->sh_link != elf_dynsymtab (abfd)
9958 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
9959 return 0;
9961 plt = bfd_get_section_by_name (abfd, ".plt");
9962 if (plt == NULL)
9963 return 0;
9965 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9966 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
9967 return -1;
9969 count = relplt->size / hdr->sh_entsize;
9970 size = count * sizeof (asymbol);
9971 p = relplt->relocation;
9972 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9974 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
9975 if (p->addend != 0)
9977 #ifdef BFD64
9978 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
9979 #else
9980 size += sizeof ("+0x") - 1 + 8;
9981 #endif
9985 s = *ret = (asymbol *) bfd_malloc (size);
9986 if (s == NULL)
9987 return -1;
9989 names = (char *) (s + count);
9990 p = relplt->relocation;
9991 n = 0;
9992 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9994 size_t len;
9995 bfd_vma addr;
9997 addr = bed->plt_sym_val (i, plt, p);
9998 if (addr == (bfd_vma) -1)
9999 continue;
10001 *s = **p->sym_ptr_ptr;
10002 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10003 we are defining a symbol, ensure one of them is set. */
10004 if ((s->flags & BSF_LOCAL) == 0)
10005 s->flags |= BSF_GLOBAL;
10006 s->flags |= BSF_SYNTHETIC;
10007 s->section = plt;
10008 s->value = addr - plt->vma;
10009 s->name = names;
10010 s->udata.p = NULL;
10011 len = strlen ((*p->sym_ptr_ptr)->name);
10012 memcpy (names, (*p->sym_ptr_ptr)->name, len);
10013 names += len;
10014 if (p->addend != 0)
10016 char buf[30], *a;
10018 memcpy (names, "+0x", sizeof ("+0x") - 1);
10019 names += sizeof ("+0x") - 1;
10020 bfd_sprintf_vma (abfd, buf, p->addend);
10021 for (a = buf; *a == '0'; ++a)
10023 len = strlen (a);
10024 memcpy (names, a, len);
10025 names += len;
10027 memcpy (names, "@plt", sizeof ("@plt"));
10028 names += sizeof ("@plt");
10029 ++s, ++n;
10032 return n;
10035 /* It is only used by x86-64 so far. */
10036 asection _bfd_elf_large_com_section
10037 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
10038 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
10040 void
10041 _bfd_elf_post_process_headers (bfd * abfd,
10042 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
10044 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
10046 i_ehdrp = elf_elfheader (abfd);
10048 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
10050 /* To make things simpler for the loader on Linux systems we set the
10051 osabi field to ELFOSABI_GNU if the binary contains symbols of
10052 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10053 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
10054 && elf_tdata (abfd)->has_gnu_symbols)
10055 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
10059 /* Return TRUE for ELF symbol types that represent functions.
10060 This is the default version of this function, which is sufficient for
10061 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10063 bfd_boolean
10064 _bfd_elf_is_function_type (unsigned int type)
10066 return (type == STT_FUNC
10067 || type == STT_GNU_IFUNC);
10070 /* If the ELF symbol SYM might be a function in SEC, return the
10071 function size and set *CODE_OFF to the function's entry point,
10072 otherwise return zero. */
10074 bfd_size_type
10075 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
10076 bfd_vma *code_off)
10078 bfd_size_type size;
10080 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10081 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10082 || sym->section != sec)
10083 return 0;
10085 *code_off = sym->value;
10086 size = 0;
10087 if (!(sym->flags & BSF_SYNTHETIC))
10088 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
10089 if (size == 0)
10090 size = 1;
10091 return size;