Update my e-mail address.
[binutils-gdb.git] / bfd / elf.c
blob9cecb249221a114d6bcea3e2c6ecf2090abaecc7
1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2017 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-core.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 elf_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 || bfd_seek (abfd, offset, SEEK_SET) != 0
301 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
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 bfd_release (abfd, shstrtab);
308 shstrtab = NULL;
309 /* Once we've failed to read it, make sure we don't keep
310 trying. Otherwise, we'll keep allocating space for
311 the string table over and over. */
312 i_shdrp[shindex]->sh_size = 0;
314 else
315 shstrtab[shstrtabsize] = '\0';
316 i_shdrp[shindex]->contents = shstrtab;
318 return (char *) shstrtab;
321 char *
322 bfd_elf_string_from_elf_section (bfd *abfd,
323 unsigned int shindex,
324 unsigned int strindex)
326 Elf_Internal_Shdr *hdr;
328 if (strindex == 0)
329 return "";
331 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
332 return NULL;
334 hdr = elf_elfsections (abfd)[shindex];
336 if (hdr->contents == NULL)
338 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
340 /* PR 17512: file: f057ec89. */
341 /* xgettext:c-format */
342 _bfd_error_handler (_("%B: attempt to load strings from"
343 " a non-string section (number %d)"),
344 abfd, shindex);
345 return NULL;
348 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
349 return NULL;
352 if (strindex >= hdr->sh_size)
354 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
355 _bfd_error_handler
356 /* xgettext:c-format */
357 (_("%B: invalid string offset %u >= %Lu for section `%s'"),
358 abfd, strindex, hdr->sh_size,
359 (shindex == shstrndx && strindex == hdr->sh_name
360 ? ".shstrtab"
361 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
362 return NULL;
365 return ((char *) hdr->contents) + strindex;
368 /* Read and convert symbols to internal format.
369 SYMCOUNT specifies the number of symbols to read, starting from
370 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
371 are non-NULL, they are used to store the internal symbols, external
372 symbols, and symbol section index extensions, respectively.
373 Returns a pointer to the internal symbol buffer (malloced if necessary)
374 or NULL if there were no symbols or some kind of problem. */
376 Elf_Internal_Sym *
377 bfd_elf_get_elf_syms (bfd *ibfd,
378 Elf_Internal_Shdr *symtab_hdr,
379 size_t symcount,
380 size_t symoffset,
381 Elf_Internal_Sym *intsym_buf,
382 void *extsym_buf,
383 Elf_External_Sym_Shndx *extshndx_buf)
385 Elf_Internal_Shdr *shndx_hdr;
386 void *alloc_ext;
387 const bfd_byte *esym;
388 Elf_External_Sym_Shndx *alloc_extshndx;
389 Elf_External_Sym_Shndx *shndx;
390 Elf_Internal_Sym *alloc_intsym;
391 Elf_Internal_Sym *isym;
392 Elf_Internal_Sym *isymend;
393 const struct elf_backend_data *bed;
394 size_t extsym_size;
395 bfd_size_type amt;
396 file_ptr pos;
398 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
399 abort ();
401 if (symcount == 0)
402 return intsym_buf;
404 /* Normal syms might have section extension entries. */
405 shndx_hdr = NULL;
406 if (elf_symtab_shndx_list (ibfd) != NULL)
408 elf_section_list * entry;
409 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
411 /* Find an index section that is linked to this symtab section. */
412 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
414 /* PR 20063. */
415 if (entry->hdr.sh_link >= elf_numsections (ibfd))
416 continue;
418 if (sections[entry->hdr.sh_link] == symtab_hdr)
420 shndx_hdr = & entry->hdr;
421 break;
425 if (shndx_hdr == NULL)
427 if (symtab_hdr == & elf_symtab_hdr (ibfd))
428 /* Not really accurate, but this was how the old code used to work. */
429 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
430 /* Otherwise we do nothing. The assumption is that
431 the index table will not be needed. */
435 /* Read the symbols. */
436 alloc_ext = NULL;
437 alloc_extshndx = NULL;
438 alloc_intsym = NULL;
439 bed = get_elf_backend_data (ibfd);
440 extsym_size = bed->s->sizeof_sym;
441 amt = (bfd_size_type) symcount * extsym_size;
442 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
443 if (extsym_buf == NULL)
445 alloc_ext = bfd_malloc2 (symcount, extsym_size);
446 extsym_buf = alloc_ext;
448 if (extsym_buf == NULL
449 || bfd_seek (ibfd, pos, SEEK_SET) != 0
450 || bfd_bread (extsym_buf, amt, ibfd) != amt)
452 intsym_buf = NULL;
453 goto out;
456 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
457 extshndx_buf = NULL;
458 else
460 amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
461 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
462 if (extshndx_buf == NULL)
464 alloc_extshndx = (Elf_External_Sym_Shndx *)
465 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
466 extshndx_buf = alloc_extshndx;
468 if (extshndx_buf == NULL
469 || bfd_seek (ibfd, pos, SEEK_SET) != 0
470 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
472 intsym_buf = NULL;
473 goto out;
477 if (intsym_buf == NULL)
479 alloc_intsym = (Elf_Internal_Sym *)
480 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
481 intsym_buf = alloc_intsym;
482 if (intsym_buf == NULL)
483 goto out;
486 /* Convert the symbols to internal form. */
487 isymend = intsym_buf + symcount;
488 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
489 shndx = extshndx_buf;
490 isym < isymend;
491 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
492 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
494 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
495 /* xgettext:c-format */
496 _bfd_error_handler (_("%B symbol number %lu references"
497 " nonexistent SHT_SYMTAB_SHNDX section"),
498 ibfd, (unsigned long) symoffset);
499 if (alloc_intsym != NULL)
500 free (alloc_intsym);
501 intsym_buf = NULL;
502 goto out;
505 out:
506 if (alloc_ext != NULL)
507 free (alloc_ext);
508 if (alloc_extshndx != NULL)
509 free (alloc_extshndx);
511 return intsym_buf;
514 /* Look up a symbol name. */
515 const char *
516 bfd_elf_sym_name (bfd *abfd,
517 Elf_Internal_Shdr *symtab_hdr,
518 Elf_Internal_Sym *isym,
519 asection *sym_sec)
521 const char *name;
522 unsigned int iname = isym->st_name;
523 unsigned int shindex = symtab_hdr->sh_link;
525 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
526 /* Check for a bogus st_shndx to avoid crashing. */
527 && isym->st_shndx < elf_numsections (abfd))
529 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
530 shindex = elf_elfheader (abfd)->e_shstrndx;
533 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
534 if (name == NULL)
535 name = "(null)";
536 else if (sym_sec && *name == '\0')
537 name = bfd_section_name (abfd, sym_sec);
539 return name;
542 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
543 sections. The first element is the flags, the rest are section
544 pointers. */
546 typedef union elf_internal_group {
547 Elf_Internal_Shdr *shdr;
548 unsigned int flags;
549 } Elf_Internal_Group;
551 /* Return the name of the group signature symbol. Why isn't the
552 signature just a string? */
554 static const char *
555 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
557 Elf_Internal_Shdr *hdr;
558 unsigned char esym[sizeof (Elf64_External_Sym)];
559 Elf_External_Sym_Shndx eshndx;
560 Elf_Internal_Sym isym;
562 /* First we need to ensure the symbol table is available. Make sure
563 that it is a symbol table section. */
564 if (ghdr->sh_link >= elf_numsections (abfd))
565 return NULL;
566 hdr = elf_elfsections (abfd) [ghdr->sh_link];
567 if (hdr->sh_type != SHT_SYMTAB
568 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
569 return NULL;
571 /* Go read the symbol. */
572 hdr = &elf_tdata (abfd)->symtab_hdr;
573 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
574 &isym, esym, &eshndx) == NULL)
575 return NULL;
577 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
580 /* Set next_in_group list pointer, and group name for NEWSECT. */
582 static bfd_boolean
583 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
585 unsigned int num_group = elf_tdata (abfd)->num_group;
587 /* If num_group is zero, read in all SHT_GROUP sections. The count
588 is set to -1 if there are no SHT_GROUP sections. */
589 if (num_group == 0)
591 unsigned int i, shnum;
593 /* First count the number of groups. If we have a SHT_GROUP
594 section with just a flag word (ie. sh_size is 4), ignore it. */
595 shnum = elf_numsections (abfd);
596 num_group = 0;
598 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
599 ( (shdr)->sh_type == SHT_GROUP \
600 && (shdr)->sh_size >= minsize \
601 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
602 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
604 for (i = 0; i < shnum; i++)
606 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
608 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
609 num_group += 1;
612 if (num_group == 0)
614 num_group = (unsigned) -1;
615 elf_tdata (abfd)->num_group = num_group;
616 elf_tdata (abfd)->group_sect_ptr = NULL;
618 else
620 /* We keep a list of elf section headers for group sections,
621 so we can find them quickly. */
622 bfd_size_type amt;
624 elf_tdata (abfd)->num_group = num_group;
625 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
626 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
627 if (elf_tdata (abfd)->group_sect_ptr == NULL)
628 return FALSE;
629 memset (elf_tdata (abfd)->group_sect_ptr, 0, num_group * sizeof (Elf_Internal_Shdr *));
630 num_group = 0;
632 for (i = 0; i < shnum; i++)
634 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
636 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
638 unsigned char *src;
639 Elf_Internal_Group *dest;
641 /* Make sure the group section has a BFD section
642 attached to it. */
643 if (!bfd_section_from_shdr (abfd, i))
644 return FALSE;
646 /* Add to list of sections. */
647 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
648 num_group += 1;
650 /* Read the raw contents. */
651 BFD_ASSERT (sizeof (*dest) >= 4);
652 amt = shdr->sh_size * sizeof (*dest) / 4;
653 shdr->contents = (unsigned char *)
654 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
655 /* PR binutils/4110: Handle corrupt group headers. */
656 if (shdr->contents == NULL)
658 _bfd_error_handler
659 /* xgettext:c-format */
660 (_("%B: corrupt size field in group section"
661 " header: %#Lx"), abfd, shdr->sh_size);
662 bfd_set_error (bfd_error_bad_value);
663 -- num_group;
664 continue;
667 memset (shdr->contents, 0, amt);
669 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
670 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
671 != shdr->sh_size))
673 _bfd_error_handler
674 /* xgettext:c-format */
675 (_("%B: invalid size field in group section"
676 " header: %#Lx"), abfd, shdr->sh_size);
677 bfd_set_error (bfd_error_bad_value);
678 -- num_group;
679 /* PR 17510: If the group contents are even
680 partially corrupt, do not allow any of the
681 contents to be used. */
682 memset (shdr->contents, 0, amt);
683 continue;
686 /* Translate raw contents, a flag word followed by an
687 array of elf section indices all in target byte order,
688 to the flag word followed by an array of elf section
689 pointers. */
690 src = shdr->contents + shdr->sh_size;
691 dest = (Elf_Internal_Group *) (shdr->contents + amt);
693 while (1)
695 unsigned int idx;
697 src -= 4;
698 --dest;
699 idx = H_GET_32 (abfd, src);
700 if (src == shdr->contents)
702 dest->flags = idx;
703 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
704 shdr->bfd_section->flags
705 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
706 break;
708 if (idx >= shnum)
710 _bfd_error_handler
711 (_("%B: invalid SHT_GROUP entry"), abfd);
712 idx = 0;
714 dest->shdr = elf_elfsections (abfd)[idx];
719 /* PR 17510: Corrupt binaries might contain invalid groups. */
720 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
722 elf_tdata (abfd)->num_group = num_group;
724 /* If all groups are invalid then fail. */
725 if (num_group == 0)
727 elf_tdata (abfd)->group_sect_ptr = NULL;
728 elf_tdata (abfd)->num_group = num_group = -1;
729 _bfd_error_handler
730 (_("%B: no valid group sections found"), abfd);
731 bfd_set_error (bfd_error_bad_value);
737 if (num_group != (unsigned) -1)
739 unsigned int i;
741 for (i = 0; i < num_group; i++)
743 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
744 Elf_Internal_Group *idx;
745 bfd_size_type n_elt;
747 if (shdr == NULL)
748 continue;
750 idx = (Elf_Internal_Group *) shdr->contents;
751 if (idx == NULL || shdr->sh_size < 4)
753 /* See PR 21957 for a reproducer. */
754 /* xgettext:c-format */
755 _bfd_error_handler (_("%B: group section '%A' has no contents"),
756 abfd, shdr->bfd_section);
757 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
758 bfd_set_error (bfd_error_bad_value);
759 return FALSE;
761 n_elt = shdr->sh_size / 4;
763 /* Look through this group's sections to see if current
764 section is a member. */
765 while (--n_elt != 0)
766 if ((++idx)->shdr == hdr)
768 asection *s = NULL;
770 /* We are a member of this group. Go looking through
771 other members to see if any others are linked via
772 next_in_group. */
773 idx = (Elf_Internal_Group *) shdr->contents;
774 n_elt = shdr->sh_size / 4;
775 while (--n_elt != 0)
776 if ((s = (++idx)->shdr->bfd_section) != NULL
777 && elf_next_in_group (s) != NULL)
778 break;
779 if (n_elt != 0)
781 /* Snarf the group name from other member, and
782 insert current section in circular list. */
783 elf_group_name (newsect) = elf_group_name (s);
784 elf_next_in_group (newsect) = elf_next_in_group (s);
785 elf_next_in_group (s) = newsect;
787 else
789 const char *gname;
791 gname = group_signature (abfd, shdr);
792 if (gname == NULL)
793 return FALSE;
794 elf_group_name (newsect) = gname;
796 /* Start a circular list with one element. */
797 elf_next_in_group (newsect) = newsect;
800 /* If the group section has been created, point to the
801 new member. */
802 if (shdr->bfd_section != NULL)
803 elf_next_in_group (shdr->bfd_section) = newsect;
805 i = num_group - 1;
806 break;
811 if (elf_group_name (newsect) == NULL)
813 /* xgettext:c-format */
814 _bfd_error_handler (_("%B: no group info for section '%A'"),
815 abfd, newsect);
816 return FALSE;
818 return TRUE;
821 bfd_boolean
822 _bfd_elf_setup_sections (bfd *abfd)
824 unsigned int i;
825 unsigned int num_group = elf_tdata (abfd)->num_group;
826 bfd_boolean result = TRUE;
827 asection *s;
829 /* Process SHF_LINK_ORDER. */
830 for (s = abfd->sections; s != NULL; s = s->next)
832 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
833 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
835 unsigned int elfsec = this_hdr->sh_link;
836 /* FIXME: The old Intel compiler and old strip/objcopy may
837 not set the sh_link or sh_info fields. Hence we could
838 get the situation where elfsec is 0. */
839 if (elfsec == 0)
841 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
842 if (bed->link_order_error_handler)
843 bed->link_order_error_handler
844 /* xgettext:c-format */
845 (_("%B: warning: sh_link not set for section `%A'"),
846 abfd, s);
848 else
850 asection *linksec = NULL;
852 if (elfsec < elf_numsections (abfd))
854 this_hdr = elf_elfsections (abfd)[elfsec];
855 linksec = this_hdr->bfd_section;
858 /* PR 1991, 2008:
859 Some strip/objcopy may leave an incorrect value in
860 sh_link. We don't want to proceed. */
861 if (linksec == NULL)
863 _bfd_error_handler
864 /* xgettext:c-format */
865 (_("%B: sh_link [%d] in section `%A' is incorrect"),
866 s->owner, elfsec, s);
867 result = FALSE;
870 elf_linked_to_section (s) = linksec;
873 else if (this_hdr->sh_type == SHT_GROUP
874 && elf_next_in_group (s) == NULL)
876 _bfd_error_handler
877 /* xgettext:c-format */
878 (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
879 abfd, elf_section_data (s)->this_idx);
880 result = FALSE;
884 /* Process section groups. */
885 if (num_group == (unsigned) -1)
886 return result;
888 for (i = 0; i < num_group; i++)
890 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
891 Elf_Internal_Group *idx;
892 unsigned int n_elt;
894 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
895 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
897 _bfd_error_handler
898 /* xgettext:c-format */
899 (_("%B: section group entry number %u is corrupt"),
900 abfd, i);
901 result = FALSE;
902 continue;
905 idx = (Elf_Internal_Group *) shdr->contents;
906 n_elt = shdr->sh_size / 4;
908 while (--n_elt != 0)
910 ++ idx;
912 if (idx->shdr == NULL)
913 continue;
914 else if (idx->shdr->bfd_section)
915 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
916 else if (idx->shdr->sh_type != SHT_RELA
917 && idx->shdr->sh_type != SHT_REL)
919 /* There are some unknown sections in the group. */
920 _bfd_error_handler
921 /* xgettext:c-format */
922 (_("%B: unknown type [%#x] section `%s' in group [%A]"),
923 abfd,
924 idx->shdr->sh_type,
925 bfd_elf_string_from_elf_section (abfd,
926 (elf_elfheader (abfd)
927 ->e_shstrndx),
928 idx->shdr->sh_name),
929 shdr->bfd_section);
930 result = FALSE;
935 return result;
938 bfd_boolean
939 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
941 return elf_next_in_group (sec) != NULL;
944 static char *
945 convert_debug_to_zdebug (bfd *abfd, const char *name)
947 unsigned int len = strlen (name);
948 char *new_name = bfd_alloc (abfd, len + 2);
949 if (new_name == NULL)
950 return NULL;
951 new_name[0] = '.';
952 new_name[1] = 'z';
953 memcpy (new_name + 2, name + 1, len);
954 return new_name;
957 static char *
958 convert_zdebug_to_debug (bfd *abfd, const char *name)
960 unsigned int len = strlen (name);
961 char *new_name = bfd_alloc (abfd, len);
962 if (new_name == NULL)
963 return NULL;
964 new_name[0] = '.';
965 memcpy (new_name + 1, name + 2, len - 1);
966 return new_name;
969 /* Make a BFD section from an ELF section. We store a pointer to the
970 BFD section in the bfd_section field of the header. */
972 bfd_boolean
973 _bfd_elf_make_section_from_shdr (bfd *abfd,
974 Elf_Internal_Shdr *hdr,
975 const char *name,
976 int shindex)
978 asection *newsect;
979 flagword flags;
980 const struct elf_backend_data *bed;
982 if (hdr->bfd_section != NULL)
983 return TRUE;
985 newsect = bfd_make_section_anyway (abfd, name);
986 if (newsect == NULL)
987 return FALSE;
989 hdr->bfd_section = newsect;
990 elf_section_data (newsect)->this_hdr = *hdr;
991 elf_section_data (newsect)->this_idx = shindex;
993 /* Always use the real type/flags. */
994 elf_section_type (newsect) = hdr->sh_type;
995 elf_section_flags (newsect) = hdr->sh_flags;
997 newsect->filepos = hdr->sh_offset;
999 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
1000 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
1001 || ! bfd_set_section_alignment (abfd, newsect,
1002 bfd_log2 (hdr->sh_addralign)))
1003 return FALSE;
1005 flags = SEC_NO_FLAGS;
1006 if (hdr->sh_type != SHT_NOBITS)
1007 flags |= SEC_HAS_CONTENTS;
1008 if (hdr->sh_type == SHT_GROUP)
1009 flags |= SEC_GROUP;
1010 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1012 flags |= SEC_ALLOC;
1013 if (hdr->sh_type != SHT_NOBITS)
1014 flags |= SEC_LOAD;
1016 if ((hdr->sh_flags & SHF_WRITE) == 0)
1017 flags |= SEC_READONLY;
1018 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1019 flags |= SEC_CODE;
1020 else if ((flags & SEC_LOAD) != 0)
1021 flags |= SEC_DATA;
1022 if ((hdr->sh_flags & SHF_MERGE) != 0)
1024 flags |= SEC_MERGE;
1025 newsect->entsize = hdr->sh_entsize;
1027 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1028 flags |= SEC_STRINGS;
1029 if (hdr->sh_flags & SHF_GROUP)
1030 if (!setup_group (abfd, hdr, newsect))
1031 return FALSE;
1032 if ((hdr->sh_flags & SHF_TLS) != 0)
1033 flags |= SEC_THREAD_LOCAL;
1034 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1035 flags |= SEC_EXCLUDE;
1037 if ((flags & SEC_ALLOC) == 0)
1039 /* The debugging sections appear to be recognized only by name,
1040 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1041 if (name [0] == '.')
1043 const char *p;
1044 int n;
1045 if (name[1] == 'd')
1046 p = ".debug", n = 6;
1047 else if (name[1] == 'g' && name[2] == 'n')
1048 p = ".gnu.linkonce.wi.", n = 17;
1049 else if (name[1] == 'g' && name[2] == 'd')
1050 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1051 else if (name[1] == 'l')
1052 p = ".line", n = 5;
1053 else if (name[1] == 's')
1054 p = ".stab", n = 5;
1055 else if (name[1] == 'z')
1056 p = ".zdebug", n = 7;
1057 else
1058 p = NULL, n = 0;
1059 if (p != NULL && strncmp (name, p, n) == 0)
1060 flags |= SEC_DEBUGGING;
1064 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1065 only link a single copy of the section. This is used to support
1066 g++. g++ will emit each template expansion in its own section.
1067 The symbols will be defined as weak, so that multiple definitions
1068 are permitted. The GNU linker extension is to actually discard
1069 all but one of the sections. */
1070 if (CONST_STRNEQ (name, ".gnu.linkonce")
1071 && elf_next_in_group (newsect) == NULL)
1072 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1074 bed = get_elf_backend_data (abfd);
1075 if (bed->elf_backend_section_flags)
1076 if (! bed->elf_backend_section_flags (&flags, hdr))
1077 return FALSE;
1079 if (! bfd_set_section_flags (abfd, newsect, flags))
1080 return FALSE;
1082 /* We do not parse the PT_NOTE segments as we are interested even in the
1083 separate debug info files which may have the segments offsets corrupted.
1084 PT_NOTEs from the core files are currently not parsed using BFD. */
1085 if (hdr->sh_type == SHT_NOTE)
1087 bfd_byte *contents;
1089 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1090 return FALSE;
1092 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, hdr->sh_offset);
1093 free (contents);
1096 if ((flags & SEC_ALLOC) != 0)
1098 Elf_Internal_Phdr *phdr;
1099 unsigned int i, nload;
1101 /* Some ELF linkers produce binaries with all the program header
1102 p_paddr fields zero. If we have such a binary with more than
1103 one PT_LOAD header, then leave the section lma equal to vma
1104 so that we don't create sections with overlapping lma. */
1105 phdr = elf_tdata (abfd)->phdr;
1106 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1107 if (phdr->p_paddr != 0)
1108 break;
1109 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1110 ++nload;
1111 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1112 return TRUE;
1114 phdr = elf_tdata (abfd)->phdr;
1115 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1117 if (((phdr->p_type == PT_LOAD
1118 && (hdr->sh_flags & SHF_TLS) == 0)
1119 || phdr->p_type == PT_TLS)
1120 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1122 if ((flags & SEC_LOAD) == 0)
1123 newsect->lma = (phdr->p_paddr
1124 + hdr->sh_addr - phdr->p_vaddr);
1125 else
1126 /* We used to use the same adjustment for SEC_LOAD
1127 sections, but that doesn't work if the segment
1128 is packed with code from multiple VMAs.
1129 Instead we calculate the section LMA based on
1130 the segment LMA. It is assumed that the
1131 segment will contain sections with contiguous
1132 LMAs, even if the VMAs are not. */
1133 newsect->lma = (phdr->p_paddr
1134 + hdr->sh_offset - phdr->p_offset);
1136 /* With contiguous segments, we can't tell from file
1137 offsets whether a section with zero size should
1138 be placed at the end of one segment or the
1139 beginning of the next. Decide based on vaddr. */
1140 if (hdr->sh_addr >= phdr->p_vaddr
1141 && (hdr->sh_addr + hdr->sh_size
1142 <= phdr->p_vaddr + phdr->p_memsz))
1143 break;
1148 /* Compress/decompress DWARF debug sections with names: .debug_* and
1149 .zdebug_*, after the section flags is set. */
1150 if ((flags & SEC_DEBUGGING)
1151 && ((name[1] == 'd' && name[6] == '_')
1152 || (name[1] == 'z' && name[7] == '_')))
1154 enum { nothing, compress, decompress } action = nothing;
1155 int compression_header_size;
1156 bfd_size_type uncompressed_size;
1157 bfd_boolean compressed
1158 = bfd_is_section_compressed_with_header (abfd, newsect,
1159 &compression_header_size,
1160 &uncompressed_size);
1162 if (compressed)
1164 /* Compressed section. Check if we should decompress. */
1165 if ((abfd->flags & BFD_DECOMPRESS))
1166 action = decompress;
1169 /* Compress the uncompressed section or convert from/to .zdebug*
1170 section. Check if we should compress. */
1171 if (action == nothing)
1173 if (newsect->size != 0
1174 && (abfd->flags & BFD_COMPRESS)
1175 && compression_header_size >= 0
1176 && uncompressed_size > 0
1177 && (!compressed
1178 || ((compression_header_size > 0)
1179 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1180 action = compress;
1181 else
1182 return TRUE;
1185 if (action == compress)
1187 if (!bfd_init_section_compress_status (abfd, newsect))
1189 _bfd_error_handler
1190 /* xgettext:c-format */
1191 (_("%B: unable to initialize compress status for section %s"),
1192 abfd, name);
1193 return FALSE;
1196 else
1198 if (!bfd_init_section_decompress_status (abfd, newsect))
1200 _bfd_error_handler
1201 /* xgettext:c-format */
1202 (_("%B: unable to initialize decompress status for section %s"),
1203 abfd, name);
1204 return FALSE;
1208 if (abfd->is_linker_input)
1210 if (name[1] == 'z'
1211 && (action == decompress
1212 || (action == compress
1213 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1215 /* Convert section name from .zdebug_* to .debug_* so
1216 that linker will consider this section as a debug
1217 section. */
1218 char *new_name = convert_zdebug_to_debug (abfd, name);
1219 if (new_name == NULL)
1220 return FALSE;
1221 bfd_rename_section (abfd, newsect, new_name);
1224 else
1225 /* For objdump, don't rename the section. For objcopy, delay
1226 section rename to elf_fake_sections. */
1227 newsect->flags |= SEC_ELF_RENAME;
1230 return TRUE;
1233 const char *const bfd_elf_section_type_names[] =
1235 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1236 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1237 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1240 /* ELF relocs are against symbols. If we are producing relocatable
1241 output, and the reloc is against an external symbol, and nothing
1242 has given us any additional addend, the resulting reloc will also
1243 be against the same symbol. In such a case, we don't want to
1244 change anything about the way the reloc is handled, since it will
1245 all be done at final link time. Rather than put special case code
1246 into bfd_perform_relocation, all the reloc types use this howto
1247 function. It just short circuits the reloc if producing
1248 relocatable output against an external symbol. */
1250 bfd_reloc_status_type
1251 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1252 arelent *reloc_entry,
1253 asymbol *symbol,
1254 void *data ATTRIBUTE_UNUSED,
1255 asection *input_section,
1256 bfd *output_bfd,
1257 char **error_message ATTRIBUTE_UNUSED)
1259 if (output_bfd != NULL
1260 && (symbol->flags & BSF_SECTION_SYM) == 0
1261 && (! reloc_entry->howto->partial_inplace
1262 || reloc_entry->addend == 0))
1264 reloc_entry->address += input_section->output_offset;
1265 return bfd_reloc_ok;
1268 return bfd_reloc_continue;
1271 /* Returns TRUE if section A matches section B.
1272 Names, addresses and links may be different, but everything else
1273 should be the same. */
1275 static bfd_boolean
1276 section_match (const Elf_Internal_Shdr * a,
1277 const Elf_Internal_Shdr * b)
1279 return
1280 a->sh_type == b->sh_type
1281 && (a->sh_flags & ~ SHF_INFO_LINK)
1282 == (b->sh_flags & ~ SHF_INFO_LINK)
1283 && a->sh_addralign == b->sh_addralign
1284 && a->sh_size == b->sh_size
1285 && a->sh_entsize == b->sh_entsize
1286 /* FIXME: Check sh_addr ? */
1290 /* Find a section in OBFD that has the same characteristics
1291 as IHEADER. Return the index of this section or SHN_UNDEF if
1292 none can be found. Check's section HINT first, as this is likely
1293 to be the correct section. */
1295 static unsigned int
1296 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1297 const unsigned int hint)
1299 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1300 unsigned int i;
1302 BFD_ASSERT (iheader != NULL);
1304 /* See PR 20922 for a reproducer of the NULL test. */
1305 if (hint < elf_numsections (obfd)
1306 && oheaders[hint] != NULL
1307 && section_match (oheaders[hint], iheader))
1308 return hint;
1310 for (i = 1; i < elf_numsections (obfd); i++)
1312 Elf_Internal_Shdr * oheader = oheaders[i];
1314 if (oheader == NULL)
1315 continue;
1316 if (section_match (oheader, iheader))
1317 /* FIXME: Do we care if there is a potential for
1318 multiple matches ? */
1319 return i;
1322 return SHN_UNDEF;
1325 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1326 Processor specific section, based upon a matching input section.
1327 Returns TRUE upon success, FALSE otherwise. */
1329 static bfd_boolean
1330 copy_special_section_fields (const bfd *ibfd,
1331 bfd *obfd,
1332 const Elf_Internal_Shdr *iheader,
1333 Elf_Internal_Shdr *oheader,
1334 const unsigned int secnum)
1336 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1337 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1338 bfd_boolean changed = FALSE;
1339 unsigned int sh_link;
1341 if (oheader->sh_type == SHT_NOBITS)
1343 /* This is a feature for objcopy --only-keep-debug:
1344 When a section's type is changed to NOBITS, we preserve
1345 the sh_link and sh_info fields so that they can be
1346 matched up with the original.
1348 Note: Strictly speaking these assignments are wrong.
1349 The sh_link and sh_info fields should point to the
1350 relevent sections in the output BFD, which may not be in
1351 the same location as they were in the input BFD. But
1352 the whole point of this action is to preserve the
1353 original values of the sh_link and sh_info fields, so
1354 that they can be matched up with the section headers in
1355 the original file. So strictly speaking we may be
1356 creating an invalid ELF file, but it is only for a file
1357 that just contains debug info and only for sections
1358 without any contents. */
1359 if (oheader->sh_link == 0)
1360 oheader->sh_link = iheader->sh_link;
1361 if (oheader->sh_info == 0)
1362 oheader->sh_info = iheader->sh_info;
1363 return TRUE;
1366 /* Allow the target a chance to decide how these fields should be set. */
1367 if (bed->elf_backend_copy_special_section_fields != NULL
1368 && bed->elf_backend_copy_special_section_fields
1369 (ibfd, obfd, iheader, oheader))
1370 return TRUE;
1372 /* We have an iheader which might match oheader, and which has non-zero
1373 sh_info and/or sh_link fields. Attempt to follow those links and find
1374 the section in the output bfd which corresponds to the linked section
1375 in the input bfd. */
1376 if (iheader->sh_link != SHN_UNDEF)
1378 /* See PR 20931 for a reproducer. */
1379 if (iheader->sh_link >= elf_numsections (ibfd))
1381 _bfd_error_handler
1382 /* xgettext:c-format */
1383 (_("%B: Invalid sh_link field (%d) in section number %d"),
1384 ibfd, iheader->sh_link, secnum);
1385 return FALSE;
1388 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1389 if (sh_link != SHN_UNDEF)
1391 oheader->sh_link = sh_link;
1392 changed = TRUE;
1394 else
1395 /* FIXME: Should we install iheader->sh_link
1396 if we could not find a match ? */
1397 _bfd_error_handler
1398 /* xgettext:c-format */
1399 (_("%B: Failed to find link section for section %d"), obfd, secnum);
1402 if (iheader->sh_info)
1404 /* The sh_info field can hold arbitrary information, but if the
1405 SHF_LINK_INFO flag is set then it should be interpreted as a
1406 section index. */
1407 if (iheader->sh_flags & SHF_INFO_LINK)
1409 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1410 iheader->sh_info);
1411 if (sh_link != SHN_UNDEF)
1412 oheader->sh_flags |= SHF_INFO_LINK;
1414 else
1415 /* No idea what it means - just copy it. */
1416 sh_link = iheader->sh_info;
1418 if (sh_link != SHN_UNDEF)
1420 oheader->sh_info = sh_link;
1421 changed = TRUE;
1423 else
1424 _bfd_error_handler
1425 /* xgettext:c-format */
1426 (_("%B: Failed to find info section for section %d"), obfd, secnum);
1429 return changed;
1432 /* Copy the program header and other data from one object module to
1433 another. */
1435 bfd_boolean
1436 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1438 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1439 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1440 const struct elf_backend_data *bed;
1441 unsigned int i;
1443 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1444 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1445 return TRUE;
1447 if (!elf_flags_init (obfd))
1449 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1450 elf_flags_init (obfd) = TRUE;
1453 elf_gp (obfd) = elf_gp (ibfd);
1455 /* Also copy the EI_OSABI field. */
1456 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1457 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1459 /* If set, copy the EI_ABIVERSION field. */
1460 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1461 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1462 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1464 /* Copy object attributes. */
1465 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1467 if (iheaders == NULL || oheaders == NULL)
1468 return TRUE;
1470 bed = get_elf_backend_data (obfd);
1472 /* Possibly copy other fields in the section header. */
1473 for (i = 1; i < elf_numsections (obfd); i++)
1475 unsigned int j;
1476 Elf_Internal_Shdr * oheader = oheaders[i];
1478 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1479 because of a special case need for generating separate debug info
1480 files. See below for more details. */
1481 if (oheader == NULL
1482 || (oheader->sh_type != SHT_NOBITS
1483 && oheader->sh_type < SHT_LOOS))
1484 continue;
1486 /* Ignore empty sections, and sections whose
1487 fields have already been initialised. */
1488 if (oheader->sh_size == 0
1489 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1490 continue;
1492 /* Scan for the matching section in the input bfd.
1493 First we try for a direct mapping between the input and output sections. */
1494 for (j = 1; j < elf_numsections (ibfd); j++)
1496 const Elf_Internal_Shdr * iheader = iheaders[j];
1498 if (iheader == NULL)
1499 continue;
1501 if (oheader->bfd_section != NULL
1502 && iheader->bfd_section != NULL
1503 && iheader->bfd_section->output_section != NULL
1504 && iheader->bfd_section->output_section == oheader->bfd_section)
1506 /* We have found a connection from the input section to the
1507 output section. Attempt to copy the header fields. If
1508 this fails then do not try any further sections - there
1509 should only be a one-to-one mapping between input and output. */
1510 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1511 j = elf_numsections (ibfd);
1512 break;
1516 if (j < elf_numsections (ibfd))
1517 continue;
1519 /* That failed. So try to deduce the corresponding input section.
1520 Unfortunately we cannot compare names as the output string table
1521 is empty, so instead we check size, address and type. */
1522 for (j = 1; j < elf_numsections (ibfd); j++)
1524 const Elf_Internal_Shdr * iheader = iheaders[j];
1526 if (iheader == NULL)
1527 continue;
1529 /* Try matching fields in the input section's header.
1530 Since --only-keep-debug turns all non-debug sections into
1531 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1532 input type. */
1533 if ((oheader->sh_type == SHT_NOBITS
1534 || iheader->sh_type == oheader->sh_type)
1535 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1536 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1537 && iheader->sh_addralign == oheader->sh_addralign
1538 && iheader->sh_entsize == oheader->sh_entsize
1539 && iheader->sh_size == oheader->sh_size
1540 && iheader->sh_addr == oheader->sh_addr
1541 && (iheader->sh_info != oheader->sh_info
1542 || iheader->sh_link != oheader->sh_link))
1544 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1545 break;
1549 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1551 /* Final attempt. Call the backend copy function
1552 with a NULL input section. */
1553 if (bed->elf_backend_copy_special_section_fields != NULL)
1554 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1558 return TRUE;
1561 static const char *
1562 get_segment_type (unsigned int p_type)
1564 const char *pt;
1565 switch (p_type)
1567 case PT_NULL: pt = "NULL"; break;
1568 case PT_LOAD: pt = "LOAD"; break;
1569 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1570 case PT_INTERP: pt = "INTERP"; break;
1571 case PT_NOTE: pt = "NOTE"; break;
1572 case PT_SHLIB: pt = "SHLIB"; break;
1573 case PT_PHDR: pt = "PHDR"; break;
1574 case PT_TLS: pt = "TLS"; break;
1575 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1576 case PT_GNU_STACK: pt = "STACK"; break;
1577 case PT_GNU_RELRO: pt = "RELRO"; break;
1578 default: pt = NULL; break;
1580 return pt;
1583 /* Print out the program headers. */
1585 bfd_boolean
1586 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1588 FILE *f = (FILE *) farg;
1589 Elf_Internal_Phdr *p;
1590 asection *s;
1591 bfd_byte *dynbuf = NULL;
1593 p = elf_tdata (abfd)->phdr;
1594 if (p != NULL)
1596 unsigned int i, c;
1598 fprintf (f, _("\nProgram Header:\n"));
1599 c = elf_elfheader (abfd)->e_phnum;
1600 for (i = 0; i < c; i++, p++)
1602 const char *pt = get_segment_type (p->p_type);
1603 char buf[20];
1605 if (pt == NULL)
1607 sprintf (buf, "0x%lx", p->p_type);
1608 pt = buf;
1610 fprintf (f, "%8s off 0x", pt);
1611 bfd_fprintf_vma (abfd, f, p->p_offset);
1612 fprintf (f, " vaddr 0x");
1613 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1614 fprintf (f, " paddr 0x");
1615 bfd_fprintf_vma (abfd, f, p->p_paddr);
1616 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1617 fprintf (f, " filesz 0x");
1618 bfd_fprintf_vma (abfd, f, p->p_filesz);
1619 fprintf (f, " memsz 0x");
1620 bfd_fprintf_vma (abfd, f, p->p_memsz);
1621 fprintf (f, " flags %c%c%c",
1622 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1623 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1624 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1625 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1626 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1627 fprintf (f, "\n");
1631 s = bfd_get_section_by_name (abfd, ".dynamic");
1632 if (s != NULL)
1634 unsigned int elfsec;
1635 unsigned long shlink;
1636 bfd_byte *extdyn, *extdynend;
1637 size_t extdynsize;
1638 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1640 fprintf (f, _("\nDynamic Section:\n"));
1642 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1643 goto error_return;
1645 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1646 if (elfsec == SHN_BAD)
1647 goto error_return;
1648 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1650 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1651 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1653 extdyn = dynbuf;
1654 /* PR 17512: file: 6f427532. */
1655 if (s->size < extdynsize)
1656 goto error_return;
1657 extdynend = extdyn + s->size;
1658 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1659 Fix range check. */
1660 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1662 Elf_Internal_Dyn dyn;
1663 const char *name = "";
1664 char ab[20];
1665 bfd_boolean stringp;
1666 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1668 (*swap_dyn_in) (abfd, extdyn, &dyn);
1670 if (dyn.d_tag == DT_NULL)
1671 break;
1673 stringp = FALSE;
1674 switch (dyn.d_tag)
1676 default:
1677 if (bed->elf_backend_get_target_dtag)
1678 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1680 if (!strcmp (name, ""))
1682 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
1683 name = ab;
1685 break;
1687 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1688 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1689 case DT_PLTGOT: name = "PLTGOT"; break;
1690 case DT_HASH: name = "HASH"; break;
1691 case DT_STRTAB: name = "STRTAB"; break;
1692 case DT_SYMTAB: name = "SYMTAB"; break;
1693 case DT_RELA: name = "RELA"; break;
1694 case DT_RELASZ: name = "RELASZ"; break;
1695 case DT_RELAENT: name = "RELAENT"; break;
1696 case DT_STRSZ: name = "STRSZ"; break;
1697 case DT_SYMENT: name = "SYMENT"; break;
1698 case DT_INIT: name = "INIT"; break;
1699 case DT_FINI: name = "FINI"; break;
1700 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1701 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1702 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1703 case DT_REL: name = "REL"; break;
1704 case DT_RELSZ: name = "RELSZ"; break;
1705 case DT_RELENT: name = "RELENT"; break;
1706 case DT_PLTREL: name = "PLTREL"; break;
1707 case DT_DEBUG: name = "DEBUG"; break;
1708 case DT_TEXTREL: name = "TEXTREL"; break;
1709 case DT_JMPREL: name = "JMPREL"; break;
1710 case DT_BIND_NOW: name = "BIND_NOW"; break;
1711 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1712 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1713 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1714 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1715 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1716 case DT_FLAGS: name = "FLAGS"; break;
1717 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1718 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1719 case DT_CHECKSUM: name = "CHECKSUM"; break;
1720 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1721 case DT_MOVEENT: name = "MOVEENT"; break;
1722 case DT_MOVESZ: name = "MOVESZ"; break;
1723 case DT_FEATURE: name = "FEATURE"; break;
1724 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1725 case DT_SYMINSZ: name = "SYMINSZ"; break;
1726 case DT_SYMINENT: name = "SYMINENT"; break;
1727 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1728 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1729 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1730 case DT_PLTPAD: name = "PLTPAD"; break;
1731 case DT_MOVETAB: name = "MOVETAB"; break;
1732 case DT_SYMINFO: name = "SYMINFO"; break;
1733 case DT_RELACOUNT: name = "RELACOUNT"; break;
1734 case DT_RELCOUNT: name = "RELCOUNT"; break;
1735 case DT_FLAGS_1: name = "FLAGS_1"; break;
1736 case DT_VERSYM: name = "VERSYM"; break;
1737 case DT_VERDEF: name = "VERDEF"; break;
1738 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1739 case DT_VERNEED: name = "VERNEED"; break;
1740 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1741 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1742 case DT_USED: name = "USED"; break;
1743 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1744 case DT_GNU_HASH: name = "GNU_HASH"; break;
1747 fprintf (f, " %-20s ", name);
1748 if (! stringp)
1750 fprintf (f, "0x");
1751 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1753 else
1755 const char *string;
1756 unsigned int tagv = dyn.d_un.d_val;
1758 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1759 if (string == NULL)
1760 goto error_return;
1761 fprintf (f, "%s", string);
1763 fprintf (f, "\n");
1766 free (dynbuf);
1767 dynbuf = NULL;
1770 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1771 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1773 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1774 return FALSE;
1777 if (elf_dynverdef (abfd) != 0)
1779 Elf_Internal_Verdef *t;
1781 fprintf (f, _("\nVersion definitions:\n"));
1782 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1784 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1785 t->vd_flags, t->vd_hash,
1786 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1787 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1789 Elf_Internal_Verdaux *a;
1791 fprintf (f, "\t");
1792 for (a = t->vd_auxptr->vda_nextptr;
1793 a != NULL;
1794 a = a->vda_nextptr)
1795 fprintf (f, "%s ",
1796 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1797 fprintf (f, "\n");
1802 if (elf_dynverref (abfd) != 0)
1804 Elf_Internal_Verneed *t;
1806 fprintf (f, _("\nVersion References:\n"));
1807 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1809 Elf_Internal_Vernaux *a;
1811 fprintf (f, _(" required from %s:\n"),
1812 t->vn_filename ? t->vn_filename : "<corrupt>");
1813 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1814 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1815 a->vna_flags, a->vna_other,
1816 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1820 return TRUE;
1822 error_return:
1823 if (dynbuf != NULL)
1824 free (dynbuf);
1825 return FALSE;
1828 /* Get version string. */
1830 const char *
1831 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1832 bfd_boolean *hidden)
1834 const char *version_string = NULL;
1835 if (elf_dynversym (abfd) != 0
1836 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1838 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1840 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1841 vernum &= VERSYM_VERSION;
1843 if (vernum == 0)
1844 version_string = "";
1845 else if (vernum == 1)
1846 version_string = "Base";
1847 else if (vernum <= elf_tdata (abfd)->cverdefs)
1848 version_string =
1849 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1850 else
1852 Elf_Internal_Verneed *t;
1854 version_string = "";
1855 for (t = elf_tdata (abfd)->verref;
1856 t != NULL;
1857 t = t->vn_nextref)
1859 Elf_Internal_Vernaux *a;
1861 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1863 if (a->vna_other == vernum)
1865 version_string = a->vna_nodename;
1866 break;
1872 return version_string;
1875 /* Display ELF-specific fields of a symbol. */
1877 void
1878 bfd_elf_print_symbol (bfd *abfd,
1879 void *filep,
1880 asymbol *symbol,
1881 bfd_print_symbol_type how)
1883 FILE *file = (FILE *) filep;
1884 switch (how)
1886 case bfd_print_symbol_name:
1887 fprintf (file, "%s", symbol->name);
1888 break;
1889 case bfd_print_symbol_more:
1890 fprintf (file, "elf ");
1891 bfd_fprintf_vma (abfd, file, symbol->value);
1892 fprintf (file, " %x", symbol->flags);
1893 break;
1894 case bfd_print_symbol_all:
1896 const char *section_name;
1897 const char *name = NULL;
1898 const struct elf_backend_data *bed;
1899 unsigned char st_other;
1900 bfd_vma val;
1901 const char *version_string;
1902 bfd_boolean hidden;
1904 section_name = symbol->section ? symbol->section->name : "(*none*)";
1906 bed = get_elf_backend_data (abfd);
1907 if (bed->elf_backend_print_symbol_all)
1908 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1910 if (name == NULL)
1912 name = symbol->name;
1913 bfd_print_symbol_vandf (abfd, file, symbol);
1916 fprintf (file, " %s\t", section_name);
1917 /* Print the "other" value for a symbol. For common symbols,
1918 we've already printed the size; now print the alignment.
1919 For other symbols, we have no specified alignment, and
1920 we've printed the address; now print the size. */
1921 if (symbol->section && bfd_is_com_section (symbol->section))
1922 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1923 else
1924 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1925 bfd_fprintf_vma (abfd, file, val);
1927 /* If we have version information, print it. */
1928 version_string = _bfd_elf_get_symbol_version_string (abfd,
1929 symbol,
1930 &hidden);
1931 if (version_string)
1933 if (!hidden)
1934 fprintf (file, " %-11s", version_string);
1935 else
1937 int i;
1939 fprintf (file, " (%s)", version_string);
1940 for (i = 10 - strlen (version_string); i > 0; --i)
1941 putc (' ', file);
1945 /* If the st_other field is not zero, print it. */
1946 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1948 switch (st_other)
1950 case 0: break;
1951 case STV_INTERNAL: fprintf (file, " .internal"); break;
1952 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1953 case STV_PROTECTED: fprintf (file, " .protected"); break;
1954 default:
1955 /* Some other non-defined flags are also present, so print
1956 everything hex. */
1957 fprintf (file, " 0x%02x", (unsigned int) st_other);
1960 fprintf (file, " %s", name);
1962 break;
1966 /* ELF .o/exec file reading */
1968 /* Create a new bfd section from an ELF section header. */
1970 bfd_boolean
1971 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1973 Elf_Internal_Shdr *hdr;
1974 Elf_Internal_Ehdr *ehdr;
1975 const struct elf_backend_data *bed;
1976 const char *name;
1977 bfd_boolean ret = TRUE;
1978 static bfd_boolean * sections_being_created = NULL;
1979 static bfd * sections_being_created_abfd = NULL;
1980 static unsigned int nesting = 0;
1982 if (shindex >= elf_numsections (abfd))
1983 return FALSE;
1985 if (++ nesting > 3)
1987 /* PR17512: A corrupt ELF binary might contain a recursive group of
1988 sections, with each the string indicies pointing to the next in the
1989 loop. Detect this here, by refusing to load a section that we are
1990 already in the process of loading. We only trigger this test if
1991 we have nested at least three sections deep as normal ELF binaries
1992 can expect to recurse at least once.
1994 FIXME: It would be better if this array was attached to the bfd,
1995 rather than being held in a static pointer. */
1997 if (sections_being_created_abfd != abfd)
1998 sections_being_created = NULL;
1999 if (sections_being_created == NULL)
2001 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
2002 sections_being_created = (bfd_boolean *)
2003 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
2004 sections_being_created_abfd = abfd;
2006 if (sections_being_created [shindex])
2008 _bfd_error_handler
2009 (_("%B: warning: loop in section dependencies detected"), abfd);
2010 return FALSE;
2012 sections_being_created [shindex] = TRUE;
2015 hdr = elf_elfsections (abfd)[shindex];
2016 ehdr = elf_elfheader (abfd);
2017 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2018 hdr->sh_name);
2019 if (name == NULL)
2020 goto fail;
2022 bed = get_elf_backend_data (abfd);
2023 switch (hdr->sh_type)
2025 case SHT_NULL:
2026 /* Inactive section. Throw it away. */
2027 goto success;
2029 case SHT_PROGBITS: /* Normal section with contents. */
2030 case SHT_NOBITS: /* .bss section. */
2031 case SHT_HASH: /* .hash section. */
2032 case SHT_NOTE: /* .note section. */
2033 case SHT_INIT_ARRAY: /* .init_array section. */
2034 case SHT_FINI_ARRAY: /* .fini_array section. */
2035 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2036 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2037 case SHT_GNU_HASH: /* .gnu.hash section. */
2038 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2039 goto success;
2041 case SHT_DYNAMIC: /* Dynamic linking information. */
2042 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2043 goto fail;
2045 if (hdr->sh_link > elf_numsections (abfd))
2047 /* PR 10478: Accept Solaris binaries with a sh_link
2048 field set to SHN_BEFORE or SHN_AFTER. */
2049 switch (bfd_get_arch (abfd))
2051 case bfd_arch_i386:
2052 case bfd_arch_sparc:
2053 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2054 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2055 break;
2056 /* Otherwise fall through. */
2057 default:
2058 goto fail;
2061 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2062 goto fail;
2063 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2065 Elf_Internal_Shdr *dynsymhdr;
2067 /* The shared libraries distributed with hpux11 have a bogus
2068 sh_link field for the ".dynamic" section. Find the
2069 string table for the ".dynsym" section instead. */
2070 if (elf_dynsymtab (abfd) != 0)
2072 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2073 hdr->sh_link = dynsymhdr->sh_link;
2075 else
2077 unsigned int i, num_sec;
2079 num_sec = elf_numsections (abfd);
2080 for (i = 1; i < num_sec; i++)
2082 dynsymhdr = elf_elfsections (abfd)[i];
2083 if (dynsymhdr->sh_type == SHT_DYNSYM)
2085 hdr->sh_link = dynsymhdr->sh_link;
2086 break;
2091 goto success;
2093 case SHT_SYMTAB: /* A symbol table. */
2094 if (elf_onesymtab (abfd) == shindex)
2095 goto success;
2097 if (hdr->sh_entsize != bed->s->sizeof_sym)
2098 goto fail;
2100 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2102 if (hdr->sh_size != 0)
2103 goto fail;
2104 /* Some assemblers erroneously set sh_info to one with a
2105 zero sh_size. ld sees this as a global symbol count
2106 of (unsigned) -1. Fix it here. */
2107 hdr->sh_info = 0;
2108 goto success;
2111 /* PR 18854: A binary might contain more than one symbol table.
2112 Unusual, but possible. Warn, but continue. */
2113 if (elf_onesymtab (abfd) != 0)
2115 _bfd_error_handler
2116 /* xgettext:c-format */
2117 (_("%B: warning: multiple symbol tables detected"
2118 " - ignoring the table in section %u"),
2119 abfd, shindex);
2120 goto success;
2122 elf_onesymtab (abfd) = shindex;
2123 elf_symtab_hdr (abfd) = *hdr;
2124 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2125 abfd->flags |= HAS_SYMS;
2127 /* Sometimes a shared object will map in the symbol table. If
2128 SHF_ALLOC is set, and this is a shared object, then we also
2129 treat this section as a BFD section. We can not base the
2130 decision purely on SHF_ALLOC, because that flag is sometimes
2131 set in a relocatable object file, which would confuse the
2132 linker. */
2133 if ((hdr->sh_flags & SHF_ALLOC) != 0
2134 && (abfd->flags & DYNAMIC) != 0
2135 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2136 shindex))
2137 goto fail;
2139 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2140 can't read symbols without that section loaded as well. It
2141 is most likely specified by the next section header. */
2143 elf_section_list * entry;
2144 unsigned int i, num_sec;
2146 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2147 if (entry->hdr.sh_link == shindex)
2148 goto success;
2150 num_sec = elf_numsections (abfd);
2151 for (i = shindex + 1; i < num_sec; i++)
2153 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2155 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2156 && hdr2->sh_link == shindex)
2157 break;
2160 if (i == num_sec)
2161 for (i = 1; i < shindex; i++)
2163 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2165 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2166 && hdr2->sh_link == shindex)
2167 break;
2170 if (i != shindex)
2171 ret = bfd_section_from_shdr (abfd, i);
2172 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2173 goto success;
2176 case SHT_DYNSYM: /* A dynamic symbol table. */
2177 if (elf_dynsymtab (abfd) == shindex)
2178 goto success;
2180 if (hdr->sh_entsize != bed->s->sizeof_sym)
2181 goto fail;
2183 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2185 if (hdr->sh_size != 0)
2186 goto fail;
2188 /* Some linkers erroneously set sh_info to one with a
2189 zero sh_size. ld sees this as a global symbol count
2190 of (unsigned) -1. Fix it here. */
2191 hdr->sh_info = 0;
2192 goto success;
2195 /* PR 18854: A binary might contain more than one dynamic symbol table.
2196 Unusual, but possible. Warn, but continue. */
2197 if (elf_dynsymtab (abfd) != 0)
2199 _bfd_error_handler
2200 /* xgettext:c-format */
2201 (_("%B: warning: multiple dynamic symbol tables detected"
2202 " - ignoring the table in section %u"),
2203 abfd, shindex);
2204 goto success;
2206 elf_dynsymtab (abfd) = shindex;
2207 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2208 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2209 abfd->flags |= HAS_SYMS;
2211 /* Besides being a symbol table, we also treat this as a regular
2212 section, so that objcopy can handle it. */
2213 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2214 goto success;
2216 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2218 elf_section_list * entry;
2220 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2221 if (entry->ndx == shindex)
2222 goto success;
2224 entry = bfd_alloc (abfd, sizeof * entry);
2225 if (entry == NULL)
2226 goto fail;
2227 entry->ndx = shindex;
2228 entry->hdr = * hdr;
2229 entry->next = elf_symtab_shndx_list (abfd);
2230 elf_symtab_shndx_list (abfd) = entry;
2231 elf_elfsections (abfd)[shindex] = & entry->hdr;
2232 goto success;
2235 case SHT_STRTAB: /* A string table. */
2236 if (hdr->bfd_section != NULL)
2237 goto success;
2239 if (ehdr->e_shstrndx == shindex)
2241 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2242 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2243 goto success;
2246 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2248 symtab_strtab:
2249 elf_tdata (abfd)->strtab_hdr = *hdr;
2250 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2251 goto success;
2254 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2256 dynsymtab_strtab:
2257 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2258 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2259 elf_elfsections (abfd)[shindex] = hdr;
2260 /* We also treat this as a regular section, so that objcopy
2261 can handle it. */
2262 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2263 shindex);
2264 goto success;
2267 /* If the string table isn't one of the above, then treat it as a
2268 regular section. We need to scan all the headers to be sure,
2269 just in case this strtab section appeared before the above. */
2270 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2272 unsigned int i, num_sec;
2274 num_sec = elf_numsections (abfd);
2275 for (i = 1; i < num_sec; i++)
2277 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2278 if (hdr2->sh_link == shindex)
2280 /* Prevent endless recursion on broken objects. */
2281 if (i == shindex)
2282 goto fail;
2283 if (! bfd_section_from_shdr (abfd, i))
2284 goto fail;
2285 if (elf_onesymtab (abfd) == i)
2286 goto symtab_strtab;
2287 if (elf_dynsymtab (abfd) == i)
2288 goto dynsymtab_strtab;
2292 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2293 goto success;
2295 case SHT_REL:
2296 case SHT_RELA:
2297 /* *These* do a lot of work -- but build no sections! */
2299 asection *target_sect;
2300 Elf_Internal_Shdr *hdr2, **p_hdr;
2301 unsigned int num_sec = elf_numsections (abfd);
2302 struct bfd_elf_section_data *esdt;
2304 if (hdr->sh_entsize
2305 != (bfd_size_type) (hdr->sh_type == SHT_REL
2306 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
2307 goto fail;
2309 /* Check for a bogus link to avoid crashing. */
2310 if (hdr->sh_link >= num_sec)
2312 _bfd_error_handler
2313 /* xgettext:c-format */
2314 (_("%B: invalid link %u for reloc section %s (index %u)"),
2315 abfd, hdr->sh_link, name, shindex);
2316 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2317 shindex);
2318 goto success;
2321 /* For some incomprehensible reason Oracle distributes
2322 libraries for Solaris in which some of the objects have
2323 bogus sh_link fields. It would be nice if we could just
2324 reject them, but, unfortunately, some people need to use
2325 them. We scan through the section headers; if we find only
2326 one suitable symbol table, we clobber the sh_link to point
2327 to it. I hope this doesn't break anything.
2329 Don't do it on executable nor shared library. */
2330 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2331 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2332 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2334 unsigned int scan;
2335 int found;
2337 found = 0;
2338 for (scan = 1; scan < num_sec; scan++)
2340 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2341 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2343 if (found != 0)
2345 found = 0;
2346 break;
2348 found = scan;
2351 if (found != 0)
2352 hdr->sh_link = found;
2355 /* Get the symbol table. */
2356 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2357 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2358 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2359 goto fail;
2361 /* If this reloc section does not use the main symbol table we
2362 don't treat it as a reloc section. BFD can't adequately
2363 represent such a section, so at least for now, we don't
2364 try. We just present it as a normal section. We also
2365 can't use it as a reloc section if it points to the null
2366 section, an invalid section, another reloc section, or its
2367 sh_link points to the null section. */
2368 if (hdr->sh_link != elf_onesymtab (abfd)
2369 || hdr->sh_link == SHN_UNDEF
2370 || hdr->sh_info == SHN_UNDEF
2371 || hdr->sh_info >= num_sec
2372 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2373 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2375 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2376 shindex);
2377 goto success;
2380 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2381 goto fail;
2383 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2384 if (target_sect == NULL)
2385 goto fail;
2387 esdt = elf_section_data (target_sect);
2388 if (hdr->sh_type == SHT_RELA)
2389 p_hdr = &esdt->rela.hdr;
2390 else
2391 p_hdr = &esdt->rel.hdr;
2393 /* PR 17512: file: 0b4f81b7. */
2394 if (*p_hdr != NULL)
2395 goto fail;
2396 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2397 if (hdr2 == NULL)
2398 goto fail;
2399 *hdr2 = *hdr;
2400 *p_hdr = hdr2;
2401 elf_elfsections (abfd)[shindex] = hdr2;
2402 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2403 * bed->s->int_rels_per_ext_rel);
2404 target_sect->flags |= SEC_RELOC;
2405 target_sect->relocation = NULL;
2406 target_sect->rel_filepos = hdr->sh_offset;
2407 /* In the section to which the relocations apply, mark whether
2408 its relocations are of the REL or RELA variety. */
2409 if (hdr->sh_size != 0)
2411 if (hdr->sh_type == SHT_RELA)
2412 target_sect->use_rela_p = 1;
2414 abfd->flags |= HAS_RELOC;
2415 goto success;
2418 case SHT_GNU_verdef:
2419 elf_dynverdef (abfd) = shindex;
2420 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2421 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2422 goto success;
2424 case SHT_GNU_versym:
2425 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2426 goto fail;
2428 elf_dynversym (abfd) = shindex;
2429 elf_tdata (abfd)->dynversym_hdr = *hdr;
2430 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2431 goto success;
2433 case SHT_GNU_verneed:
2434 elf_dynverref (abfd) = shindex;
2435 elf_tdata (abfd)->dynverref_hdr = *hdr;
2436 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2437 goto success;
2439 case SHT_SHLIB:
2440 goto success;
2442 case SHT_GROUP:
2443 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2444 goto fail;
2446 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2447 goto fail;
2449 goto success;
2451 default:
2452 /* Possibly an attributes section. */
2453 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2454 || hdr->sh_type == bed->obj_attrs_section_type)
2456 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2457 goto fail;
2458 _bfd_elf_parse_attributes (abfd, hdr);
2459 goto success;
2462 /* Check for any processor-specific section types. */
2463 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2464 goto success;
2466 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2468 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2469 /* FIXME: How to properly handle allocated section reserved
2470 for applications? */
2471 _bfd_error_handler
2472 /* xgettext:c-format */
2473 (_("%B: unknown type [%#x] section `%s'"),
2474 abfd, hdr->sh_type, name);
2475 else
2477 /* Allow sections reserved for applications. */
2478 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2479 shindex);
2480 goto success;
2483 else if (hdr->sh_type >= SHT_LOPROC
2484 && hdr->sh_type <= SHT_HIPROC)
2485 /* FIXME: We should handle this section. */
2486 _bfd_error_handler
2487 /* xgettext:c-format */
2488 (_("%B: unknown type [%#x] section `%s'"),
2489 abfd, hdr->sh_type, name);
2490 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2492 /* Unrecognised OS-specific sections. */
2493 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2494 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2495 required to correctly process the section and the file should
2496 be rejected with an error message. */
2497 _bfd_error_handler
2498 /* xgettext:c-format */
2499 (_("%B: unknown type [%#x] section `%s'"),
2500 abfd, hdr->sh_type, name);
2501 else
2503 /* Otherwise it should be processed. */
2504 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2505 goto success;
2508 else
2509 /* FIXME: We should handle this section. */
2510 _bfd_error_handler
2511 /* xgettext:c-format */
2512 (_("%B: unknown type [%#x] section `%s'"),
2513 abfd, hdr->sh_type, name);
2515 goto fail;
2518 fail:
2519 ret = FALSE;
2520 success:
2521 if (sections_being_created && sections_being_created_abfd == abfd)
2522 sections_being_created [shindex] = FALSE;
2523 if (-- nesting == 0)
2525 sections_being_created = NULL;
2526 sections_being_created_abfd = abfd;
2528 return ret;
2531 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2533 Elf_Internal_Sym *
2534 bfd_sym_from_r_symndx (struct sym_cache *cache,
2535 bfd *abfd,
2536 unsigned long r_symndx)
2538 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2540 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2542 Elf_Internal_Shdr *symtab_hdr;
2543 unsigned char esym[sizeof (Elf64_External_Sym)];
2544 Elf_External_Sym_Shndx eshndx;
2546 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2547 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2548 &cache->sym[ent], esym, &eshndx) == NULL)
2549 return NULL;
2551 if (cache->abfd != abfd)
2553 memset (cache->indx, -1, sizeof (cache->indx));
2554 cache->abfd = abfd;
2556 cache->indx[ent] = r_symndx;
2559 return &cache->sym[ent];
2562 /* Given an ELF section number, retrieve the corresponding BFD
2563 section. */
2565 asection *
2566 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2568 if (sec_index >= elf_numsections (abfd))
2569 return NULL;
2570 return elf_elfsections (abfd)[sec_index]->bfd_section;
2573 static const struct bfd_elf_special_section special_sections_b[] =
2575 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2576 { NULL, 0, 0, 0, 0 }
2579 static const struct bfd_elf_special_section special_sections_c[] =
2581 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2582 { NULL, 0, 0, 0, 0 }
2585 static const struct bfd_elf_special_section special_sections_d[] =
2587 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2588 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2589 /* There are more DWARF sections than these, but they needn't be added here
2590 unless you have to cope with broken compilers that don't emit section
2591 attributes or you want to help the user writing assembler. */
2592 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2593 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2594 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2595 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2596 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2597 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2598 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2599 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2600 { NULL, 0, 0, 0, 0 }
2603 static const struct bfd_elf_special_section special_sections_f[] =
2605 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2606 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2607 { NULL, 0 , 0, 0, 0 }
2610 static const struct bfd_elf_special_section special_sections_g[] =
2612 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2613 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2614 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2615 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2616 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2617 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2618 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2619 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2620 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2621 { NULL, 0, 0, 0, 0 }
2624 static const struct bfd_elf_special_section special_sections_h[] =
2626 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2627 { NULL, 0, 0, 0, 0 }
2630 static const struct bfd_elf_special_section special_sections_i[] =
2632 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2633 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2634 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2635 { NULL, 0, 0, 0, 0 }
2638 static const struct bfd_elf_special_section special_sections_l[] =
2640 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2641 { NULL, 0, 0, 0, 0 }
2644 static const struct bfd_elf_special_section special_sections_n[] =
2646 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2647 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2648 { NULL, 0, 0, 0, 0 }
2651 static const struct bfd_elf_special_section special_sections_p[] =
2653 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2654 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2655 { NULL, 0, 0, 0, 0 }
2658 static const struct bfd_elf_special_section special_sections_r[] =
2660 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2661 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2662 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2663 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2664 { NULL, 0, 0, 0, 0 }
2667 static const struct bfd_elf_special_section special_sections_s[] =
2669 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2670 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2671 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2672 /* See struct bfd_elf_special_section declaration for the semantics of
2673 this special case where .prefix_length != strlen (.prefix). */
2674 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2675 { NULL, 0, 0, 0, 0 }
2678 static const struct bfd_elf_special_section special_sections_t[] =
2680 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2681 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2682 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2683 { NULL, 0, 0, 0, 0 }
2686 static const struct bfd_elf_special_section special_sections_z[] =
2688 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2689 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2690 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2691 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2692 { NULL, 0, 0, 0, 0 }
2695 static const struct bfd_elf_special_section * const special_sections[] =
2697 special_sections_b, /* 'b' */
2698 special_sections_c, /* 'c' */
2699 special_sections_d, /* 'd' */
2700 NULL, /* 'e' */
2701 special_sections_f, /* 'f' */
2702 special_sections_g, /* 'g' */
2703 special_sections_h, /* 'h' */
2704 special_sections_i, /* 'i' */
2705 NULL, /* 'j' */
2706 NULL, /* 'k' */
2707 special_sections_l, /* 'l' */
2708 NULL, /* 'm' */
2709 special_sections_n, /* 'n' */
2710 NULL, /* 'o' */
2711 special_sections_p, /* 'p' */
2712 NULL, /* 'q' */
2713 special_sections_r, /* 'r' */
2714 special_sections_s, /* 's' */
2715 special_sections_t, /* 't' */
2716 NULL, /* 'u' */
2717 NULL, /* 'v' */
2718 NULL, /* 'w' */
2719 NULL, /* 'x' */
2720 NULL, /* 'y' */
2721 special_sections_z /* 'z' */
2724 const struct bfd_elf_special_section *
2725 _bfd_elf_get_special_section (const char *name,
2726 const struct bfd_elf_special_section *spec,
2727 unsigned int rela)
2729 int i;
2730 int len;
2732 len = strlen (name);
2734 for (i = 0; spec[i].prefix != NULL; i++)
2736 int suffix_len;
2737 int prefix_len = spec[i].prefix_length;
2739 if (len < prefix_len)
2740 continue;
2741 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2742 continue;
2744 suffix_len = spec[i].suffix_length;
2745 if (suffix_len <= 0)
2747 if (name[prefix_len] != 0)
2749 if (suffix_len == 0)
2750 continue;
2751 if (name[prefix_len] != '.'
2752 && (suffix_len == -2
2753 || (rela && spec[i].type == SHT_REL)))
2754 continue;
2757 else
2759 if (len < prefix_len + suffix_len)
2760 continue;
2761 if (memcmp (name + len - suffix_len,
2762 spec[i].prefix + prefix_len,
2763 suffix_len) != 0)
2764 continue;
2766 return &spec[i];
2769 return NULL;
2772 const struct bfd_elf_special_section *
2773 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2775 int i;
2776 const struct bfd_elf_special_section *spec;
2777 const struct elf_backend_data *bed;
2779 /* See if this is one of the special sections. */
2780 if (sec->name == NULL)
2781 return NULL;
2783 bed = get_elf_backend_data (abfd);
2784 spec = bed->special_sections;
2785 if (spec)
2787 spec = _bfd_elf_get_special_section (sec->name,
2788 bed->special_sections,
2789 sec->use_rela_p);
2790 if (spec != NULL)
2791 return spec;
2794 if (sec->name[0] != '.')
2795 return NULL;
2797 i = sec->name[1] - 'b';
2798 if (i < 0 || i > 'z' - 'b')
2799 return NULL;
2801 spec = special_sections[i];
2803 if (spec == NULL)
2804 return NULL;
2806 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2809 bfd_boolean
2810 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2812 struct bfd_elf_section_data *sdata;
2813 const struct elf_backend_data *bed;
2814 const struct bfd_elf_special_section *ssect;
2816 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2817 if (sdata == NULL)
2819 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2820 sizeof (*sdata));
2821 if (sdata == NULL)
2822 return FALSE;
2823 sec->used_by_bfd = sdata;
2826 /* Indicate whether or not this section should use RELA relocations. */
2827 bed = get_elf_backend_data (abfd);
2828 sec->use_rela_p = bed->default_use_rela_p;
2830 /* When we read a file, we don't need to set ELF section type and
2831 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2832 anyway. We will set ELF section type and flags for all linker
2833 created sections. If user specifies BFD section flags, we will
2834 set ELF section type and flags based on BFD section flags in
2835 elf_fake_sections. Special handling for .init_array/.fini_array
2836 output sections since they may contain .ctors/.dtors input
2837 sections. We don't want _bfd_elf_init_private_section_data to
2838 copy ELF section type from .ctors/.dtors input sections. */
2839 if (abfd->direction != read_direction
2840 || (sec->flags & SEC_LINKER_CREATED) != 0)
2842 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2843 if (ssect != NULL
2844 && (!sec->flags
2845 || (sec->flags & SEC_LINKER_CREATED) != 0
2846 || ssect->type == SHT_INIT_ARRAY
2847 || ssect->type == SHT_FINI_ARRAY))
2849 elf_section_type (sec) = ssect->type;
2850 elf_section_flags (sec) = ssect->attr;
2854 return _bfd_generic_new_section_hook (abfd, sec);
2857 /* Create a new bfd section from an ELF program header.
2859 Since program segments have no names, we generate a synthetic name
2860 of the form segment<NUM>, where NUM is generally the index in the
2861 program header table. For segments that are split (see below) we
2862 generate the names segment<NUM>a and segment<NUM>b.
2864 Note that some program segments may have a file size that is different than
2865 (less than) the memory size. All this means is that at execution the
2866 system must allocate the amount of memory specified by the memory size,
2867 but only initialize it with the first "file size" bytes read from the
2868 file. This would occur for example, with program segments consisting
2869 of combined data+bss.
2871 To handle the above situation, this routine generates TWO bfd sections
2872 for the single program segment. The first has the length specified by
2873 the file size of the segment, and the second has the length specified
2874 by the difference between the two sizes. In effect, the segment is split
2875 into its initialized and uninitialized parts.
2879 bfd_boolean
2880 _bfd_elf_make_section_from_phdr (bfd *abfd,
2881 Elf_Internal_Phdr *hdr,
2882 int hdr_index,
2883 const char *type_name)
2885 asection *newsect;
2886 char *name;
2887 char namebuf[64];
2888 size_t len;
2889 int split;
2891 split = ((hdr->p_memsz > 0)
2892 && (hdr->p_filesz > 0)
2893 && (hdr->p_memsz > hdr->p_filesz));
2895 if (hdr->p_filesz > 0)
2897 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2898 len = strlen (namebuf) + 1;
2899 name = (char *) bfd_alloc (abfd, len);
2900 if (!name)
2901 return FALSE;
2902 memcpy (name, namebuf, len);
2903 newsect = bfd_make_section (abfd, name);
2904 if (newsect == NULL)
2905 return FALSE;
2906 newsect->vma = hdr->p_vaddr;
2907 newsect->lma = hdr->p_paddr;
2908 newsect->size = hdr->p_filesz;
2909 newsect->filepos = hdr->p_offset;
2910 newsect->flags |= SEC_HAS_CONTENTS;
2911 newsect->alignment_power = bfd_log2 (hdr->p_align);
2912 if (hdr->p_type == PT_LOAD)
2914 newsect->flags |= SEC_ALLOC;
2915 newsect->flags |= SEC_LOAD;
2916 if (hdr->p_flags & PF_X)
2918 /* FIXME: all we known is that it has execute PERMISSION,
2919 may be data. */
2920 newsect->flags |= SEC_CODE;
2923 if (!(hdr->p_flags & PF_W))
2925 newsect->flags |= SEC_READONLY;
2929 if (hdr->p_memsz > hdr->p_filesz)
2931 bfd_vma align;
2933 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2934 len = strlen (namebuf) + 1;
2935 name = (char *) bfd_alloc (abfd, len);
2936 if (!name)
2937 return FALSE;
2938 memcpy (name, namebuf, len);
2939 newsect = bfd_make_section (abfd, name);
2940 if (newsect == NULL)
2941 return FALSE;
2942 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2943 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2944 newsect->size = hdr->p_memsz - hdr->p_filesz;
2945 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2946 align = newsect->vma & -newsect->vma;
2947 if (align == 0 || align > hdr->p_align)
2948 align = hdr->p_align;
2949 newsect->alignment_power = bfd_log2 (align);
2950 if (hdr->p_type == PT_LOAD)
2952 /* Hack for gdb. Segments that have not been modified do
2953 not have their contents written to a core file, on the
2954 assumption that a debugger can find the contents in the
2955 executable. We flag this case by setting the fake
2956 section size to zero. Note that "real" bss sections will
2957 always have their contents dumped to the core file. */
2958 if (bfd_get_format (abfd) == bfd_core)
2959 newsect->size = 0;
2960 newsect->flags |= SEC_ALLOC;
2961 if (hdr->p_flags & PF_X)
2962 newsect->flags |= SEC_CODE;
2964 if (!(hdr->p_flags & PF_W))
2965 newsect->flags |= SEC_READONLY;
2968 return TRUE;
2971 bfd_boolean
2972 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2974 const struct elf_backend_data *bed;
2976 switch (hdr->p_type)
2978 case PT_NULL:
2979 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2981 case PT_LOAD:
2982 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2984 case PT_DYNAMIC:
2985 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2987 case PT_INTERP:
2988 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2990 case PT_NOTE:
2991 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2992 return FALSE;
2993 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2994 return FALSE;
2995 return TRUE;
2997 case PT_SHLIB:
2998 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3000 case PT_PHDR:
3001 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3003 case PT_GNU_EH_FRAME:
3004 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3005 "eh_frame_hdr");
3007 case PT_GNU_STACK:
3008 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3010 case PT_GNU_RELRO:
3011 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3013 default:
3014 /* Check for any processor-specific program segment types. */
3015 bed = get_elf_backend_data (abfd);
3016 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3020 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3021 REL or RELA. */
3023 Elf_Internal_Shdr *
3024 _bfd_elf_single_rel_hdr (asection *sec)
3026 if (elf_section_data (sec)->rel.hdr)
3028 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3029 return elf_section_data (sec)->rel.hdr;
3031 else
3032 return elf_section_data (sec)->rela.hdr;
3035 static bfd_boolean
3036 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3037 Elf_Internal_Shdr *rel_hdr,
3038 const char *sec_name,
3039 bfd_boolean use_rela_p)
3041 char *name = (char *) bfd_alloc (abfd,
3042 sizeof ".rela" + strlen (sec_name));
3043 if (name == NULL)
3044 return FALSE;
3046 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3047 rel_hdr->sh_name =
3048 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3049 FALSE);
3050 if (rel_hdr->sh_name == (unsigned int) -1)
3051 return FALSE;
3053 return TRUE;
3056 /* Allocate and initialize a section-header for a new reloc section,
3057 containing relocations against ASECT. It is stored in RELDATA. If
3058 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3059 relocations. */
3061 static bfd_boolean
3062 _bfd_elf_init_reloc_shdr (bfd *abfd,
3063 struct bfd_elf_section_reloc_data *reldata,
3064 const Elf_Internal_Shdr *sec_hdr,
3065 const char *sec_name,
3066 bfd_boolean use_rela_p,
3067 bfd_boolean delay_st_name_p)
3069 Elf_Internal_Shdr *rel_hdr;
3070 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3072 BFD_ASSERT (reldata->hdr == NULL);
3073 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3074 reldata->hdr = rel_hdr;
3076 if (delay_st_name_p)
3077 rel_hdr->sh_name = (unsigned int) -1;
3078 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3079 use_rela_p))
3080 return FALSE;
3081 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3082 rel_hdr->sh_entsize = (use_rela_p
3083 ? bed->s->sizeof_rela
3084 : bed->s->sizeof_rel);
3085 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3086 rel_hdr->sh_flags = sec_hdr->sh_flags & SHF_GROUP;
3087 rel_hdr->sh_addr = 0;
3088 rel_hdr->sh_size = 0;
3089 rel_hdr->sh_offset = 0;
3091 return TRUE;
3094 /* Return the default section type based on the passed in section flags. */
3097 bfd_elf_get_default_section_type (flagword flags)
3099 if ((flags & SEC_ALLOC) != 0
3100 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3101 return SHT_NOBITS;
3102 return SHT_PROGBITS;
3105 struct fake_section_arg
3107 struct bfd_link_info *link_info;
3108 bfd_boolean failed;
3111 /* Set up an ELF internal section header for a section. */
3113 static void
3114 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3116 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3117 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3118 struct bfd_elf_section_data *esd = elf_section_data (asect);
3119 Elf_Internal_Shdr *this_hdr;
3120 unsigned int sh_type;
3121 const char *name = asect->name;
3122 bfd_boolean delay_st_name_p = FALSE;
3124 if (arg->failed)
3126 /* We already failed; just get out of the bfd_map_over_sections
3127 loop. */
3128 return;
3131 this_hdr = &esd->this_hdr;
3133 if (arg->link_info)
3135 /* ld: compress DWARF debug sections with names: .debug_*. */
3136 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3137 && (asect->flags & SEC_DEBUGGING)
3138 && name[1] == 'd'
3139 && name[6] == '_')
3141 /* Set SEC_ELF_COMPRESS to indicate this section should be
3142 compressed. */
3143 asect->flags |= SEC_ELF_COMPRESS;
3145 /* If this section will be compressed, delay adding section
3146 name to section name section after it is compressed in
3147 _bfd_elf_assign_file_positions_for_non_load. */
3148 delay_st_name_p = TRUE;
3151 else if ((asect->flags & SEC_ELF_RENAME))
3153 /* objcopy: rename output DWARF debug section. */
3154 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3156 /* When we decompress or compress with SHF_COMPRESSED,
3157 convert section name from .zdebug_* to .debug_* if
3158 needed. */
3159 if (name[1] == 'z')
3161 char *new_name = convert_zdebug_to_debug (abfd, name);
3162 if (new_name == NULL)
3164 arg->failed = TRUE;
3165 return;
3167 name = new_name;
3170 else if (asect->compress_status == COMPRESS_SECTION_DONE)
3172 /* PR binutils/18087: Compression does not always make a
3173 section smaller. So only rename the section when
3174 compression has actually taken place. If input section
3175 name is .zdebug_*, we should never compress it again. */
3176 char *new_name = convert_debug_to_zdebug (abfd, name);
3177 if (new_name == NULL)
3179 arg->failed = TRUE;
3180 return;
3182 BFD_ASSERT (name[1] != 'z');
3183 name = new_name;
3187 if (delay_st_name_p)
3188 this_hdr->sh_name = (unsigned int) -1;
3189 else
3191 this_hdr->sh_name
3192 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3193 name, FALSE);
3194 if (this_hdr->sh_name == (unsigned int) -1)
3196 arg->failed = TRUE;
3197 return;
3201 /* Don't clear sh_flags. Assembler may set additional bits. */
3203 if ((asect->flags & SEC_ALLOC) != 0
3204 || asect->user_set_vma)
3205 this_hdr->sh_addr = asect->vma;
3206 else
3207 this_hdr->sh_addr = 0;
3209 this_hdr->sh_offset = 0;
3210 this_hdr->sh_size = asect->size;
3211 this_hdr->sh_link = 0;
3212 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3213 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3215 _bfd_error_handler
3216 /* xgettext:c-format */
3217 (_("%B: error: Alignment power %d of section `%A' is too big"),
3218 abfd, asect->alignment_power, asect);
3219 arg->failed = TRUE;
3220 return;
3222 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
3223 /* The sh_entsize and sh_info fields may have been set already by
3224 copy_private_section_data. */
3226 this_hdr->bfd_section = asect;
3227 this_hdr->contents = NULL;
3229 /* If the section type is unspecified, we set it based on
3230 asect->flags. */
3231 if ((asect->flags & SEC_GROUP) != 0)
3232 sh_type = SHT_GROUP;
3233 else
3234 sh_type = bfd_elf_get_default_section_type (asect->flags);
3236 if (this_hdr->sh_type == SHT_NULL)
3237 this_hdr->sh_type = sh_type;
3238 else if (this_hdr->sh_type == SHT_NOBITS
3239 && sh_type == SHT_PROGBITS
3240 && (asect->flags & SEC_ALLOC) != 0)
3242 /* Warn if we are changing a NOBITS section to PROGBITS, but
3243 allow the link to proceed. This can happen when users link
3244 non-bss input sections to bss output sections, or emit data
3245 to a bss output section via a linker script. */
3246 _bfd_error_handler
3247 (_("warning: section `%A' type changed to PROGBITS"), asect);
3248 this_hdr->sh_type = sh_type;
3251 switch (this_hdr->sh_type)
3253 default:
3254 break;
3256 case SHT_STRTAB:
3257 case SHT_NOTE:
3258 case SHT_NOBITS:
3259 case SHT_PROGBITS:
3260 break;
3262 case SHT_INIT_ARRAY:
3263 case SHT_FINI_ARRAY:
3264 case SHT_PREINIT_ARRAY:
3265 this_hdr->sh_entsize = bed->s->arch_size / 8;
3266 break;
3268 case SHT_HASH:
3269 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3270 break;
3272 case SHT_DYNSYM:
3273 this_hdr->sh_entsize = bed->s->sizeof_sym;
3274 break;
3276 case SHT_DYNAMIC:
3277 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3278 break;
3280 case SHT_RELA:
3281 if (get_elf_backend_data (abfd)->may_use_rela_p)
3282 this_hdr->sh_entsize = bed->s->sizeof_rela;
3283 break;
3285 case SHT_REL:
3286 if (get_elf_backend_data (abfd)->may_use_rel_p)
3287 this_hdr->sh_entsize = bed->s->sizeof_rel;
3288 break;
3290 case SHT_GNU_versym:
3291 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3292 break;
3294 case SHT_GNU_verdef:
3295 this_hdr->sh_entsize = 0;
3296 /* objcopy or strip will copy over sh_info, but may not set
3297 cverdefs. The linker will set cverdefs, but sh_info will be
3298 zero. */
3299 if (this_hdr->sh_info == 0)
3300 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3301 else
3302 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3303 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3304 break;
3306 case SHT_GNU_verneed:
3307 this_hdr->sh_entsize = 0;
3308 /* objcopy or strip will copy over sh_info, but may not set
3309 cverrefs. The linker will set cverrefs, but sh_info will be
3310 zero. */
3311 if (this_hdr->sh_info == 0)
3312 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3313 else
3314 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3315 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3316 break;
3318 case SHT_GROUP:
3319 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3320 break;
3322 case SHT_GNU_HASH:
3323 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3324 break;
3327 if ((asect->flags & SEC_ALLOC) != 0)
3328 this_hdr->sh_flags |= SHF_ALLOC;
3329 if ((asect->flags & SEC_READONLY) == 0)
3330 this_hdr->sh_flags |= SHF_WRITE;
3331 if ((asect->flags & SEC_CODE) != 0)
3332 this_hdr->sh_flags |= SHF_EXECINSTR;
3333 if ((asect->flags & SEC_MERGE) != 0)
3335 this_hdr->sh_flags |= SHF_MERGE;
3336 this_hdr->sh_entsize = asect->entsize;
3338 if ((asect->flags & SEC_STRINGS) != 0)
3339 this_hdr->sh_flags |= SHF_STRINGS;
3340 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3341 this_hdr->sh_flags |= SHF_GROUP;
3342 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3344 this_hdr->sh_flags |= SHF_TLS;
3345 if (asect->size == 0
3346 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3348 struct bfd_link_order *o = asect->map_tail.link_order;
3350 this_hdr->sh_size = 0;
3351 if (o != NULL)
3353 this_hdr->sh_size = o->offset + o->size;
3354 if (this_hdr->sh_size != 0)
3355 this_hdr->sh_type = SHT_NOBITS;
3359 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3360 this_hdr->sh_flags |= SHF_EXCLUDE;
3362 /* If the section has relocs, set up a section header for the
3363 SHT_REL[A] section. If two relocation sections are required for
3364 this section, it is up to the processor-specific back-end to
3365 create the other. */
3366 if ((asect->flags & SEC_RELOC) != 0)
3368 /* When doing a relocatable link, create both REL and RELA sections if
3369 needed. */
3370 if (arg->link_info
3371 /* Do the normal setup if we wouldn't create any sections here. */
3372 && esd->rel.count + esd->rela.count > 0
3373 && (bfd_link_relocatable (arg->link_info)
3374 || arg->link_info->emitrelocations))
3376 if (esd->rel.count && esd->rel.hdr == NULL
3377 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, this_hdr, name,
3378 FALSE, delay_st_name_p))
3380 arg->failed = TRUE;
3381 return;
3383 if (esd->rela.count && esd->rela.hdr == NULL
3384 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, this_hdr, name,
3385 TRUE, delay_st_name_p))
3387 arg->failed = TRUE;
3388 return;
3391 else if (!_bfd_elf_init_reloc_shdr (abfd,
3392 (asect->use_rela_p
3393 ? &esd->rela : &esd->rel),
3394 this_hdr,
3395 name,
3396 asect->use_rela_p,
3397 delay_st_name_p))
3399 arg->failed = TRUE;
3400 return;
3404 /* Check for processor-specific section types. */
3405 sh_type = this_hdr->sh_type;
3406 if (bed->elf_backend_fake_sections
3407 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3409 arg->failed = TRUE;
3410 return;
3413 if (sh_type == SHT_NOBITS && asect->size != 0)
3415 /* Don't change the header type from NOBITS if we are being
3416 called for objcopy --only-keep-debug. */
3417 this_hdr->sh_type = sh_type;
3421 /* Fill in the contents of a SHT_GROUP section. Called from
3422 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3423 when ELF targets use the generic linker, ld. Called for ld -r
3424 from bfd_elf_final_link. */
3426 void
3427 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3429 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
3430 asection *elt, *first;
3431 unsigned char *loc;
3432 bfd_boolean gas;
3434 /* Ignore linker created group section. See elfNN_ia64_object_p in
3435 elfxx-ia64.c. */
3436 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
3437 || *failedptr)
3438 return;
3440 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3442 unsigned long symindx = 0;
3444 /* elf_group_id will have been set up by objcopy and the
3445 generic linker. */
3446 if (elf_group_id (sec) != NULL)
3447 symindx = elf_group_id (sec)->udata.i;
3449 if (symindx == 0)
3451 /* If called from the assembler, swap_out_syms will have set up
3452 elf_section_syms. */
3453 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3454 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3456 elf_section_data (sec)->this_hdr.sh_info = symindx;
3458 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3460 /* The ELF backend linker sets sh_info to -2 when the group
3461 signature symbol is global, and thus the index can't be
3462 set until all local symbols are output. */
3463 asection *igroup;
3464 struct bfd_elf_section_data *sec_data;
3465 unsigned long symndx;
3466 unsigned long extsymoff;
3467 struct elf_link_hash_entry *h;
3469 /* The point of this little dance to the first SHF_GROUP section
3470 then back to the SHT_GROUP section is that this gets us to
3471 the SHT_GROUP in the input object. */
3472 igroup = elf_sec_group (elf_next_in_group (sec));
3473 sec_data = elf_section_data (igroup);
3474 symndx = sec_data->this_hdr.sh_info;
3475 extsymoff = 0;
3476 if (!elf_bad_symtab (igroup->owner))
3478 Elf_Internal_Shdr *symtab_hdr;
3480 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3481 extsymoff = symtab_hdr->sh_info;
3483 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3484 while (h->root.type == bfd_link_hash_indirect
3485 || h->root.type == bfd_link_hash_warning)
3486 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3488 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3491 /* The contents won't be allocated for "ld -r" or objcopy. */
3492 gas = TRUE;
3493 if (sec->contents == NULL)
3495 gas = FALSE;
3496 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3498 /* Arrange for the section to be written out. */
3499 elf_section_data (sec)->this_hdr.contents = sec->contents;
3500 if (sec->contents == NULL)
3502 *failedptr = TRUE;
3503 return;
3507 loc = sec->contents + sec->size;
3509 /* Get the pointer to the first section in the group that gas
3510 squirreled away here. objcopy arranges for this to be set to the
3511 start of the input section group. */
3512 first = elt = elf_next_in_group (sec);
3514 /* First element is a flag word. Rest of section is elf section
3515 indices for all the sections of the group. Write them backwards
3516 just to keep the group in the same order as given in .section
3517 directives, not that it matters. */
3518 while (elt != NULL)
3520 asection *s;
3522 s = elt;
3523 if (!gas)
3524 s = s->output_section;
3525 if (s != NULL
3526 && !bfd_is_abs_section (s))
3528 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3529 if (elf_sec->rel.hdr != NULL)
3531 loc -= 4;
3532 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3534 if (elf_sec->rela.hdr != NULL)
3536 loc -= 4;
3537 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3539 loc -= 4;
3540 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3542 elt = elf_next_in_group (elt);
3543 if (elt == first)
3544 break;
3547 loc -= 4;
3548 BFD_ASSERT (loc == sec->contents);
3550 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3553 /* Given NAME, the name of a relocation section stripped of its
3554 .rel/.rela prefix, return the section in ABFD to which the
3555 relocations apply. */
3557 asection *
3558 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3560 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3561 section likely apply to .got.plt or .got section. */
3562 if (get_elf_backend_data (abfd)->want_got_plt
3563 && strcmp (name, ".plt") == 0)
3565 asection *sec;
3567 name = ".got.plt";
3568 sec = bfd_get_section_by_name (abfd, name);
3569 if (sec != NULL)
3570 return sec;
3571 name = ".got";
3574 return bfd_get_section_by_name (abfd, name);
3577 /* Return the section to which RELOC_SEC applies. */
3579 static asection *
3580 elf_get_reloc_section (asection *reloc_sec)
3582 const char *name;
3583 unsigned int type;
3584 bfd *abfd;
3585 const struct elf_backend_data *bed;
3587 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3588 if (type != SHT_REL && type != SHT_RELA)
3589 return NULL;
3591 /* We look up the section the relocs apply to by name. */
3592 name = reloc_sec->name;
3593 if (strncmp (name, ".rel", 4) != 0)
3594 return NULL;
3595 name += 4;
3596 if (type == SHT_RELA && *name++ != 'a')
3597 return NULL;
3599 abfd = reloc_sec->owner;
3600 bed = get_elf_backend_data (abfd);
3601 return bed->get_reloc_section (abfd, name);
3604 /* Assign all ELF section numbers. The dummy first section is handled here
3605 too. The link/info pointers for the standard section types are filled
3606 in here too, while we're at it. */
3608 static bfd_boolean
3609 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3611 struct elf_obj_tdata *t = elf_tdata (abfd);
3612 asection *sec;
3613 unsigned int section_number;
3614 Elf_Internal_Shdr **i_shdrp;
3615 struct bfd_elf_section_data *d;
3616 bfd_boolean need_symtab;
3618 section_number = 1;
3620 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3622 /* SHT_GROUP sections are in relocatable files only. */
3623 if (link_info == NULL || !link_info->resolve_section_groups)
3625 size_t reloc_count = 0;
3627 /* Put SHT_GROUP sections first. */
3628 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3630 d = elf_section_data (sec);
3632 if (d->this_hdr.sh_type == SHT_GROUP)
3634 if (sec->flags & SEC_LINKER_CREATED)
3636 /* Remove the linker created SHT_GROUP sections. */
3637 bfd_section_list_remove (abfd, sec);
3638 abfd->section_count--;
3640 else
3641 d->this_idx = section_number++;
3644 /* Count relocations. */
3645 reloc_count += sec->reloc_count;
3648 /* Clear HAS_RELOC if there are no relocations. */
3649 if (reloc_count == 0)
3650 abfd->flags &= ~HAS_RELOC;
3653 for (sec = abfd->sections; sec; sec = sec->next)
3655 d = elf_section_data (sec);
3657 if (d->this_hdr.sh_type != SHT_GROUP)
3658 d->this_idx = section_number++;
3659 if (d->this_hdr.sh_name != (unsigned int) -1)
3660 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3661 if (d->rel.hdr)
3663 d->rel.idx = section_number++;
3664 if (d->rel.hdr->sh_name != (unsigned int) -1)
3665 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3667 else
3668 d->rel.idx = 0;
3670 if (d->rela.hdr)
3672 d->rela.idx = section_number++;
3673 if (d->rela.hdr->sh_name != (unsigned int) -1)
3674 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3676 else
3677 d->rela.idx = 0;
3680 need_symtab = (bfd_get_symcount (abfd) > 0
3681 || (link_info == NULL
3682 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3683 == HAS_RELOC)));
3684 if (need_symtab)
3686 elf_onesymtab (abfd) = section_number++;
3687 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3688 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3690 elf_section_list * entry;
3692 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3694 entry = bfd_zalloc (abfd, sizeof * entry);
3695 entry->ndx = section_number++;
3696 elf_symtab_shndx_list (abfd) = entry;
3697 entry->hdr.sh_name
3698 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3699 ".symtab_shndx", FALSE);
3700 if (entry->hdr.sh_name == (unsigned int) -1)
3701 return FALSE;
3703 elf_strtab_sec (abfd) = section_number++;
3704 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3707 elf_shstrtab_sec (abfd) = section_number++;
3708 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3709 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3711 if (section_number >= SHN_LORESERVE)
3713 /* xgettext:c-format */
3714 _bfd_error_handler (_("%B: too many sections: %u"),
3715 abfd, section_number);
3716 return FALSE;
3719 elf_numsections (abfd) = section_number;
3720 elf_elfheader (abfd)->e_shnum = section_number;
3722 /* Set up the list of section header pointers, in agreement with the
3723 indices. */
3724 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3725 sizeof (Elf_Internal_Shdr *));
3726 if (i_shdrp == NULL)
3727 return FALSE;
3729 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3730 sizeof (Elf_Internal_Shdr));
3731 if (i_shdrp[0] == NULL)
3733 bfd_release (abfd, i_shdrp);
3734 return FALSE;
3737 elf_elfsections (abfd) = i_shdrp;
3739 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3740 if (need_symtab)
3742 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3743 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3745 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3746 BFD_ASSERT (entry != NULL);
3747 i_shdrp[entry->ndx] = & entry->hdr;
3748 entry->hdr.sh_link = elf_onesymtab (abfd);
3750 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3751 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3754 for (sec = abfd->sections; sec; sec = sec->next)
3756 asection *s;
3758 d = elf_section_data (sec);
3760 i_shdrp[d->this_idx] = &d->this_hdr;
3761 if (d->rel.idx != 0)
3762 i_shdrp[d->rel.idx] = d->rel.hdr;
3763 if (d->rela.idx != 0)
3764 i_shdrp[d->rela.idx] = d->rela.hdr;
3766 /* Fill in the sh_link and sh_info fields while we're at it. */
3768 /* sh_link of a reloc section is the section index of the symbol
3769 table. sh_info is the section index of the section to which
3770 the relocation entries apply. */
3771 if (d->rel.idx != 0)
3773 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3774 d->rel.hdr->sh_info = d->this_idx;
3775 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3777 if (d->rela.idx != 0)
3779 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3780 d->rela.hdr->sh_info = d->this_idx;
3781 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3784 /* We need to set up sh_link for SHF_LINK_ORDER. */
3785 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3787 s = elf_linked_to_section (sec);
3788 if (s)
3790 /* elf_linked_to_section points to the input section. */
3791 if (link_info != NULL)
3793 /* Check discarded linkonce section. */
3794 if (discarded_section (s))
3796 asection *kept;
3797 _bfd_error_handler
3798 /* xgettext:c-format */
3799 (_("%B: sh_link of section `%A' points to"
3800 " discarded section `%A' of `%B'"),
3801 abfd, d->this_hdr.bfd_section,
3802 s, s->owner);
3803 /* Point to the kept section if it has the same
3804 size as the discarded one. */
3805 kept = _bfd_elf_check_kept_section (s, link_info);
3806 if (kept == NULL)
3808 bfd_set_error (bfd_error_bad_value);
3809 return FALSE;
3811 s = kept;
3814 s = s->output_section;
3815 BFD_ASSERT (s != NULL);
3817 else
3819 /* Handle objcopy. */
3820 if (s->output_section == NULL)
3822 _bfd_error_handler
3823 /* xgettext:c-format */
3824 (_("%B: sh_link of section `%A' points to"
3825 " removed section `%A' of `%B'"),
3826 abfd, d->this_hdr.bfd_section, s, s->owner);
3827 bfd_set_error (bfd_error_bad_value);
3828 return FALSE;
3830 s = s->output_section;
3832 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3834 else
3836 /* PR 290:
3837 The Intel C compiler generates SHT_IA_64_UNWIND with
3838 SHF_LINK_ORDER. But it doesn't set the sh_link or
3839 sh_info fields. Hence we could get the situation
3840 where s is NULL. */
3841 const struct elf_backend_data *bed
3842 = get_elf_backend_data (abfd);
3843 if (bed->link_order_error_handler)
3844 bed->link_order_error_handler
3845 /* xgettext:c-format */
3846 (_("%B: warning: sh_link not set for section `%A'"),
3847 abfd, sec);
3851 switch (d->this_hdr.sh_type)
3853 case SHT_REL:
3854 case SHT_RELA:
3855 /* A reloc section which we are treating as a normal BFD
3856 section. sh_link is the section index of the symbol
3857 table. sh_info is the section index of the section to
3858 which the relocation entries apply. We assume that an
3859 allocated reloc section uses the dynamic symbol table.
3860 FIXME: How can we be sure? */
3861 s = bfd_get_section_by_name (abfd, ".dynsym");
3862 if (s != NULL)
3863 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3865 s = elf_get_reloc_section (sec);
3866 if (s != NULL)
3868 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3869 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3871 break;
3873 case SHT_STRTAB:
3874 /* We assume that a section named .stab*str is a stabs
3875 string section. We look for a section with the same name
3876 but without the trailing ``str'', and set its sh_link
3877 field to point to this section. */
3878 if (CONST_STRNEQ (sec->name, ".stab")
3879 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3881 size_t len;
3882 char *alc;
3884 len = strlen (sec->name);
3885 alc = (char *) bfd_malloc (len - 2);
3886 if (alc == NULL)
3887 return FALSE;
3888 memcpy (alc, sec->name, len - 3);
3889 alc[len - 3] = '\0';
3890 s = bfd_get_section_by_name (abfd, alc);
3891 free (alc);
3892 if (s != NULL)
3894 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3896 /* This is a .stab section. */
3897 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3898 elf_section_data (s)->this_hdr.sh_entsize
3899 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3902 break;
3904 case SHT_DYNAMIC:
3905 case SHT_DYNSYM:
3906 case SHT_GNU_verneed:
3907 case SHT_GNU_verdef:
3908 /* sh_link is the section header index of the string table
3909 used for the dynamic entries, or the symbol table, or the
3910 version strings. */
3911 s = bfd_get_section_by_name (abfd, ".dynstr");
3912 if (s != NULL)
3913 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3914 break;
3916 case SHT_GNU_LIBLIST:
3917 /* sh_link is the section header index of the prelink library
3918 list used for the dynamic entries, or the symbol table, or
3919 the version strings. */
3920 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3921 ? ".dynstr" : ".gnu.libstr");
3922 if (s != NULL)
3923 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3924 break;
3926 case SHT_HASH:
3927 case SHT_GNU_HASH:
3928 case SHT_GNU_versym:
3929 /* sh_link is the section header index of the symbol table
3930 this hash table or version table is for. */
3931 s = bfd_get_section_by_name (abfd, ".dynsym");
3932 if (s != NULL)
3933 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3934 break;
3936 case SHT_GROUP:
3937 d->this_hdr.sh_link = elf_onesymtab (abfd);
3941 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3942 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3943 debug section name from .debug_* to .zdebug_* if needed. */
3945 return TRUE;
3948 static bfd_boolean
3949 sym_is_global (bfd *abfd, asymbol *sym)
3951 /* If the backend has a special mapping, use it. */
3952 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3953 if (bed->elf_backend_sym_is_global)
3954 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3956 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3957 || bfd_is_und_section (bfd_get_section (sym))
3958 || bfd_is_com_section (bfd_get_section (sym)));
3961 /* Filter global symbols of ABFD to include in the import library. All
3962 SYMCOUNT symbols of ABFD can be examined from their pointers in
3963 SYMS. Pointers of symbols to keep should be stored contiguously at
3964 the beginning of that array.
3966 Returns the number of symbols to keep. */
3968 unsigned int
3969 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
3970 asymbol **syms, long symcount)
3972 long src_count, dst_count = 0;
3974 for (src_count = 0; src_count < symcount; src_count++)
3976 asymbol *sym = syms[src_count];
3977 char *name = (char *) bfd_asymbol_name (sym);
3978 struct bfd_link_hash_entry *h;
3980 if (!sym_is_global (abfd, sym))
3981 continue;
3983 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
3984 if (h == NULL)
3985 continue;
3986 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
3987 continue;
3988 if (h->linker_def || h->ldscript_def)
3989 continue;
3991 syms[dst_count++] = sym;
3994 syms[dst_count] = NULL;
3996 return dst_count;
3999 /* Don't output section symbols for sections that are not going to be
4000 output, that are duplicates or there is no BFD section. */
4002 static bfd_boolean
4003 ignore_section_sym (bfd *abfd, asymbol *sym)
4005 elf_symbol_type *type_ptr;
4007 if ((sym->flags & BSF_SECTION_SYM) == 0)
4008 return FALSE;
4010 type_ptr = elf_symbol_from (abfd, sym);
4011 return ((type_ptr != NULL
4012 && type_ptr->internal_elf_sym.st_shndx != 0
4013 && bfd_is_abs_section (sym->section))
4014 || !(sym->section->owner == abfd
4015 || (sym->section->output_section->owner == abfd
4016 && sym->section->output_offset == 0)
4017 || bfd_is_abs_section (sym->section)));
4020 /* Map symbol from it's internal number to the external number, moving
4021 all local symbols to be at the head of the list. */
4023 static bfd_boolean
4024 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4026 unsigned int symcount = bfd_get_symcount (abfd);
4027 asymbol **syms = bfd_get_outsymbols (abfd);
4028 asymbol **sect_syms;
4029 unsigned int num_locals = 0;
4030 unsigned int num_globals = 0;
4031 unsigned int num_locals2 = 0;
4032 unsigned int num_globals2 = 0;
4033 unsigned int max_index = 0;
4034 unsigned int idx;
4035 asection *asect;
4036 asymbol **new_syms;
4038 #ifdef DEBUG
4039 fprintf (stderr, "elf_map_symbols\n");
4040 fflush (stderr);
4041 #endif
4043 for (asect = abfd->sections; asect; asect = asect->next)
4045 if (max_index < asect->index)
4046 max_index = asect->index;
4049 max_index++;
4050 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
4051 if (sect_syms == NULL)
4052 return FALSE;
4053 elf_section_syms (abfd) = sect_syms;
4054 elf_num_section_syms (abfd) = max_index;
4056 /* Init sect_syms entries for any section symbols we have already
4057 decided to output. */
4058 for (idx = 0; idx < symcount; idx++)
4060 asymbol *sym = syms[idx];
4062 if ((sym->flags & BSF_SECTION_SYM) != 0
4063 && sym->value == 0
4064 && !ignore_section_sym (abfd, sym)
4065 && !bfd_is_abs_section (sym->section))
4067 asection *sec = sym->section;
4069 if (sec->owner != abfd)
4070 sec = sec->output_section;
4072 sect_syms[sec->index] = syms[idx];
4076 /* Classify all of the symbols. */
4077 for (idx = 0; idx < symcount; idx++)
4079 if (sym_is_global (abfd, syms[idx]))
4080 num_globals++;
4081 else if (!ignore_section_sym (abfd, syms[idx]))
4082 num_locals++;
4085 /* We will be adding a section symbol for each normal BFD section. Most
4086 sections will already have a section symbol in outsymbols, but
4087 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4088 at least in that case. */
4089 for (asect = abfd->sections; asect; asect = asect->next)
4091 if (sect_syms[asect->index] == NULL)
4093 if (!sym_is_global (abfd, asect->symbol))
4094 num_locals++;
4095 else
4096 num_globals++;
4100 /* Now sort the symbols so the local symbols are first. */
4101 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
4102 sizeof (asymbol *));
4104 if (new_syms == NULL)
4105 return FALSE;
4107 for (idx = 0; idx < symcount; idx++)
4109 asymbol *sym = syms[idx];
4110 unsigned int i;
4112 if (sym_is_global (abfd, sym))
4113 i = num_locals + num_globals2++;
4114 else if (!ignore_section_sym (abfd, sym))
4115 i = num_locals2++;
4116 else
4117 continue;
4118 new_syms[i] = sym;
4119 sym->udata.i = i + 1;
4121 for (asect = abfd->sections; asect; asect = asect->next)
4123 if (sect_syms[asect->index] == NULL)
4125 asymbol *sym = asect->symbol;
4126 unsigned int i;
4128 sect_syms[asect->index] = sym;
4129 if (!sym_is_global (abfd, sym))
4130 i = num_locals2++;
4131 else
4132 i = num_locals + num_globals2++;
4133 new_syms[i] = sym;
4134 sym->udata.i = i + 1;
4138 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4140 *pnum_locals = num_locals;
4141 return TRUE;
4144 /* Align to the maximum file alignment that could be required for any
4145 ELF data structure. */
4147 static inline file_ptr
4148 align_file_position (file_ptr off, int align)
4150 return (off + align - 1) & ~(align - 1);
4153 /* Assign a file position to a section, optionally aligning to the
4154 required section alignment. */
4156 file_ptr
4157 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4158 file_ptr offset,
4159 bfd_boolean align)
4161 if (align && i_shdrp->sh_addralign > 1)
4162 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
4163 i_shdrp->sh_offset = offset;
4164 if (i_shdrp->bfd_section != NULL)
4165 i_shdrp->bfd_section->filepos = offset;
4166 if (i_shdrp->sh_type != SHT_NOBITS)
4167 offset += i_shdrp->sh_size;
4168 return offset;
4171 /* Compute the file positions we are going to put the sections at, and
4172 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4173 is not NULL, this is being called by the ELF backend linker. */
4175 bfd_boolean
4176 _bfd_elf_compute_section_file_positions (bfd *abfd,
4177 struct bfd_link_info *link_info)
4179 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4180 struct fake_section_arg fsargs;
4181 bfd_boolean failed;
4182 struct elf_strtab_hash *strtab = NULL;
4183 Elf_Internal_Shdr *shstrtab_hdr;
4184 bfd_boolean need_symtab;
4186 if (abfd->output_has_begun)
4187 return TRUE;
4189 /* Do any elf backend specific processing first. */
4190 if (bed->elf_backend_begin_write_processing)
4191 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4193 if (! prep_headers (abfd))
4194 return FALSE;
4196 /* Post process the headers if necessary. */
4197 (*bed->elf_backend_post_process_headers) (abfd, link_info);
4199 fsargs.failed = FALSE;
4200 fsargs.link_info = link_info;
4201 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4202 if (fsargs.failed)
4203 return FALSE;
4205 if (!assign_section_numbers (abfd, link_info))
4206 return FALSE;
4208 /* The backend linker builds symbol table information itself. */
4209 need_symtab = (link_info == NULL
4210 && (bfd_get_symcount (abfd) > 0
4211 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4212 == HAS_RELOC)));
4213 if (need_symtab)
4215 /* Non-zero if doing a relocatable link. */
4216 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4218 if (! swap_out_syms (abfd, &strtab, relocatable_p))
4219 return FALSE;
4222 failed = FALSE;
4223 if (link_info == NULL)
4225 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4226 if (failed)
4227 return FALSE;
4230 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4231 /* sh_name was set in prep_headers. */
4232 shstrtab_hdr->sh_type = SHT_STRTAB;
4233 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4234 shstrtab_hdr->sh_addr = 0;
4235 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4236 shstrtab_hdr->sh_entsize = 0;
4237 shstrtab_hdr->sh_link = 0;
4238 shstrtab_hdr->sh_info = 0;
4239 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4240 shstrtab_hdr->sh_addralign = 1;
4242 if (!assign_file_positions_except_relocs (abfd, link_info))
4243 return FALSE;
4245 if (need_symtab)
4247 file_ptr off;
4248 Elf_Internal_Shdr *hdr;
4250 off = elf_next_file_pos (abfd);
4252 hdr = & elf_symtab_hdr (abfd);
4253 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4255 if (elf_symtab_shndx_list (abfd) != NULL)
4257 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4258 if (hdr->sh_size != 0)
4259 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4260 /* FIXME: What about other symtab_shndx sections in the list ? */
4263 hdr = &elf_tdata (abfd)->strtab_hdr;
4264 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4266 elf_next_file_pos (abfd) = off;
4268 /* Now that we know where the .strtab section goes, write it
4269 out. */
4270 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4271 || ! _bfd_elf_strtab_emit (abfd, strtab))
4272 return FALSE;
4273 _bfd_elf_strtab_free (strtab);
4276 abfd->output_has_begun = TRUE;
4278 return TRUE;
4281 /* Make an initial estimate of the size of the program header. If we
4282 get the number wrong here, we'll redo section placement. */
4284 static bfd_size_type
4285 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4287 size_t segs;
4288 asection *s;
4289 const struct elf_backend_data *bed;
4291 /* Assume we will need exactly two PT_LOAD segments: one for text
4292 and one for data. */
4293 segs = 2;
4295 s = bfd_get_section_by_name (abfd, ".interp");
4296 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4298 /* If we have a loadable interpreter section, we need a
4299 PT_INTERP segment. In this case, assume we also need a
4300 PT_PHDR segment, although that may not be true for all
4301 targets. */
4302 segs += 2;
4305 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4307 /* We need a PT_DYNAMIC segment. */
4308 ++segs;
4311 if (info != NULL && info->relro)
4313 /* We need a PT_GNU_RELRO segment. */
4314 ++segs;
4317 if (elf_eh_frame_hdr (abfd))
4319 /* We need a PT_GNU_EH_FRAME segment. */
4320 ++segs;
4323 if (elf_stack_flags (abfd))
4325 /* We need a PT_GNU_STACK segment. */
4326 ++segs;
4329 for (s = abfd->sections; s != NULL; s = s->next)
4331 if ((s->flags & SEC_LOAD) != 0
4332 && CONST_STRNEQ (s->name, ".note"))
4334 /* We need a PT_NOTE segment. */
4335 ++segs;
4336 /* Try to create just one PT_NOTE segment
4337 for all adjacent loadable .note* sections.
4338 gABI requires that within a PT_NOTE segment
4339 (and also inside of each SHT_NOTE section)
4340 each note is padded to a multiple of 4 size,
4341 so we check whether the sections are correctly
4342 aligned. */
4343 if (s->alignment_power == 2)
4344 while (s->next != NULL
4345 && s->next->alignment_power == 2
4346 && (s->next->flags & SEC_LOAD) != 0
4347 && CONST_STRNEQ (s->next->name, ".note"))
4348 s = s->next;
4352 for (s = abfd->sections; s != NULL; s = s->next)
4354 if (s->flags & SEC_THREAD_LOCAL)
4356 /* We need a PT_TLS segment. */
4357 ++segs;
4358 break;
4362 bed = get_elf_backend_data (abfd);
4364 if ((abfd->flags & D_PAGED) != 0)
4366 /* Add a PT_GNU_MBIND segment for each mbind section. */
4367 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4368 for (s = abfd->sections; s != NULL; s = s->next)
4369 if (elf_section_flags (s) & SHF_GNU_MBIND)
4371 if (elf_section_data (s)->this_hdr.sh_info
4372 > PT_GNU_MBIND_NUM)
4374 _bfd_error_handler
4375 /* xgettext:c-format */
4376 (_("%B: GNU_MBIN section `%A' has invalid sh_info field: %d"),
4377 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4378 continue;
4380 /* Align mbind section to page size. */
4381 if (s->alignment_power < page_align_power)
4382 s->alignment_power = page_align_power;
4383 segs ++;
4387 /* Let the backend count up any program headers it might need. */
4388 if (bed->elf_backend_additional_program_headers)
4390 int a;
4392 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4393 if (a == -1)
4394 abort ();
4395 segs += a;
4398 return segs * bed->s->sizeof_phdr;
4401 /* Find the segment that contains the output_section of section. */
4403 Elf_Internal_Phdr *
4404 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4406 struct elf_segment_map *m;
4407 Elf_Internal_Phdr *p;
4409 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4410 m != NULL;
4411 m = m->next, p++)
4413 int i;
4415 for (i = m->count - 1; i >= 0; i--)
4416 if (m->sections[i] == section)
4417 return p;
4420 return NULL;
4423 /* Create a mapping from a set of sections to a program segment. */
4425 static struct elf_segment_map *
4426 make_mapping (bfd *abfd,
4427 asection **sections,
4428 unsigned int from,
4429 unsigned int to,
4430 bfd_boolean phdr)
4432 struct elf_segment_map *m;
4433 unsigned int i;
4434 asection **hdrpp;
4435 bfd_size_type amt;
4437 amt = sizeof (struct elf_segment_map);
4438 amt += (to - from - 1) * sizeof (asection *);
4439 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4440 if (m == NULL)
4441 return NULL;
4442 m->next = NULL;
4443 m->p_type = PT_LOAD;
4444 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4445 m->sections[i - from] = *hdrpp;
4446 m->count = to - from;
4448 if (from == 0 && phdr)
4450 /* Include the headers in the first PT_LOAD segment. */
4451 m->includes_filehdr = 1;
4452 m->includes_phdrs = 1;
4455 return m;
4458 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4459 on failure. */
4461 struct elf_segment_map *
4462 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4464 struct elf_segment_map *m;
4466 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4467 sizeof (struct elf_segment_map));
4468 if (m == NULL)
4469 return NULL;
4470 m->next = NULL;
4471 m->p_type = PT_DYNAMIC;
4472 m->count = 1;
4473 m->sections[0] = dynsec;
4475 return m;
4478 /* Possibly add or remove segments from the segment map. */
4480 static bfd_boolean
4481 elf_modify_segment_map (bfd *abfd,
4482 struct bfd_link_info *info,
4483 bfd_boolean remove_empty_load)
4485 struct elf_segment_map **m;
4486 const struct elf_backend_data *bed;
4488 /* The placement algorithm assumes that non allocated sections are
4489 not in PT_LOAD segments. We ensure this here by removing such
4490 sections from the segment map. We also remove excluded
4491 sections. Finally, any PT_LOAD segment without sections is
4492 removed. */
4493 m = &elf_seg_map (abfd);
4494 while (*m)
4496 unsigned int i, new_count;
4498 for (new_count = 0, i = 0; i < (*m)->count; i++)
4500 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4501 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4502 || (*m)->p_type != PT_LOAD))
4504 (*m)->sections[new_count] = (*m)->sections[i];
4505 new_count++;
4508 (*m)->count = new_count;
4510 if (remove_empty_load
4511 && (*m)->p_type == PT_LOAD
4512 && (*m)->count == 0
4513 && !(*m)->includes_phdrs)
4514 *m = (*m)->next;
4515 else
4516 m = &(*m)->next;
4519 bed = get_elf_backend_data (abfd);
4520 if (bed->elf_backend_modify_segment_map != NULL)
4522 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4523 return FALSE;
4526 return TRUE;
4529 /* Set up a mapping from BFD sections to program segments. */
4531 bfd_boolean
4532 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4534 unsigned int count;
4535 struct elf_segment_map *m;
4536 asection **sections = NULL;
4537 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4538 bfd_boolean no_user_phdrs;
4540 no_user_phdrs = elf_seg_map (abfd) == NULL;
4542 if (info != NULL)
4543 info->user_phdrs = !no_user_phdrs;
4545 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4547 asection *s;
4548 unsigned int i;
4549 struct elf_segment_map *mfirst;
4550 struct elf_segment_map **pm;
4551 asection *last_hdr;
4552 bfd_vma last_size;
4553 unsigned int phdr_index;
4554 bfd_vma maxpagesize;
4555 asection **hdrpp;
4556 bfd_boolean phdr_in_segment = TRUE;
4557 bfd_boolean writable;
4558 int tls_count = 0;
4559 asection *first_tls = NULL;
4560 asection *first_mbind = NULL;
4561 asection *dynsec, *eh_frame_hdr;
4562 bfd_size_type amt;
4563 bfd_vma addr_mask, wrap_to = 0;
4564 bfd_boolean linker_created_pt_phdr_segment = FALSE;
4566 /* Select the allocated sections, and sort them. */
4568 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
4569 sizeof (asection *));
4570 if (sections == NULL)
4571 goto error_return;
4573 /* Calculate top address, avoiding undefined behaviour of shift
4574 left operator when shift count is equal to size of type
4575 being shifted. */
4576 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4577 addr_mask = (addr_mask << 1) + 1;
4579 i = 0;
4580 for (s = abfd->sections; s != NULL; s = s->next)
4582 if ((s->flags & SEC_ALLOC) != 0)
4584 sections[i] = s;
4585 ++i;
4586 /* A wrapping section potentially clashes with header. */
4587 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4588 wrap_to = (s->lma + s->size) & addr_mask;
4591 BFD_ASSERT (i <= bfd_count_sections (abfd));
4592 count = i;
4594 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4596 /* Build the mapping. */
4598 mfirst = NULL;
4599 pm = &mfirst;
4601 /* If we have a .interp section, then create a PT_PHDR segment for
4602 the program headers and a PT_INTERP segment for the .interp
4603 section. */
4604 s = bfd_get_section_by_name (abfd, ".interp");
4605 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4607 amt = sizeof (struct elf_segment_map);
4608 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4609 if (m == NULL)
4610 goto error_return;
4611 m->next = NULL;
4612 m->p_type = PT_PHDR;
4613 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4614 m->p_flags = PF_R | PF_X;
4615 m->p_flags_valid = 1;
4616 m->includes_phdrs = 1;
4617 linker_created_pt_phdr_segment = TRUE;
4618 *pm = m;
4619 pm = &m->next;
4621 amt = sizeof (struct elf_segment_map);
4622 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4623 if (m == NULL)
4624 goto error_return;
4625 m->next = NULL;
4626 m->p_type = PT_INTERP;
4627 m->count = 1;
4628 m->sections[0] = s;
4630 *pm = m;
4631 pm = &m->next;
4634 /* Look through the sections. We put sections in the same program
4635 segment when the start of the second section can be placed within
4636 a few bytes of the end of the first section. */
4637 last_hdr = NULL;
4638 last_size = 0;
4639 phdr_index = 0;
4640 maxpagesize = bed->maxpagesize;
4641 /* PR 17512: file: c8455299.
4642 Avoid divide-by-zero errors later on.
4643 FIXME: Should we abort if the maxpagesize is zero ? */
4644 if (maxpagesize == 0)
4645 maxpagesize = 1;
4646 writable = FALSE;
4647 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4648 if (dynsec != NULL
4649 && (dynsec->flags & SEC_LOAD) == 0)
4650 dynsec = NULL;
4652 /* Deal with -Ttext or something similar such that the first section
4653 is not adjacent to the program headers. This is an
4654 approximation, since at this point we don't know exactly how many
4655 program headers we will need. */
4656 if (count > 0)
4658 bfd_size_type phdr_size = elf_program_header_size (abfd);
4660 if (phdr_size == (bfd_size_type) -1)
4661 phdr_size = get_program_header_size (abfd, info);
4662 phdr_size += bed->s->sizeof_ehdr;
4663 if ((abfd->flags & D_PAGED) == 0
4664 || (sections[0]->lma & addr_mask) < phdr_size
4665 || ((sections[0]->lma & addr_mask) % maxpagesize
4666 < phdr_size % maxpagesize)
4667 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
4669 /* PR 20815: The ELF standard says that a PT_PHDR segment, if
4670 present, must be included as part of the memory image of the
4671 program. Ie it must be part of a PT_LOAD segment as well.
4672 If we have had to create our own PT_PHDR segment, but it is
4673 not going to be covered by the first PT_LOAD segment, then
4674 force the inclusion if we can... */
4675 if ((abfd->flags & D_PAGED) != 0
4676 && linker_created_pt_phdr_segment)
4677 phdr_in_segment = TRUE;
4678 else
4679 phdr_in_segment = FALSE;
4683 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4685 asection *hdr;
4686 bfd_boolean new_segment;
4688 hdr = *hdrpp;
4690 /* See if this section and the last one will fit in the same
4691 segment. */
4693 if (last_hdr == NULL)
4695 /* If we don't have a segment yet, then we don't need a new
4696 one (we build the last one after this loop). */
4697 new_segment = FALSE;
4699 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4701 /* If this section has a different relation between the
4702 virtual address and the load address, then we need a new
4703 segment. */
4704 new_segment = TRUE;
4706 else if (hdr->lma < last_hdr->lma + last_size
4707 || last_hdr->lma + last_size < last_hdr->lma)
4709 /* If this section has a load address that makes it overlap
4710 the previous section, then we need a new segment. */
4711 new_segment = TRUE;
4713 /* In the next test we have to be careful when last_hdr->lma is close
4714 to the end of the address space. If the aligned address wraps
4715 around to the start of the address space, then there are no more
4716 pages left in memory and it is OK to assume that the current
4717 section can be included in the current segment. */
4718 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4719 > last_hdr->lma)
4720 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4721 <= hdr->lma))
4723 /* If putting this section in this segment would force us to
4724 skip a page in the segment, then we need a new segment. */
4725 new_segment = TRUE;
4727 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4728 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0
4729 && ((abfd->flags & D_PAGED) == 0
4730 || (((last_hdr->lma + last_size - 1) & -maxpagesize)
4731 != (hdr->lma & -maxpagesize))))
4733 /* We don't want to put a loaded section after a
4734 nonloaded (ie. bss style) section in the same segment
4735 as that will force the non-loaded section to be loaded.
4736 Consider .tbss sections as loaded for this purpose.
4737 However, like the writable/non-writable case below,
4738 if they are on the same page then they must be put
4739 in the same segment. */
4740 new_segment = TRUE;
4742 else if ((abfd->flags & D_PAGED) == 0)
4744 /* If the file is not demand paged, which means that we
4745 don't require the sections to be correctly aligned in the
4746 file, then there is no other reason for a new segment. */
4747 new_segment = FALSE;
4749 else if (! writable
4750 && (hdr->flags & SEC_READONLY) == 0
4751 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4752 != (hdr->lma & -maxpagesize)))
4754 /* We don't want to put a writable section in a read only
4755 segment, unless they are on the same page in memory
4756 anyhow. We already know that the last section does not
4757 bring us past the current section on the page, so the
4758 only case in which the new section is not on the same
4759 page as the previous section is when the previous section
4760 ends precisely on a page boundary. */
4761 new_segment = TRUE;
4763 else
4765 /* Otherwise, we can use the same segment. */
4766 new_segment = FALSE;
4769 /* Allow interested parties a chance to override our decision. */
4770 if (last_hdr != NULL
4771 && info != NULL
4772 && info->callbacks->override_segment_assignment != NULL)
4773 new_segment
4774 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4775 last_hdr,
4776 new_segment);
4778 if (! new_segment)
4780 if ((hdr->flags & SEC_READONLY) == 0)
4781 writable = TRUE;
4782 last_hdr = hdr;
4783 /* .tbss sections effectively have zero size. */
4784 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4785 != SEC_THREAD_LOCAL)
4786 last_size = hdr->size;
4787 else
4788 last_size = 0;
4789 continue;
4792 /* We need a new program segment. We must create a new program
4793 header holding all the sections from phdr_index until hdr. */
4795 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4796 if (m == NULL)
4797 goto error_return;
4799 *pm = m;
4800 pm = &m->next;
4802 if ((hdr->flags & SEC_READONLY) == 0)
4803 writable = TRUE;
4804 else
4805 writable = FALSE;
4807 last_hdr = hdr;
4808 /* .tbss sections effectively have zero size. */
4809 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4810 last_size = hdr->size;
4811 else
4812 last_size = 0;
4813 phdr_index = i;
4814 phdr_in_segment = FALSE;
4817 /* Create a final PT_LOAD program segment, but not if it's just
4818 for .tbss. */
4819 if (last_hdr != NULL
4820 && (i - phdr_index != 1
4821 || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4822 != SEC_THREAD_LOCAL)))
4824 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4825 if (m == NULL)
4826 goto error_return;
4828 *pm = m;
4829 pm = &m->next;
4832 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4833 if (dynsec != NULL)
4835 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4836 if (m == NULL)
4837 goto error_return;
4838 *pm = m;
4839 pm = &m->next;
4842 /* For each batch of consecutive loadable .note sections,
4843 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4844 because if we link together nonloadable .note sections and
4845 loadable .note sections, we will generate two .note sections
4846 in the output file. FIXME: Using names for section types is
4847 bogus anyhow. */
4848 for (s = abfd->sections; s != NULL; s = s->next)
4850 if ((s->flags & SEC_LOAD) != 0
4851 && CONST_STRNEQ (s->name, ".note"))
4853 asection *s2;
4855 count = 1;
4856 amt = sizeof (struct elf_segment_map);
4857 if (s->alignment_power == 2)
4858 for (s2 = s; s2->next != NULL; s2 = s2->next)
4860 if (s2->next->alignment_power == 2
4861 && (s2->next->flags & SEC_LOAD) != 0
4862 && CONST_STRNEQ (s2->next->name, ".note")
4863 && align_power (s2->lma + s2->size, 2)
4864 == s2->next->lma)
4865 count++;
4866 else
4867 break;
4869 amt += (count - 1) * sizeof (asection *);
4870 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4871 if (m == NULL)
4872 goto error_return;
4873 m->next = NULL;
4874 m->p_type = PT_NOTE;
4875 m->count = count;
4876 while (count > 1)
4878 m->sections[m->count - count--] = s;
4879 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4880 s = s->next;
4882 m->sections[m->count - 1] = s;
4883 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4884 *pm = m;
4885 pm = &m->next;
4887 if (s->flags & SEC_THREAD_LOCAL)
4889 if (! tls_count)
4890 first_tls = s;
4891 tls_count++;
4893 if (first_mbind == NULL
4894 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
4895 first_mbind = s;
4898 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4899 if (tls_count > 0)
4901 amt = sizeof (struct elf_segment_map);
4902 amt += (tls_count - 1) * sizeof (asection *);
4903 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4904 if (m == NULL)
4905 goto error_return;
4906 m->next = NULL;
4907 m->p_type = PT_TLS;
4908 m->count = tls_count;
4909 /* Mandated PF_R. */
4910 m->p_flags = PF_R;
4911 m->p_flags_valid = 1;
4912 s = first_tls;
4913 for (i = 0; i < (unsigned int) tls_count; ++i)
4915 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4917 _bfd_error_handler
4918 (_("%B: TLS sections are not adjacent:"), abfd);
4919 s = first_tls;
4920 i = 0;
4921 while (i < (unsigned int) tls_count)
4923 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4925 _bfd_error_handler (_(" TLS: %A"), s);
4926 i++;
4928 else
4929 _bfd_error_handler (_(" non-TLS: %A"), s);
4930 s = s->next;
4932 bfd_set_error (bfd_error_bad_value);
4933 goto error_return;
4935 m->sections[i] = s;
4936 s = s->next;
4939 *pm = m;
4940 pm = &m->next;
4943 if (first_mbind && (abfd->flags & D_PAGED) != 0)
4944 for (s = first_mbind; s != NULL; s = s->next)
4945 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
4946 && (elf_section_data (s)->this_hdr.sh_info
4947 <= PT_GNU_MBIND_NUM))
4949 /* Mandated PF_R. */
4950 unsigned long p_flags = PF_R;
4951 if ((s->flags & SEC_READONLY) == 0)
4952 p_flags |= PF_W;
4953 if ((s->flags & SEC_CODE) != 0)
4954 p_flags |= PF_X;
4956 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
4957 m = bfd_zalloc (abfd, amt);
4958 if (m == NULL)
4959 goto error_return;
4960 m->next = NULL;
4961 m->p_type = (PT_GNU_MBIND_LO
4962 + elf_section_data (s)->this_hdr.sh_info);
4963 m->count = 1;
4964 m->p_flags_valid = 1;
4965 m->sections[0] = s;
4966 m->p_flags = p_flags;
4968 *pm = m;
4969 pm = &m->next;
4972 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4973 segment. */
4974 eh_frame_hdr = elf_eh_frame_hdr (abfd);
4975 if (eh_frame_hdr != NULL
4976 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4978 amt = sizeof (struct elf_segment_map);
4979 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4980 if (m == NULL)
4981 goto error_return;
4982 m->next = NULL;
4983 m->p_type = PT_GNU_EH_FRAME;
4984 m->count = 1;
4985 m->sections[0] = eh_frame_hdr->output_section;
4987 *pm = m;
4988 pm = &m->next;
4991 if (elf_stack_flags (abfd))
4993 amt = sizeof (struct elf_segment_map);
4994 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4995 if (m == NULL)
4996 goto error_return;
4997 m->next = NULL;
4998 m->p_type = PT_GNU_STACK;
4999 m->p_flags = elf_stack_flags (abfd);
5000 m->p_align = bed->stack_align;
5001 m->p_flags_valid = 1;
5002 m->p_align_valid = m->p_align != 0;
5003 if (info->stacksize > 0)
5005 m->p_size = info->stacksize;
5006 m->p_size_valid = 1;
5009 *pm = m;
5010 pm = &m->next;
5013 if (info != NULL && info->relro)
5015 for (m = mfirst; m != NULL; m = m->next)
5017 if (m->p_type == PT_LOAD
5018 && m->count != 0
5019 && m->sections[0]->vma >= info->relro_start
5020 && m->sections[0]->vma < info->relro_end)
5022 i = m->count;
5023 while (--i != (unsigned) -1)
5024 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5025 == (SEC_LOAD | SEC_HAS_CONTENTS))
5026 break;
5028 if (i != (unsigned) -1)
5029 break;
5033 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5034 if (m != NULL)
5036 amt = sizeof (struct elf_segment_map);
5037 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5038 if (m == NULL)
5039 goto error_return;
5040 m->next = NULL;
5041 m->p_type = PT_GNU_RELRO;
5042 *pm = m;
5043 pm = &m->next;
5047 free (sections);
5048 elf_seg_map (abfd) = mfirst;
5051 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5052 return FALSE;
5054 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5055 ++count;
5056 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5058 return TRUE;
5060 error_return:
5061 if (sections != NULL)
5062 free (sections);
5063 return FALSE;
5066 /* Sort sections by address. */
5068 static int
5069 elf_sort_sections (const void *arg1, const void *arg2)
5071 const asection *sec1 = *(const asection **) arg1;
5072 const asection *sec2 = *(const asection **) arg2;
5073 bfd_size_type size1, size2;
5075 /* Sort by LMA first, since this is the address used to
5076 place the section into a segment. */
5077 if (sec1->lma < sec2->lma)
5078 return -1;
5079 else if (sec1->lma > sec2->lma)
5080 return 1;
5082 /* Then sort by VMA. Normally the LMA and the VMA will be
5083 the same, and this will do nothing. */
5084 if (sec1->vma < sec2->vma)
5085 return -1;
5086 else if (sec1->vma > sec2->vma)
5087 return 1;
5089 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5091 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5093 if (TOEND (sec1))
5095 if (TOEND (sec2))
5097 /* If the indicies are the same, do not return 0
5098 here, but continue to try the next comparison. */
5099 if (sec1->target_index - sec2->target_index != 0)
5100 return sec1->target_index - sec2->target_index;
5102 else
5103 return 1;
5105 else if (TOEND (sec2))
5106 return -1;
5108 #undef TOEND
5110 /* Sort by size, to put zero sized sections
5111 before others at the same address. */
5113 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5114 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5116 if (size1 < size2)
5117 return -1;
5118 if (size1 > size2)
5119 return 1;
5121 return sec1->target_index - sec2->target_index;
5124 /* Ian Lance Taylor writes:
5126 We shouldn't be using % with a negative signed number. That's just
5127 not good. We have to make sure either that the number is not
5128 negative, or that the number has an unsigned type. When the types
5129 are all the same size they wind up as unsigned. When file_ptr is a
5130 larger signed type, the arithmetic winds up as signed long long,
5131 which is wrong.
5133 What we're trying to say here is something like ``increase OFF by
5134 the least amount that will cause it to be equal to the VMA modulo
5135 the page size.'' */
5136 /* In other words, something like:
5138 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5139 off_offset = off % bed->maxpagesize;
5140 if (vma_offset < off_offset)
5141 adjustment = vma_offset + bed->maxpagesize - off_offset;
5142 else
5143 adjustment = vma_offset - off_offset;
5145 which can be collapsed into the expression below. */
5147 static file_ptr
5148 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5150 /* PR binutils/16199: Handle an alignment of zero. */
5151 if (maxpagesize == 0)
5152 maxpagesize = 1;
5153 return ((vma - off) % maxpagesize);
5156 static void
5157 print_segment_map (const struct elf_segment_map *m)
5159 unsigned int j;
5160 const char *pt = get_segment_type (m->p_type);
5161 char buf[32];
5163 if (pt == NULL)
5165 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5166 sprintf (buf, "LOPROC+%7.7x",
5167 (unsigned int) (m->p_type - PT_LOPROC));
5168 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5169 sprintf (buf, "LOOS+%7.7x",
5170 (unsigned int) (m->p_type - PT_LOOS));
5171 else
5172 snprintf (buf, sizeof (buf), "%8.8x",
5173 (unsigned int) m->p_type);
5174 pt = buf;
5176 fflush (stdout);
5177 fprintf (stderr, "%s:", pt);
5178 for (j = 0; j < m->count; j++)
5179 fprintf (stderr, " %s", m->sections [j]->name);
5180 putc ('\n',stderr);
5181 fflush (stderr);
5184 static bfd_boolean
5185 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5187 void *buf;
5188 bfd_boolean ret;
5190 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5191 return FALSE;
5192 buf = bfd_zmalloc (len);
5193 if (buf == NULL)
5194 return FALSE;
5195 ret = bfd_bwrite (buf, len, abfd) == len;
5196 free (buf);
5197 return ret;
5200 /* Assign file positions to the sections based on the mapping from
5201 sections to segments. This function also sets up some fields in
5202 the file header. */
5204 static bfd_boolean
5205 assign_file_positions_for_load_sections (bfd *abfd,
5206 struct bfd_link_info *link_info)
5208 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5209 struct elf_segment_map *m;
5210 Elf_Internal_Phdr *phdrs;
5211 Elf_Internal_Phdr *p;
5212 file_ptr off;
5213 bfd_size_type maxpagesize;
5214 unsigned int pt_load_count = 0;
5215 unsigned int alloc;
5216 unsigned int i, j;
5217 bfd_vma header_pad = 0;
5219 if (link_info == NULL
5220 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
5221 return FALSE;
5223 alloc = 0;
5224 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5226 ++alloc;
5227 if (m->header_size)
5228 header_pad = m->header_size;
5231 if (alloc)
5233 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5234 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5236 else
5238 /* PR binutils/12467. */
5239 elf_elfheader (abfd)->e_phoff = 0;
5240 elf_elfheader (abfd)->e_phentsize = 0;
5243 elf_elfheader (abfd)->e_phnum = alloc;
5245 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5246 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5247 else
5248 BFD_ASSERT (elf_program_header_size (abfd)
5249 >= alloc * bed->s->sizeof_phdr);
5251 if (alloc == 0)
5253 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5254 return TRUE;
5257 /* We're writing the size in elf_program_header_size (abfd),
5258 see assign_file_positions_except_relocs, so make sure we have
5259 that amount allocated, with trailing space cleared.
5260 The variable alloc contains the computed need, while
5261 elf_program_header_size (abfd) contains the size used for the
5262 layout.
5263 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5264 where the layout is forced to according to a larger size in the
5265 last iterations for the testcase ld-elf/header. */
5266 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
5267 == 0);
5268 phdrs = (Elf_Internal_Phdr *)
5269 bfd_zalloc2 (abfd,
5270 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5271 sizeof (Elf_Internal_Phdr));
5272 elf_tdata (abfd)->phdr = phdrs;
5273 if (phdrs == NULL)
5274 return FALSE;
5276 maxpagesize = 1;
5277 if ((abfd->flags & D_PAGED) != 0)
5278 maxpagesize = bed->maxpagesize;
5280 off = bed->s->sizeof_ehdr;
5281 off += alloc * bed->s->sizeof_phdr;
5282 if (header_pad < (bfd_vma) off)
5283 header_pad = 0;
5284 else
5285 header_pad -= off;
5286 off += header_pad;
5288 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
5289 m != NULL;
5290 m = m->next, p++, j++)
5292 asection **secpp;
5293 bfd_vma off_adjust;
5294 bfd_boolean no_contents;
5296 /* If elf_segment_map is not from map_sections_to_segments, the
5297 sections may not be correctly ordered. NOTE: sorting should
5298 not be done to the PT_NOTE section of a corefile, which may
5299 contain several pseudo-sections artificially created by bfd.
5300 Sorting these pseudo-sections breaks things badly. */
5301 if (m->count > 1
5302 && !(elf_elfheader (abfd)->e_type == ET_CORE
5303 && m->p_type == PT_NOTE))
5304 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5305 elf_sort_sections);
5307 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5308 number of sections with contents contributing to both p_filesz
5309 and p_memsz, followed by a number of sections with no contents
5310 that just contribute to p_memsz. In this loop, OFF tracks next
5311 available file offset for PT_LOAD and PT_NOTE segments. */
5312 p->p_type = m->p_type;
5313 p->p_flags = m->p_flags;
5315 if (m->count == 0)
5316 p->p_vaddr = 0;
5317 else
5318 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
5320 if (m->p_paddr_valid)
5321 p->p_paddr = m->p_paddr;
5322 else if (m->count == 0)
5323 p->p_paddr = 0;
5324 else
5325 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
5327 if (p->p_type == PT_LOAD
5328 && (abfd->flags & D_PAGED) != 0)
5330 /* p_align in demand paged PT_LOAD segments effectively stores
5331 the maximum page size. When copying an executable with
5332 objcopy, we set m->p_align from the input file. Use this
5333 value for maxpagesize rather than bed->maxpagesize, which
5334 may be different. Note that we use maxpagesize for PT_TLS
5335 segment alignment later in this function, so we are relying
5336 on at least one PT_LOAD segment appearing before a PT_TLS
5337 segment. */
5338 if (m->p_align_valid)
5339 maxpagesize = m->p_align;
5341 p->p_align = maxpagesize;
5342 pt_load_count += 1;
5344 else if (m->p_align_valid)
5345 p->p_align = m->p_align;
5346 else if (m->count == 0)
5347 p->p_align = 1 << bed->s->log_file_align;
5348 else
5349 p->p_align = 0;
5351 no_contents = FALSE;
5352 off_adjust = 0;
5353 if (p->p_type == PT_LOAD
5354 && m->count > 0)
5356 bfd_size_type align;
5357 unsigned int align_power = 0;
5359 if (m->p_align_valid)
5360 align = p->p_align;
5361 else
5363 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5365 unsigned int secalign;
5367 secalign = bfd_get_section_alignment (abfd, *secpp);
5368 if (secalign > align_power)
5369 align_power = secalign;
5371 align = (bfd_size_type) 1 << align_power;
5372 if (align < maxpagesize)
5373 align = maxpagesize;
5376 for (i = 0; i < m->count; i++)
5377 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5378 /* If we aren't making room for this section, then
5379 it must be SHT_NOBITS regardless of what we've
5380 set via struct bfd_elf_special_section. */
5381 elf_section_type (m->sections[i]) = SHT_NOBITS;
5383 /* Find out whether this segment contains any loadable
5384 sections. */
5385 no_contents = TRUE;
5386 for (i = 0; i < m->count; i++)
5387 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5389 no_contents = FALSE;
5390 break;
5393 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
5395 /* Broken hardware and/or kernel require that files do not
5396 map the same page with different permissions on some hppa
5397 processors. */
5398 if (pt_load_count > 1
5399 && bed->no_page_alias
5400 && (off & (maxpagesize - 1)) != 0
5401 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5402 off_adjust += maxpagesize;
5403 off += off_adjust;
5404 if (no_contents)
5406 /* We shouldn't need to align the segment on disk since
5407 the segment doesn't need file space, but the gABI
5408 arguably requires the alignment and glibc ld.so
5409 checks it. So to comply with the alignment
5410 requirement but not waste file space, we adjust
5411 p_offset for just this segment. (OFF_ADJUST is
5412 subtracted from OFF later.) This may put p_offset
5413 past the end of file, but that shouldn't matter. */
5415 else
5416 off_adjust = 0;
5418 /* Make sure the .dynamic section is the first section in the
5419 PT_DYNAMIC segment. */
5420 else if (p->p_type == PT_DYNAMIC
5421 && m->count > 1
5422 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5424 _bfd_error_handler
5425 (_("%B: The first section in the PT_DYNAMIC segment"
5426 " is not the .dynamic section"),
5427 abfd);
5428 bfd_set_error (bfd_error_bad_value);
5429 return FALSE;
5431 /* Set the note section type to SHT_NOTE. */
5432 else if (p->p_type == PT_NOTE)
5433 for (i = 0; i < m->count; i++)
5434 elf_section_type (m->sections[i]) = SHT_NOTE;
5436 p->p_offset = 0;
5437 p->p_filesz = 0;
5438 p->p_memsz = 0;
5440 if (m->includes_filehdr)
5442 if (!m->p_flags_valid)
5443 p->p_flags |= PF_R;
5444 p->p_filesz = bed->s->sizeof_ehdr;
5445 p->p_memsz = bed->s->sizeof_ehdr;
5446 if (m->count > 0)
5448 if (p->p_vaddr < (bfd_vma) off
5449 || (!m->p_paddr_valid
5450 && p->p_paddr < (bfd_vma) off))
5452 _bfd_error_handler
5453 (_("%B: Not enough room for program headers,"
5454 " try linking with -N"),
5455 abfd);
5456 bfd_set_error (bfd_error_bad_value);
5457 return FALSE;
5460 p->p_vaddr -= off;
5461 if (!m->p_paddr_valid)
5462 p->p_paddr -= off;
5466 if (m->includes_phdrs)
5468 if (!m->p_flags_valid)
5469 p->p_flags |= PF_R;
5471 if (!m->includes_filehdr)
5473 p->p_offset = bed->s->sizeof_ehdr;
5475 if (m->count > 0)
5477 p->p_vaddr -= off - p->p_offset;
5478 if (!m->p_paddr_valid)
5479 p->p_paddr -= off - p->p_offset;
5483 p->p_filesz += alloc * bed->s->sizeof_phdr;
5484 p->p_memsz += alloc * bed->s->sizeof_phdr;
5485 if (m->count)
5487 p->p_filesz += header_pad;
5488 p->p_memsz += header_pad;
5492 if (p->p_type == PT_LOAD
5493 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5495 if (!m->includes_filehdr && !m->includes_phdrs)
5496 p->p_offset = off;
5497 else
5499 file_ptr adjust;
5501 adjust = off - (p->p_offset + p->p_filesz);
5502 if (!no_contents)
5503 p->p_filesz += adjust;
5504 p->p_memsz += adjust;
5508 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5509 maps. Set filepos for sections in PT_LOAD segments, and in
5510 core files, for sections in PT_NOTE segments.
5511 assign_file_positions_for_non_load_sections will set filepos
5512 for other sections and update p_filesz for other segments. */
5513 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5515 asection *sec;
5516 bfd_size_type align;
5517 Elf_Internal_Shdr *this_hdr;
5519 sec = *secpp;
5520 this_hdr = &elf_section_data (sec)->this_hdr;
5521 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
5523 if ((p->p_type == PT_LOAD
5524 || p->p_type == PT_TLS)
5525 && (this_hdr->sh_type != SHT_NOBITS
5526 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5527 && ((this_hdr->sh_flags & SHF_TLS) == 0
5528 || p->p_type == PT_TLS))))
5530 bfd_vma p_start = p->p_paddr;
5531 bfd_vma p_end = p_start + p->p_memsz;
5532 bfd_vma s_start = sec->lma;
5533 bfd_vma adjust = s_start - p_end;
5535 if (adjust != 0
5536 && (s_start < p_end
5537 || p_end < p_start))
5539 _bfd_error_handler
5540 /* xgettext:c-format */
5541 (_("%B: section %A lma %#Lx adjusted to %#Lx"),
5542 abfd, sec, s_start, p_end);
5543 adjust = 0;
5544 sec->lma = p_end;
5546 p->p_memsz += adjust;
5548 if (this_hdr->sh_type != SHT_NOBITS)
5550 if (p->p_filesz + adjust < p->p_memsz)
5552 /* We have a PROGBITS section following NOBITS ones.
5553 Allocate file space for the NOBITS section(s) and
5554 zero it. */
5555 adjust = p->p_memsz - p->p_filesz;
5556 if (!write_zeros (abfd, off, adjust))
5557 return FALSE;
5559 off += adjust;
5560 p->p_filesz += adjust;
5564 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5566 /* The section at i == 0 is the one that actually contains
5567 everything. */
5568 if (i == 0)
5570 this_hdr->sh_offset = sec->filepos = off;
5571 off += this_hdr->sh_size;
5572 p->p_filesz = this_hdr->sh_size;
5573 p->p_memsz = 0;
5574 p->p_align = 1;
5576 else
5578 /* The rest are fake sections that shouldn't be written. */
5579 sec->filepos = 0;
5580 sec->size = 0;
5581 sec->flags = 0;
5582 continue;
5585 else
5587 if (p->p_type == PT_LOAD)
5589 this_hdr->sh_offset = sec->filepos = off;
5590 if (this_hdr->sh_type != SHT_NOBITS)
5591 off += this_hdr->sh_size;
5593 else if (this_hdr->sh_type == SHT_NOBITS
5594 && (this_hdr->sh_flags & SHF_TLS) != 0
5595 && this_hdr->sh_offset == 0)
5597 /* This is a .tbss section that didn't get a PT_LOAD.
5598 (See _bfd_elf_map_sections_to_segments "Create a
5599 final PT_LOAD".) Set sh_offset to the value it
5600 would have if we had created a zero p_filesz and
5601 p_memsz PT_LOAD header for the section. This
5602 also makes the PT_TLS header have the same
5603 p_offset value. */
5604 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5605 off, align);
5606 this_hdr->sh_offset = sec->filepos = off + adjust;
5609 if (this_hdr->sh_type != SHT_NOBITS)
5611 p->p_filesz += this_hdr->sh_size;
5612 /* A load section without SHF_ALLOC is something like
5613 a note section in a PT_NOTE segment. These take
5614 file space but are not loaded into memory. */
5615 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5616 p->p_memsz += this_hdr->sh_size;
5618 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5620 if (p->p_type == PT_TLS)
5621 p->p_memsz += this_hdr->sh_size;
5623 /* .tbss is special. It doesn't contribute to p_memsz of
5624 normal segments. */
5625 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5626 p->p_memsz += this_hdr->sh_size;
5629 if (align > p->p_align
5630 && !m->p_align_valid
5631 && (p->p_type != PT_LOAD
5632 || (abfd->flags & D_PAGED) == 0))
5633 p->p_align = align;
5636 if (!m->p_flags_valid)
5638 p->p_flags |= PF_R;
5639 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5640 p->p_flags |= PF_X;
5641 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5642 p->p_flags |= PF_W;
5646 off -= off_adjust;
5648 /* Check that all sections are in a PT_LOAD segment.
5649 Don't check funky gdb generated core files. */
5650 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5652 bfd_boolean check_vma = TRUE;
5654 for (i = 1; i < m->count; i++)
5655 if (m->sections[i]->vma == m->sections[i - 1]->vma
5656 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5657 ->this_hdr), p) != 0
5658 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5659 ->this_hdr), p) != 0)
5661 /* Looks like we have overlays packed into the segment. */
5662 check_vma = FALSE;
5663 break;
5666 for (i = 0; i < m->count; i++)
5668 Elf_Internal_Shdr *this_hdr;
5669 asection *sec;
5671 sec = m->sections[i];
5672 this_hdr = &(elf_section_data(sec)->this_hdr);
5673 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5674 && !ELF_TBSS_SPECIAL (this_hdr, p))
5676 _bfd_error_handler
5677 /* xgettext:c-format */
5678 (_("%B: section `%A' can't be allocated in segment %d"),
5679 abfd, sec, j);
5680 print_segment_map (m);
5686 elf_next_file_pos (abfd) = off;
5687 return TRUE;
5690 /* Assign file positions for the other sections. */
5692 static bfd_boolean
5693 assign_file_positions_for_non_load_sections (bfd *abfd,
5694 struct bfd_link_info *link_info)
5696 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5697 Elf_Internal_Shdr **i_shdrpp;
5698 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
5699 Elf_Internal_Phdr *phdrs;
5700 Elf_Internal_Phdr *p;
5701 struct elf_segment_map *m;
5702 struct elf_segment_map *hdrs_segment;
5703 bfd_vma filehdr_vaddr, filehdr_paddr;
5704 bfd_vma phdrs_vaddr, phdrs_paddr;
5705 file_ptr off;
5706 unsigned int count;
5708 i_shdrpp = elf_elfsections (abfd);
5709 end_hdrpp = i_shdrpp + elf_numsections (abfd);
5710 off = elf_next_file_pos (abfd);
5711 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5713 Elf_Internal_Shdr *hdr;
5715 hdr = *hdrpp;
5716 if (hdr->bfd_section != NULL
5717 && (hdr->bfd_section->filepos != 0
5718 || (hdr->sh_type == SHT_NOBITS
5719 && hdr->contents == NULL)))
5720 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5721 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5723 if (hdr->sh_size != 0)
5724 _bfd_error_handler
5725 /* xgettext:c-format */
5726 (_("%B: warning: allocated section `%s' not in segment"),
5727 abfd,
5728 (hdr->bfd_section == NULL
5729 ? "*unknown*"
5730 : hdr->bfd_section->name));
5731 /* We don't need to page align empty sections. */
5732 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5733 off += vma_page_aligned_bias (hdr->sh_addr, off,
5734 bed->maxpagesize);
5735 else
5736 off += vma_page_aligned_bias (hdr->sh_addr, off,
5737 hdr->sh_addralign);
5738 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5739 FALSE);
5741 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5742 && hdr->bfd_section == NULL)
5743 || (hdr->bfd_section != NULL
5744 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5745 /* Compress DWARF debug sections. */
5746 || hdr == i_shdrpp[elf_onesymtab (abfd)]
5747 || (elf_symtab_shndx_list (abfd) != NULL
5748 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
5749 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5750 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5751 hdr->sh_offset = -1;
5752 else
5753 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5756 /* Now that we have set the section file positions, we can set up
5757 the file positions for the non PT_LOAD segments. */
5758 count = 0;
5759 filehdr_vaddr = 0;
5760 filehdr_paddr = 0;
5761 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5762 phdrs_paddr = 0;
5763 hdrs_segment = NULL;
5764 phdrs = elf_tdata (abfd)->phdr;
5765 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5767 ++count;
5768 if (p->p_type != PT_LOAD)
5769 continue;
5771 if (m->includes_filehdr)
5773 filehdr_vaddr = p->p_vaddr;
5774 filehdr_paddr = p->p_paddr;
5776 if (m->includes_phdrs)
5778 phdrs_vaddr = p->p_vaddr;
5779 phdrs_paddr = p->p_paddr;
5780 if (m->includes_filehdr)
5782 hdrs_segment = m;
5783 phdrs_vaddr += bed->s->sizeof_ehdr;
5784 phdrs_paddr += bed->s->sizeof_ehdr;
5789 if (hdrs_segment != NULL && link_info != NULL)
5791 /* There is a segment that contains both the file headers and the
5792 program headers, so provide a symbol __ehdr_start pointing there.
5793 A program can use this to examine itself robustly. */
5795 struct elf_link_hash_entry *hash
5796 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5797 FALSE, FALSE, TRUE);
5798 /* If the symbol was referenced and not defined, define it. */
5799 if (hash != NULL
5800 && (hash->root.type == bfd_link_hash_new
5801 || hash->root.type == bfd_link_hash_undefined
5802 || hash->root.type == bfd_link_hash_undefweak
5803 || hash->root.type == bfd_link_hash_common))
5805 asection *s = NULL;
5806 if (hdrs_segment->count != 0)
5807 /* The segment contains sections, so use the first one. */
5808 s = hdrs_segment->sections[0];
5809 else
5810 /* Use the first (i.e. lowest-addressed) section in any segment. */
5811 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5812 if (m->count != 0)
5814 s = m->sections[0];
5815 break;
5818 if (s != NULL)
5820 hash->root.u.def.value = filehdr_vaddr - s->vma;
5821 hash->root.u.def.section = s;
5823 else
5825 hash->root.u.def.value = filehdr_vaddr;
5826 hash->root.u.def.section = bfd_abs_section_ptr;
5829 hash->root.type = bfd_link_hash_defined;
5830 hash->def_regular = 1;
5831 hash->non_elf = 0;
5835 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5837 if (p->p_type == PT_GNU_RELRO)
5839 const Elf_Internal_Phdr *lp;
5840 struct elf_segment_map *lm;
5842 if (link_info != NULL)
5844 /* During linking the range of the RELRO segment is passed
5845 in link_info. */
5846 for (lm = elf_seg_map (abfd), lp = phdrs;
5847 lm != NULL;
5848 lm = lm->next, lp++)
5850 if (lp->p_type == PT_LOAD
5851 && lp->p_vaddr < link_info->relro_end
5852 && lm->count != 0
5853 && lm->sections[0]->vma >= link_info->relro_start)
5854 break;
5857 BFD_ASSERT (lm != NULL);
5859 else
5861 /* Otherwise we are copying an executable or shared
5862 library, but we need to use the same linker logic. */
5863 for (lp = phdrs; lp < phdrs + count; ++lp)
5865 if (lp->p_type == PT_LOAD
5866 && lp->p_paddr == p->p_paddr)
5867 break;
5871 if (lp < phdrs + count)
5873 p->p_vaddr = lp->p_vaddr;
5874 p->p_paddr = lp->p_paddr;
5875 p->p_offset = lp->p_offset;
5876 if (link_info != NULL)
5877 p->p_filesz = link_info->relro_end - lp->p_vaddr;
5878 else if (m->p_size_valid)
5879 p->p_filesz = m->p_size;
5880 else
5881 abort ();
5882 p->p_memsz = p->p_filesz;
5883 /* Preserve the alignment and flags if they are valid. The
5884 gold linker generates RW/4 for the PT_GNU_RELRO section.
5885 It is better for objcopy/strip to honor these attributes
5886 otherwise gdb will choke when using separate debug files.
5888 if (!m->p_align_valid)
5889 p->p_align = 1;
5890 if (!m->p_flags_valid)
5891 p->p_flags = PF_R;
5893 else
5895 memset (p, 0, sizeof *p);
5896 p->p_type = PT_NULL;
5899 else if (p->p_type == PT_GNU_STACK)
5901 if (m->p_size_valid)
5902 p->p_memsz = m->p_size;
5904 else if (m->count != 0)
5906 unsigned int i;
5908 if (p->p_type != PT_LOAD
5909 && (p->p_type != PT_NOTE
5910 || bfd_get_format (abfd) != bfd_core))
5912 /* A user specified segment layout may include a PHDR
5913 segment that overlaps with a LOAD segment... */
5914 if (p->p_type == PT_PHDR)
5916 m->count = 0;
5917 continue;
5920 if (m->includes_filehdr || m->includes_phdrs)
5922 /* PR 17512: file: 2195325e. */
5923 _bfd_error_handler
5924 (_("%B: error: non-load segment %d includes file header "
5925 "and/or program header"),
5926 abfd, (int) (p - phdrs));
5927 return FALSE;
5930 p->p_filesz = 0;
5931 p->p_offset = m->sections[0]->filepos;
5932 for (i = m->count; i-- != 0;)
5934 asection *sect = m->sections[i];
5935 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5936 if (hdr->sh_type != SHT_NOBITS)
5938 p->p_filesz = (sect->filepos - m->sections[0]->filepos
5939 + hdr->sh_size);
5940 break;
5945 else if (m->includes_filehdr)
5947 p->p_vaddr = filehdr_vaddr;
5948 if (! m->p_paddr_valid)
5949 p->p_paddr = filehdr_paddr;
5951 else if (m->includes_phdrs)
5953 p->p_vaddr = phdrs_vaddr;
5954 if (! m->p_paddr_valid)
5955 p->p_paddr = phdrs_paddr;
5959 elf_next_file_pos (abfd) = off;
5961 return TRUE;
5964 static elf_section_list *
5965 find_section_in_list (unsigned int i, elf_section_list * list)
5967 for (;list != NULL; list = list->next)
5968 if (list->ndx == i)
5969 break;
5970 return list;
5973 /* Work out the file positions of all the sections. This is called by
5974 _bfd_elf_compute_section_file_positions. All the section sizes and
5975 VMAs must be known before this is called.
5977 Reloc sections come in two flavours: Those processed specially as
5978 "side-channel" data attached to a section to which they apply, and
5979 those that bfd doesn't process as relocations. The latter sort are
5980 stored in a normal bfd section by bfd_section_from_shdr. We don't
5981 consider the former sort here, unless they form part of the loadable
5982 image. Reloc sections not assigned here will be handled later by
5983 assign_file_positions_for_relocs.
5985 We also don't set the positions of the .symtab and .strtab here. */
5987 static bfd_boolean
5988 assign_file_positions_except_relocs (bfd *abfd,
5989 struct bfd_link_info *link_info)
5991 struct elf_obj_tdata *tdata = elf_tdata (abfd);
5992 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5993 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5995 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5996 && bfd_get_format (abfd) != bfd_core)
5998 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5999 unsigned int num_sec = elf_numsections (abfd);
6000 Elf_Internal_Shdr **hdrpp;
6001 unsigned int i;
6002 file_ptr off;
6004 /* Start after the ELF header. */
6005 off = i_ehdrp->e_ehsize;
6007 /* We are not creating an executable, which means that we are
6008 not creating a program header, and that the actual order of
6009 the sections in the file is unimportant. */
6010 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6012 Elf_Internal_Shdr *hdr;
6014 hdr = *hdrpp;
6015 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6016 && hdr->bfd_section == NULL)
6017 || (hdr->bfd_section != NULL
6018 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
6019 /* Compress DWARF debug sections. */
6020 || i == elf_onesymtab (abfd)
6021 || (elf_symtab_shndx_list (abfd) != NULL
6022 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6023 || i == elf_strtab_sec (abfd)
6024 || i == elf_shstrtab_sec (abfd))
6026 hdr->sh_offset = -1;
6028 else
6029 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
6032 elf_next_file_pos (abfd) = off;
6034 else
6036 unsigned int alloc;
6038 /* Assign file positions for the loaded sections based on the
6039 assignment of sections to segments. */
6040 if (!assign_file_positions_for_load_sections (abfd, link_info))
6041 return FALSE;
6043 /* And for non-load sections. */
6044 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6045 return FALSE;
6047 if (bed->elf_backend_modify_program_headers != NULL)
6049 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6050 return FALSE;
6053 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6054 if (link_info != NULL && bfd_link_pie (link_info))
6056 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6057 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6058 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6060 /* Find the lowest p_vaddr in PT_LOAD segments. */
6061 bfd_vma p_vaddr = (bfd_vma) -1;
6062 for (; segment < end_segment; segment++)
6063 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6064 p_vaddr = segment->p_vaddr;
6066 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6067 segments is non-zero. */
6068 if (p_vaddr)
6069 i_ehdrp->e_type = ET_EXEC;
6072 /* Write out the program headers. */
6073 alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
6075 /* Sort the program headers into the ordering required by the ELF standard. */
6076 if (alloc == 0)
6077 return TRUE;
6079 /* PR ld/20815 - Check that the program header segment, if present, will
6080 be loaded into memory. FIXME: The check below is not sufficient as
6081 really all PT_LOAD segments should be checked before issuing an error
6082 message. Plus the PHDR segment does not have to be the first segment
6083 in the program header table. But this version of the check should
6084 catch all real world use cases.
6086 FIXME: We used to have code here to sort the PT_LOAD segments into
6087 ascending order, as per the ELF spec. But this breaks some programs,
6088 including the Linux kernel. But really either the spec should be
6089 changed or the programs updated. */
6090 if (alloc > 1
6091 && tdata->phdr[0].p_type == PT_PHDR
6092 && ! bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, alloc)
6093 && tdata->phdr[1].p_type == PT_LOAD
6094 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6095 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz)
6096 < (tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
6098 /* The fix for this error is usually to edit the linker script being
6099 used and set up the program headers manually. Either that or
6100 leave room for the headers at the start of the SECTIONS. */
6101 _bfd_error_handler (_("\
6102 %B: error: PHDR segment not covered by LOAD segment"),
6103 abfd);
6104 return FALSE;
6107 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
6108 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6109 return FALSE;
6112 return TRUE;
6115 static bfd_boolean
6116 prep_headers (bfd *abfd)
6118 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6119 struct elf_strtab_hash *shstrtab;
6120 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6122 i_ehdrp = elf_elfheader (abfd);
6124 shstrtab = _bfd_elf_strtab_init ();
6125 if (shstrtab == NULL)
6126 return FALSE;
6128 elf_shstrtab (abfd) = shstrtab;
6130 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6131 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6132 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6133 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6135 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6136 i_ehdrp->e_ident[EI_DATA] =
6137 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6138 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6140 if ((abfd->flags & DYNAMIC) != 0)
6141 i_ehdrp->e_type = ET_DYN;
6142 else if ((abfd->flags & EXEC_P) != 0)
6143 i_ehdrp->e_type = ET_EXEC;
6144 else if (bfd_get_format (abfd) == bfd_core)
6145 i_ehdrp->e_type = ET_CORE;
6146 else
6147 i_ehdrp->e_type = ET_REL;
6149 switch (bfd_get_arch (abfd))
6151 case bfd_arch_unknown:
6152 i_ehdrp->e_machine = EM_NONE;
6153 break;
6155 /* There used to be a long list of cases here, each one setting
6156 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6157 in the corresponding bfd definition. To avoid duplication,
6158 the switch was removed. Machines that need special handling
6159 can generally do it in elf_backend_final_write_processing(),
6160 unless they need the information earlier than the final write.
6161 Such need can generally be supplied by replacing the tests for
6162 e_machine with the conditions used to determine it. */
6163 default:
6164 i_ehdrp->e_machine = bed->elf_machine_code;
6167 i_ehdrp->e_version = bed->s->ev_current;
6168 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6170 /* No program header, for now. */
6171 i_ehdrp->e_phoff = 0;
6172 i_ehdrp->e_phentsize = 0;
6173 i_ehdrp->e_phnum = 0;
6175 /* Each bfd section is section header entry. */
6176 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6177 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6179 /* If we're building an executable, we'll need a program header table. */
6180 if (abfd->flags & EXEC_P)
6181 /* It all happens later. */
6183 else
6185 i_ehdrp->e_phentsize = 0;
6186 i_ehdrp->e_phoff = 0;
6189 elf_tdata (abfd)->symtab_hdr.sh_name =
6190 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
6191 elf_tdata (abfd)->strtab_hdr.sh_name =
6192 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
6193 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6194 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
6195 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6196 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6197 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6198 return FALSE;
6200 return TRUE;
6203 /* Assign file positions for all the reloc sections which are not part
6204 of the loadable file image, and the file position of section headers. */
6206 static bfd_boolean
6207 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6209 file_ptr off;
6210 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6211 Elf_Internal_Shdr *shdrp;
6212 Elf_Internal_Ehdr *i_ehdrp;
6213 const struct elf_backend_data *bed;
6215 off = elf_next_file_pos (abfd);
6217 shdrpp = elf_elfsections (abfd);
6218 end_shdrpp = shdrpp + elf_numsections (abfd);
6219 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6221 shdrp = *shdrpp;
6222 if (shdrp->sh_offset == -1)
6224 asection *sec = shdrp->bfd_section;
6225 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6226 || shdrp->sh_type == SHT_RELA);
6227 if (is_rel
6228 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6230 if (!is_rel)
6232 const char *name = sec->name;
6233 struct bfd_elf_section_data *d;
6235 /* Compress DWARF debug sections. */
6236 if (!bfd_compress_section (abfd, sec,
6237 shdrp->contents))
6238 return FALSE;
6240 if (sec->compress_status == COMPRESS_SECTION_DONE
6241 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6243 /* If section is compressed with zlib-gnu, convert
6244 section name from .debug_* to .zdebug_*. */
6245 char *new_name
6246 = convert_debug_to_zdebug (abfd, name);
6247 if (new_name == NULL)
6248 return FALSE;
6249 name = new_name;
6251 /* Add section name to section name section. */
6252 if (shdrp->sh_name != (unsigned int) -1)
6253 abort ();
6254 shdrp->sh_name
6255 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6256 name, FALSE);
6257 d = elf_section_data (sec);
6259 /* Add reloc section name to section name section. */
6260 if (d->rel.hdr
6261 && !_bfd_elf_set_reloc_sh_name (abfd,
6262 d->rel.hdr,
6263 name, FALSE))
6264 return FALSE;
6265 if (d->rela.hdr
6266 && !_bfd_elf_set_reloc_sh_name (abfd,
6267 d->rela.hdr,
6268 name, TRUE))
6269 return FALSE;
6271 /* Update section size and contents. */
6272 shdrp->sh_size = sec->size;
6273 shdrp->contents = sec->contents;
6274 shdrp->bfd_section->contents = NULL;
6276 off = _bfd_elf_assign_file_position_for_section (shdrp,
6277 off,
6278 TRUE);
6283 /* Place section name section after DWARF debug sections have been
6284 compressed. */
6285 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6286 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6287 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6288 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6290 /* Place the section headers. */
6291 i_ehdrp = elf_elfheader (abfd);
6292 bed = get_elf_backend_data (abfd);
6293 off = align_file_position (off, 1 << bed->s->log_file_align);
6294 i_ehdrp->e_shoff = off;
6295 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6296 elf_next_file_pos (abfd) = off;
6298 return TRUE;
6301 bfd_boolean
6302 _bfd_elf_write_object_contents (bfd *abfd)
6304 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6305 Elf_Internal_Shdr **i_shdrp;
6306 bfd_boolean failed;
6307 unsigned int count, num_sec;
6308 struct elf_obj_tdata *t;
6310 if (! abfd->output_has_begun
6311 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6312 return FALSE;
6314 i_shdrp = elf_elfsections (abfd);
6316 failed = FALSE;
6317 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6318 if (failed)
6319 return FALSE;
6321 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6322 return FALSE;
6324 /* After writing the headers, we need to write the sections too... */
6325 num_sec = elf_numsections (abfd);
6326 for (count = 1; count < num_sec; count++)
6328 i_shdrp[count]->sh_name
6329 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6330 i_shdrp[count]->sh_name);
6331 if (bed->elf_backend_section_processing)
6332 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
6333 if (i_shdrp[count]->contents)
6335 bfd_size_type amt = i_shdrp[count]->sh_size;
6337 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6338 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6339 return FALSE;
6343 /* Write out the section header names. */
6344 t = elf_tdata (abfd);
6345 if (elf_shstrtab (abfd) != NULL
6346 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6347 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6348 return FALSE;
6350 if (bed->elf_backend_final_write_processing)
6351 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
6353 if (!bed->s->write_shdrs_and_ehdr (abfd))
6354 return FALSE;
6356 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6357 if (t->o->build_id.after_write_object_contents != NULL)
6358 return (*t->o->build_id.after_write_object_contents) (abfd);
6360 return TRUE;
6363 bfd_boolean
6364 _bfd_elf_write_corefile_contents (bfd *abfd)
6366 /* Hopefully this can be done just like an object file. */
6367 return _bfd_elf_write_object_contents (abfd);
6370 /* Given a section, search the header to find them. */
6372 unsigned int
6373 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6375 const struct elf_backend_data *bed;
6376 unsigned int sec_index;
6378 if (elf_section_data (asect) != NULL
6379 && elf_section_data (asect)->this_idx != 0)
6380 return elf_section_data (asect)->this_idx;
6382 if (bfd_is_abs_section (asect))
6383 sec_index = SHN_ABS;
6384 else if (bfd_is_com_section (asect))
6385 sec_index = SHN_COMMON;
6386 else if (bfd_is_und_section (asect))
6387 sec_index = SHN_UNDEF;
6388 else
6389 sec_index = SHN_BAD;
6391 bed = get_elf_backend_data (abfd);
6392 if (bed->elf_backend_section_from_bfd_section)
6394 int retval = sec_index;
6396 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6397 return retval;
6400 if (sec_index == SHN_BAD)
6401 bfd_set_error (bfd_error_nonrepresentable_section);
6403 return sec_index;
6406 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6407 on error. */
6410 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6412 asymbol *asym_ptr = *asym_ptr_ptr;
6413 int idx;
6414 flagword flags = asym_ptr->flags;
6416 /* When gas creates relocations against local labels, it creates its
6417 own symbol for the section, but does put the symbol into the
6418 symbol chain, so udata is 0. When the linker is generating
6419 relocatable output, this section symbol may be for one of the
6420 input sections rather than the output section. */
6421 if (asym_ptr->udata.i == 0
6422 && (flags & BSF_SECTION_SYM)
6423 && asym_ptr->section)
6425 asection *sec;
6426 int indx;
6428 sec = asym_ptr->section;
6429 if (sec->owner != abfd && sec->output_section != NULL)
6430 sec = sec->output_section;
6431 if (sec->owner == abfd
6432 && (indx = sec->index) < elf_num_section_syms (abfd)
6433 && elf_section_syms (abfd)[indx] != NULL)
6434 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6437 idx = asym_ptr->udata.i;
6439 if (idx == 0)
6441 /* This case can occur when using --strip-symbol on a symbol
6442 which is used in a relocation entry. */
6443 _bfd_error_handler
6444 /* xgettext:c-format */
6445 (_("%B: symbol `%s' required but not present"),
6446 abfd, bfd_asymbol_name (asym_ptr));
6447 bfd_set_error (bfd_error_no_symbols);
6448 return -1;
6451 #if DEBUG & 4
6453 fprintf (stderr,
6454 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6455 (long) asym_ptr, asym_ptr->name, idx, flags);
6456 fflush (stderr);
6458 #endif
6460 return idx;
6463 /* Rewrite program header information. */
6465 static bfd_boolean
6466 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
6468 Elf_Internal_Ehdr *iehdr;
6469 struct elf_segment_map *map;
6470 struct elf_segment_map *map_first;
6471 struct elf_segment_map **pointer_to_map;
6472 Elf_Internal_Phdr *segment;
6473 asection *section;
6474 unsigned int i;
6475 unsigned int num_segments;
6476 bfd_boolean phdr_included = FALSE;
6477 bfd_boolean p_paddr_valid;
6478 bfd_vma maxpagesize;
6479 struct elf_segment_map *phdr_adjust_seg = NULL;
6480 unsigned int phdr_adjust_num = 0;
6481 const struct elf_backend_data *bed;
6483 bed = get_elf_backend_data (ibfd);
6484 iehdr = elf_elfheader (ibfd);
6486 map_first = NULL;
6487 pointer_to_map = &map_first;
6489 num_segments = elf_elfheader (ibfd)->e_phnum;
6490 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6492 /* Returns the end address of the segment + 1. */
6493 #define SEGMENT_END(segment, start) \
6494 (start + (segment->p_memsz > segment->p_filesz \
6495 ? segment->p_memsz : segment->p_filesz))
6497 #define SECTION_SIZE(section, segment) \
6498 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6499 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6500 ? section->size : 0)
6502 /* Returns TRUE if the given section is contained within
6503 the given segment. VMA addresses are compared. */
6504 #define IS_CONTAINED_BY_VMA(section, segment) \
6505 (section->vma >= segment->p_vaddr \
6506 && (section->vma + SECTION_SIZE (section, segment) \
6507 <= (SEGMENT_END (segment, segment->p_vaddr))))
6509 /* Returns TRUE if the given section is contained within
6510 the given segment. LMA addresses are compared. */
6511 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6512 (section->lma >= base \
6513 && (section->lma + SECTION_SIZE (section, segment) \
6514 <= SEGMENT_END (segment, base)))
6516 /* Handle PT_NOTE segment. */
6517 #define IS_NOTE(p, s) \
6518 (p->p_type == PT_NOTE \
6519 && elf_section_type (s) == SHT_NOTE \
6520 && (bfd_vma) s->filepos >= p->p_offset \
6521 && ((bfd_vma) s->filepos + s->size \
6522 <= p->p_offset + p->p_filesz))
6524 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6525 etc. */
6526 #define IS_COREFILE_NOTE(p, s) \
6527 (IS_NOTE (p, s) \
6528 && bfd_get_format (ibfd) == bfd_core \
6529 && s->vma == 0 \
6530 && s->lma == 0)
6532 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6533 linker, which generates a PT_INTERP section with p_vaddr and
6534 p_memsz set to 0. */
6535 #define IS_SOLARIS_PT_INTERP(p, s) \
6536 (p->p_vaddr == 0 \
6537 && p->p_paddr == 0 \
6538 && p->p_memsz == 0 \
6539 && p->p_filesz > 0 \
6540 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6541 && s->size > 0 \
6542 && (bfd_vma) s->filepos >= p->p_offset \
6543 && ((bfd_vma) s->filepos + s->size \
6544 <= p->p_offset + p->p_filesz))
6546 /* Decide if the given section should be included in the given segment.
6547 A section will be included if:
6548 1. It is within the address space of the segment -- we use the LMA
6549 if that is set for the segment and the VMA otherwise,
6550 2. It is an allocated section or a NOTE section in a PT_NOTE
6551 segment.
6552 3. There is an output section associated with it,
6553 4. The section has not already been allocated to a previous segment.
6554 5. PT_GNU_STACK segments do not include any sections.
6555 6. PT_TLS segment includes only SHF_TLS sections.
6556 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6557 8. PT_DYNAMIC should not contain empty sections at the beginning
6558 (with the possible exception of .dynamic). */
6559 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6560 ((((segment->p_paddr \
6561 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6562 : IS_CONTAINED_BY_VMA (section, segment)) \
6563 && (section->flags & SEC_ALLOC) != 0) \
6564 || IS_NOTE (segment, section)) \
6565 && segment->p_type != PT_GNU_STACK \
6566 && (segment->p_type != PT_TLS \
6567 || (section->flags & SEC_THREAD_LOCAL)) \
6568 && (segment->p_type == PT_LOAD \
6569 || segment->p_type == PT_TLS \
6570 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6571 && (segment->p_type != PT_DYNAMIC \
6572 || SECTION_SIZE (section, segment) > 0 \
6573 || (segment->p_paddr \
6574 ? segment->p_paddr != section->lma \
6575 : segment->p_vaddr != section->vma) \
6576 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6577 == 0)) \
6578 && !section->segment_mark)
6580 /* If the output section of a section in the input segment is NULL,
6581 it is removed from the corresponding output segment. */
6582 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6583 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6584 && section->output_section != NULL)
6586 /* Returns TRUE iff seg1 starts after the end of seg2. */
6587 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6588 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6590 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6591 their VMA address ranges and their LMA address ranges overlap.
6592 It is possible to have overlapping VMA ranges without overlapping LMA
6593 ranges. RedBoot images for example can have both .data and .bss mapped
6594 to the same VMA range, but with the .data section mapped to a different
6595 LMA. */
6596 #define SEGMENT_OVERLAPS(seg1, seg2) \
6597 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6598 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6599 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6600 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6602 /* Initialise the segment mark field. */
6603 for (section = ibfd->sections; section != NULL; section = section->next)
6604 section->segment_mark = FALSE;
6606 /* The Solaris linker creates program headers in which all the
6607 p_paddr fields are zero. When we try to objcopy or strip such a
6608 file, we get confused. Check for this case, and if we find it
6609 don't set the p_paddr_valid fields. */
6610 p_paddr_valid = FALSE;
6611 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6612 i < num_segments;
6613 i++, segment++)
6614 if (segment->p_paddr != 0)
6616 p_paddr_valid = TRUE;
6617 break;
6620 /* Scan through the segments specified in the program header
6621 of the input BFD. For this first scan we look for overlaps
6622 in the loadable segments. These can be created by weird
6623 parameters to objcopy. Also, fix some solaris weirdness. */
6624 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6625 i < num_segments;
6626 i++, segment++)
6628 unsigned int j;
6629 Elf_Internal_Phdr *segment2;
6631 if (segment->p_type == PT_INTERP)
6632 for (section = ibfd->sections; section; section = section->next)
6633 if (IS_SOLARIS_PT_INTERP (segment, section))
6635 /* Mininal change so that the normal section to segment
6636 assignment code will work. */
6637 segment->p_vaddr = section->vma;
6638 break;
6641 if (segment->p_type != PT_LOAD)
6643 /* Remove PT_GNU_RELRO segment. */
6644 if (segment->p_type == PT_GNU_RELRO)
6645 segment->p_type = PT_NULL;
6646 continue;
6649 /* Determine if this segment overlaps any previous segments. */
6650 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
6652 bfd_signed_vma extra_length;
6654 if (segment2->p_type != PT_LOAD
6655 || !SEGMENT_OVERLAPS (segment, segment2))
6656 continue;
6658 /* Merge the two segments together. */
6659 if (segment2->p_vaddr < segment->p_vaddr)
6661 /* Extend SEGMENT2 to include SEGMENT and then delete
6662 SEGMENT. */
6663 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6664 - SEGMENT_END (segment2, segment2->p_vaddr));
6666 if (extra_length > 0)
6668 segment2->p_memsz += extra_length;
6669 segment2->p_filesz += extra_length;
6672 segment->p_type = PT_NULL;
6674 /* Since we have deleted P we must restart the outer loop. */
6675 i = 0;
6676 segment = elf_tdata (ibfd)->phdr;
6677 break;
6679 else
6681 /* Extend SEGMENT to include SEGMENT2 and then delete
6682 SEGMENT2. */
6683 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6684 - SEGMENT_END (segment, segment->p_vaddr));
6686 if (extra_length > 0)
6688 segment->p_memsz += extra_length;
6689 segment->p_filesz += extra_length;
6692 segment2->p_type = PT_NULL;
6697 /* The second scan attempts to assign sections to segments. */
6698 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6699 i < num_segments;
6700 i++, segment++)
6702 unsigned int section_count;
6703 asection **sections;
6704 asection *output_section;
6705 unsigned int isec;
6706 bfd_vma matching_lma;
6707 bfd_vma suggested_lma;
6708 unsigned int j;
6709 bfd_size_type amt;
6710 asection *first_section;
6711 bfd_boolean first_matching_lma;
6712 bfd_boolean first_suggested_lma;
6714 if (segment->p_type == PT_NULL)
6715 continue;
6717 first_section = NULL;
6718 /* Compute how many sections might be placed into this segment. */
6719 for (section = ibfd->sections, section_count = 0;
6720 section != NULL;
6721 section = section->next)
6723 /* Find the first section in the input segment, which may be
6724 removed from the corresponding output segment. */
6725 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6727 if (first_section == NULL)
6728 first_section = section;
6729 if (section->output_section != NULL)
6730 ++section_count;
6734 /* Allocate a segment map big enough to contain
6735 all of the sections we have selected. */
6736 amt = sizeof (struct elf_segment_map);
6737 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6738 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6739 if (map == NULL)
6740 return FALSE;
6742 /* Initialise the fields of the segment map. Default to
6743 using the physical address of the segment in the input BFD. */
6744 map->next = NULL;
6745 map->p_type = segment->p_type;
6746 map->p_flags = segment->p_flags;
6747 map->p_flags_valid = 1;
6749 /* If the first section in the input segment is removed, there is
6750 no need to preserve segment physical address in the corresponding
6751 output segment. */
6752 if (!first_section || first_section->output_section != NULL)
6754 map->p_paddr = segment->p_paddr;
6755 map->p_paddr_valid = p_paddr_valid;
6758 /* Determine if this segment contains the ELF file header
6759 and if it contains the program headers themselves. */
6760 map->includes_filehdr = (segment->p_offset == 0
6761 && segment->p_filesz >= iehdr->e_ehsize);
6762 map->includes_phdrs = 0;
6764 if (!phdr_included || segment->p_type != PT_LOAD)
6766 map->includes_phdrs =
6767 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6768 && (segment->p_offset + segment->p_filesz
6769 >= ((bfd_vma) iehdr->e_phoff
6770 + iehdr->e_phnum * iehdr->e_phentsize)));
6772 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6773 phdr_included = TRUE;
6776 if (section_count == 0)
6778 /* Special segments, such as the PT_PHDR segment, may contain
6779 no sections, but ordinary, loadable segments should contain
6780 something. They are allowed by the ELF spec however, so only
6781 a warning is produced.
6782 There is however the valid use case of embedded systems which
6783 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
6784 flash memory with zeros. No warning is shown for that case. */
6785 if (segment->p_type == PT_LOAD
6786 && (segment->p_filesz > 0 || segment->p_memsz == 0))
6787 /* xgettext:c-format */
6788 _bfd_error_handler (_("%B: warning: Empty loadable segment detected"
6789 " at vaddr=%#Lx, is this intentional?"),
6790 ibfd, segment->p_vaddr);
6792 map->count = 0;
6793 *pointer_to_map = map;
6794 pointer_to_map = &map->next;
6796 continue;
6799 /* Now scan the sections in the input BFD again and attempt
6800 to add their corresponding output sections to the segment map.
6801 The problem here is how to handle an output section which has
6802 been moved (ie had its LMA changed). There are four possibilities:
6804 1. None of the sections have been moved.
6805 In this case we can continue to use the segment LMA from the
6806 input BFD.
6808 2. All of the sections have been moved by the same amount.
6809 In this case we can change the segment's LMA to match the LMA
6810 of the first section.
6812 3. Some of the sections have been moved, others have not.
6813 In this case those sections which have not been moved can be
6814 placed in the current segment which will have to have its size,
6815 and possibly its LMA changed, and a new segment or segments will
6816 have to be created to contain the other sections.
6818 4. The sections have been moved, but not by the same amount.
6819 In this case we can change the segment's LMA to match the LMA
6820 of the first section and we will have to create a new segment
6821 or segments to contain the other sections.
6823 In order to save time, we allocate an array to hold the section
6824 pointers that we are interested in. As these sections get assigned
6825 to a segment, they are removed from this array. */
6827 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
6828 if (sections == NULL)
6829 return FALSE;
6831 /* Step One: Scan for segment vs section LMA conflicts.
6832 Also add the sections to the section array allocated above.
6833 Also add the sections to the current segment. In the common
6834 case, where the sections have not been moved, this means that
6835 we have completely filled the segment, and there is nothing
6836 more to do. */
6837 isec = 0;
6838 matching_lma = 0;
6839 suggested_lma = 0;
6840 first_matching_lma = TRUE;
6841 first_suggested_lma = TRUE;
6843 for (section = first_section, j = 0;
6844 section != NULL;
6845 section = section->next)
6847 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
6849 output_section = section->output_section;
6851 sections[j++] = section;
6853 /* The Solaris native linker always sets p_paddr to 0.
6854 We try to catch that case here, and set it to the
6855 correct value. Note - some backends require that
6856 p_paddr be left as zero. */
6857 if (!p_paddr_valid
6858 && segment->p_vaddr != 0
6859 && !bed->want_p_paddr_set_to_zero
6860 && isec == 0
6861 && output_section->lma != 0
6862 && output_section->vma == (segment->p_vaddr
6863 + (map->includes_filehdr
6864 ? iehdr->e_ehsize
6865 : 0)
6866 + (map->includes_phdrs
6867 ? (iehdr->e_phnum
6868 * iehdr->e_phentsize)
6869 : 0)))
6870 map->p_paddr = segment->p_vaddr;
6872 /* Match up the physical address of the segment with the
6873 LMA address of the output section. */
6874 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6875 || IS_COREFILE_NOTE (segment, section)
6876 || (bed->want_p_paddr_set_to_zero
6877 && IS_CONTAINED_BY_VMA (output_section, segment)))
6879 if (first_matching_lma || output_section->lma < matching_lma)
6881 matching_lma = output_section->lma;
6882 first_matching_lma = FALSE;
6885 /* We assume that if the section fits within the segment
6886 then it does not overlap any other section within that
6887 segment. */
6888 map->sections[isec++] = output_section;
6890 else if (first_suggested_lma)
6892 suggested_lma = output_section->lma;
6893 first_suggested_lma = FALSE;
6896 if (j == section_count)
6897 break;
6901 BFD_ASSERT (j == section_count);
6903 /* Step Two: Adjust the physical address of the current segment,
6904 if necessary. */
6905 if (isec == section_count)
6907 /* All of the sections fitted within the segment as currently
6908 specified. This is the default case. Add the segment to
6909 the list of built segments and carry on to process the next
6910 program header in the input BFD. */
6911 map->count = section_count;
6912 *pointer_to_map = map;
6913 pointer_to_map = &map->next;
6915 if (p_paddr_valid
6916 && !bed->want_p_paddr_set_to_zero
6917 && matching_lma != map->p_paddr
6918 && !map->includes_filehdr
6919 && !map->includes_phdrs)
6920 /* There is some padding before the first section in the
6921 segment. So, we must account for that in the output
6922 segment's vma. */
6923 map->p_vaddr_offset = matching_lma - map->p_paddr;
6925 free (sections);
6926 continue;
6928 else
6930 if (!first_matching_lma)
6932 /* At least one section fits inside the current segment.
6933 Keep it, but modify its physical address to match the
6934 LMA of the first section that fitted. */
6935 map->p_paddr = matching_lma;
6937 else
6939 /* None of the sections fitted inside the current segment.
6940 Change the current segment's physical address to match
6941 the LMA of the first section. */
6942 map->p_paddr = suggested_lma;
6945 /* Offset the segment physical address from the lma
6946 to allow for space taken up by elf headers. */
6947 if (map->includes_filehdr)
6949 if (map->p_paddr >= iehdr->e_ehsize)
6950 map->p_paddr -= iehdr->e_ehsize;
6951 else
6953 map->includes_filehdr = FALSE;
6954 map->includes_phdrs = FALSE;
6958 if (map->includes_phdrs)
6960 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
6962 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
6964 /* iehdr->e_phnum is just an estimate of the number
6965 of program headers that we will need. Make a note
6966 here of the number we used and the segment we chose
6967 to hold these headers, so that we can adjust the
6968 offset when we know the correct value. */
6969 phdr_adjust_num = iehdr->e_phnum;
6970 phdr_adjust_seg = map;
6972 else
6973 map->includes_phdrs = FALSE;
6977 /* Step Three: Loop over the sections again, this time assigning
6978 those that fit to the current segment and removing them from the
6979 sections array; but making sure not to leave large gaps. Once all
6980 possible sections have been assigned to the current segment it is
6981 added to the list of built segments and if sections still remain
6982 to be assigned, a new segment is constructed before repeating
6983 the loop. */
6984 isec = 0;
6987 map->count = 0;
6988 suggested_lma = 0;
6989 first_suggested_lma = TRUE;
6991 /* Fill the current segment with sections that fit. */
6992 for (j = 0; j < section_count; j++)
6994 section = sections[j];
6996 if (section == NULL)
6997 continue;
6999 output_section = section->output_section;
7001 BFD_ASSERT (output_section != NULL);
7003 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7004 || IS_COREFILE_NOTE (segment, section))
7006 if (map->count == 0)
7008 /* If the first section in a segment does not start at
7009 the beginning of the segment, then something is
7010 wrong. */
7011 if (output_section->lma
7012 != (map->p_paddr
7013 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
7014 + (map->includes_phdrs
7015 ? iehdr->e_phnum * iehdr->e_phentsize
7016 : 0)))
7017 abort ();
7019 else
7021 asection *prev_sec;
7023 prev_sec = map->sections[map->count - 1];
7025 /* If the gap between the end of the previous section
7026 and the start of this section is more than
7027 maxpagesize then we need to start a new segment. */
7028 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7029 maxpagesize)
7030 < BFD_ALIGN (output_section->lma, maxpagesize))
7031 || (prev_sec->lma + prev_sec->size
7032 > output_section->lma))
7034 if (first_suggested_lma)
7036 suggested_lma = output_section->lma;
7037 first_suggested_lma = FALSE;
7040 continue;
7044 map->sections[map->count++] = output_section;
7045 ++isec;
7046 sections[j] = NULL;
7047 section->segment_mark = TRUE;
7049 else if (first_suggested_lma)
7051 suggested_lma = output_section->lma;
7052 first_suggested_lma = FALSE;
7056 BFD_ASSERT (map->count > 0);
7058 /* Add the current segment to the list of built segments. */
7059 *pointer_to_map = map;
7060 pointer_to_map = &map->next;
7062 if (isec < section_count)
7064 /* We still have not allocated all of the sections to
7065 segments. Create a new segment here, initialise it
7066 and carry on looping. */
7067 amt = sizeof (struct elf_segment_map);
7068 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
7069 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7070 if (map == NULL)
7072 free (sections);
7073 return FALSE;
7076 /* Initialise the fields of the segment map. Set the physical
7077 physical address to the LMA of the first section that has
7078 not yet been assigned. */
7079 map->next = NULL;
7080 map->p_type = segment->p_type;
7081 map->p_flags = segment->p_flags;
7082 map->p_flags_valid = 1;
7083 map->p_paddr = suggested_lma;
7084 map->p_paddr_valid = p_paddr_valid;
7085 map->includes_filehdr = 0;
7086 map->includes_phdrs = 0;
7089 while (isec < section_count);
7091 free (sections);
7094 elf_seg_map (obfd) = map_first;
7096 /* If we had to estimate the number of program headers that were
7097 going to be needed, then check our estimate now and adjust
7098 the offset if necessary. */
7099 if (phdr_adjust_seg != NULL)
7101 unsigned int count;
7103 for (count = 0, map = map_first; map != NULL; map = map->next)
7104 count++;
7106 if (count > phdr_adjust_num)
7107 phdr_adjust_seg->p_paddr
7108 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7111 #undef SEGMENT_END
7112 #undef SECTION_SIZE
7113 #undef IS_CONTAINED_BY_VMA
7114 #undef IS_CONTAINED_BY_LMA
7115 #undef IS_NOTE
7116 #undef IS_COREFILE_NOTE
7117 #undef IS_SOLARIS_PT_INTERP
7118 #undef IS_SECTION_IN_INPUT_SEGMENT
7119 #undef INCLUDE_SECTION_IN_SEGMENT
7120 #undef SEGMENT_AFTER_SEGMENT
7121 #undef SEGMENT_OVERLAPS
7122 return TRUE;
7125 /* Copy ELF program header information. */
7127 static bfd_boolean
7128 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7130 Elf_Internal_Ehdr *iehdr;
7131 struct elf_segment_map *map;
7132 struct elf_segment_map *map_first;
7133 struct elf_segment_map **pointer_to_map;
7134 Elf_Internal_Phdr *segment;
7135 unsigned int i;
7136 unsigned int num_segments;
7137 bfd_boolean phdr_included = FALSE;
7138 bfd_boolean p_paddr_valid;
7140 iehdr = elf_elfheader (ibfd);
7142 map_first = NULL;
7143 pointer_to_map = &map_first;
7145 /* If all the segment p_paddr fields are zero, don't set
7146 map->p_paddr_valid. */
7147 p_paddr_valid = FALSE;
7148 num_segments = elf_elfheader (ibfd)->e_phnum;
7149 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7150 i < num_segments;
7151 i++, segment++)
7152 if (segment->p_paddr != 0)
7154 p_paddr_valid = TRUE;
7155 break;
7158 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7159 i < num_segments;
7160 i++, segment++)
7162 asection *section;
7163 unsigned int section_count;
7164 bfd_size_type amt;
7165 Elf_Internal_Shdr *this_hdr;
7166 asection *first_section = NULL;
7167 asection *lowest_section;
7169 /* Compute how many sections are in this segment. */
7170 for (section = ibfd->sections, section_count = 0;
7171 section != NULL;
7172 section = section->next)
7174 this_hdr = &(elf_section_data(section)->this_hdr);
7175 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7177 if (first_section == NULL)
7178 first_section = section;
7179 section_count++;
7183 /* Allocate a segment map big enough to contain
7184 all of the sections we have selected. */
7185 amt = sizeof (struct elf_segment_map);
7186 if (section_count != 0)
7187 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
7188 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7189 if (map == NULL)
7190 return FALSE;
7192 /* Initialize the fields of the output segment map with the
7193 input segment. */
7194 map->next = NULL;
7195 map->p_type = segment->p_type;
7196 map->p_flags = segment->p_flags;
7197 map->p_flags_valid = 1;
7198 map->p_paddr = segment->p_paddr;
7199 map->p_paddr_valid = p_paddr_valid;
7200 map->p_align = segment->p_align;
7201 map->p_align_valid = 1;
7202 map->p_vaddr_offset = 0;
7204 if (map->p_type == PT_GNU_RELRO
7205 || map->p_type == PT_GNU_STACK)
7207 /* The PT_GNU_RELRO segment may contain the first a few
7208 bytes in the .got.plt section even if the whole .got.plt
7209 section isn't in the PT_GNU_RELRO segment. We won't
7210 change the size of the PT_GNU_RELRO segment.
7211 Similarly, PT_GNU_STACK size is significant on uclinux
7212 systems. */
7213 map->p_size = segment->p_memsz;
7214 map->p_size_valid = 1;
7217 /* Determine if this segment contains the ELF file header
7218 and if it contains the program headers themselves. */
7219 map->includes_filehdr = (segment->p_offset == 0
7220 && segment->p_filesz >= iehdr->e_ehsize);
7222 map->includes_phdrs = 0;
7223 if (! phdr_included || segment->p_type != PT_LOAD)
7225 map->includes_phdrs =
7226 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7227 && (segment->p_offset + segment->p_filesz
7228 >= ((bfd_vma) iehdr->e_phoff
7229 + iehdr->e_phnum * iehdr->e_phentsize)));
7231 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7232 phdr_included = TRUE;
7235 lowest_section = NULL;
7236 if (section_count != 0)
7238 unsigned int isec = 0;
7240 for (section = first_section;
7241 section != NULL;
7242 section = section->next)
7244 this_hdr = &(elf_section_data(section)->this_hdr);
7245 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7247 map->sections[isec++] = section->output_section;
7248 if ((section->flags & SEC_ALLOC) != 0)
7250 bfd_vma seg_off;
7252 if (lowest_section == NULL
7253 || section->lma < lowest_section->lma)
7254 lowest_section = section;
7256 /* Section lmas are set up from PT_LOAD header
7257 p_paddr in _bfd_elf_make_section_from_shdr.
7258 If this header has a p_paddr that disagrees
7259 with the section lma, flag the p_paddr as
7260 invalid. */
7261 if ((section->flags & SEC_LOAD) != 0)
7262 seg_off = this_hdr->sh_offset - segment->p_offset;
7263 else
7264 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7265 if (section->lma - segment->p_paddr != seg_off)
7266 map->p_paddr_valid = FALSE;
7268 if (isec == section_count)
7269 break;
7274 if (map->includes_filehdr && lowest_section != NULL)
7275 /* We need to keep the space used by the headers fixed. */
7276 map->header_size = lowest_section->vma - segment->p_vaddr;
7278 if (!map->includes_phdrs
7279 && !map->includes_filehdr
7280 && map->p_paddr_valid)
7281 /* There is some other padding before the first section. */
7282 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
7283 - segment->p_paddr);
7285 map->count = section_count;
7286 *pointer_to_map = map;
7287 pointer_to_map = &map->next;
7290 elf_seg_map (obfd) = map_first;
7291 return TRUE;
7294 /* Copy private BFD data. This copies or rewrites ELF program header
7295 information. */
7297 static bfd_boolean
7298 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7300 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7301 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7302 return TRUE;
7304 if (elf_tdata (ibfd)->phdr == NULL)
7305 return TRUE;
7307 if (ibfd->xvec == obfd->xvec)
7309 /* Check to see if any sections in the input BFD
7310 covered by ELF program header have changed. */
7311 Elf_Internal_Phdr *segment;
7312 asection *section, *osec;
7313 unsigned int i, num_segments;
7314 Elf_Internal_Shdr *this_hdr;
7315 const struct elf_backend_data *bed;
7317 bed = get_elf_backend_data (ibfd);
7319 /* Regenerate the segment map if p_paddr is set to 0. */
7320 if (bed->want_p_paddr_set_to_zero)
7321 goto rewrite;
7323 /* Initialize the segment mark field. */
7324 for (section = obfd->sections; section != NULL;
7325 section = section->next)
7326 section->segment_mark = FALSE;
7328 num_segments = elf_elfheader (ibfd)->e_phnum;
7329 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7330 i < num_segments;
7331 i++, segment++)
7333 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7334 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7335 which severly confuses things, so always regenerate the segment
7336 map in this case. */
7337 if (segment->p_paddr == 0
7338 && segment->p_memsz == 0
7339 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
7340 goto rewrite;
7342 for (section = ibfd->sections;
7343 section != NULL; section = section->next)
7345 /* We mark the output section so that we know it comes
7346 from the input BFD. */
7347 osec = section->output_section;
7348 if (osec)
7349 osec->segment_mark = TRUE;
7351 /* Check if this section is covered by the segment. */
7352 this_hdr = &(elf_section_data(section)->this_hdr);
7353 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7355 /* FIXME: Check if its output section is changed or
7356 removed. What else do we need to check? */
7357 if (osec == NULL
7358 || section->flags != osec->flags
7359 || section->lma != osec->lma
7360 || section->vma != osec->vma
7361 || section->size != osec->size
7362 || section->rawsize != osec->rawsize
7363 || section->alignment_power != osec->alignment_power)
7364 goto rewrite;
7369 /* Check to see if any output section do not come from the
7370 input BFD. */
7371 for (section = obfd->sections; section != NULL;
7372 section = section->next)
7374 if (!section->segment_mark)
7375 goto rewrite;
7376 else
7377 section->segment_mark = FALSE;
7380 return copy_elf_program_header (ibfd, obfd);
7383 rewrite:
7384 if (ibfd->xvec == obfd->xvec)
7386 /* When rewriting program header, set the output maxpagesize to
7387 the maximum alignment of input PT_LOAD segments. */
7388 Elf_Internal_Phdr *segment;
7389 unsigned int i;
7390 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7391 bfd_vma maxpagesize = 0;
7393 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7394 i < num_segments;
7395 i++, segment++)
7396 if (segment->p_type == PT_LOAD
7397 && maxpagesize < segment->p_align)
7399 /* PR 17512: file: f17299af. */
7400 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7401 /* xgettext:c-format */
7402 _bfd_error_handler (_("%B: warning: segment alignment of %#Lx"
7403 " is too large"),
7404 ibfd, segment->p_align);
7405 else
7406 maxpagesize = segment->p_align;
7409 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7410 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7413 return rewrite_elf_program_header (ibfd, obfd);
7416 /* Initialize private output section information from input section. */
7418 bfd_boolean
7419 _bfd_elf_init_private_section_data (bfd *ibfd,
7420 asection *isec,
7421 bfd *obfd,
7422 asection *osec,
7423 struct bfd_link_info *link_info)
7426 Elf_Internal_Shdr *ihdr, *ohdr;
7427 bfd_boolean final_link = (link_info != NULL
7428 && !bfd_link_relocatable (link_info));
7430 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7431 || obfd->xvec->flavour != bfd_target_elf_flavour)
7432 return TRUE;
7434 BFD_ASSERT (elf_section_data (osec) != NULL);
7436 /* For objcopy and relocatable link, don't copy the output ELF
7437 section type from input if the output BFD section flags have been
7438 set to something different. For a final link allow some flags
7439 that the linker clears to differ. */
7440 if (elf_section_type (osec) == SHT_NULL
7441 && (osec->flags == isec->flags
7442 || (final_link
7443 && ((osec->flags ^ isec->flags)
7444 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7445 elf_section_type (osec) = elf_section_type (isec);
7447 /* FIXME: Is this correct for all OS/PROC specific flags? */
7448 elf_section_flags (osec) |= (elf_section_flags (isec)
7449 & (SHF_MASKOS | SHF_MASKPROC));
7451 /* Copy sh_info from input for mbind section. */
7452 if (elf_section_flags (isec) & SHF_GNU_MBIND)
7453 elf_section_data (osec)->this_hdr.sh_info
7454 = elf_section_data (isec)->this_hdr.sh_info;
7456 /* Set things up for objcopy and relocatable link. The output
7457 SHT_GROUP section will have its elf_next_in_group pointing back
7458 to the input group members. Ignore linker created group section.
7459 See elfNN_ia64_object_p in elfxx-ia64.c. */
7460 if ((link_info == NULL
7461 || !link_info->resolve_section_groups)
7462 && (elf_sec_group (isec) == NULL
7463 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7465 if (elf_section_flags (isec) & SHF_GROUP)
7466 elf_section_flags (osec) |= SHF_GROUP;
7467 elf_next_in_group (osec) = elf_next_in_group (isec);
7468 elf_section_data (osec)->group = elf_section_data (isec)->group;
7471 /* If not decompress, preserve SHF_COMPRESSED. */
7472 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7473 elf_section_flags (osec) |= (elf_section_flags (isec)
7474 & SHF_COMPRESSED);
7476 ihdr = &elf_section_data (isec)->this_hdr;
7478 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7479 don't use the output section of the linked-to section since it
7480 may be NULL at this point. */
7481 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7483 ohdr = &elf_section_data (osec)->this_hdr;
7484 ohdr->sh_flags |= SHF_LINK_ORDER;
7485 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7488 osec->use_rela_p = isec->use_rela_p;
7490 return TRUE;
7493 /* Copy private section information. This copies over the entsize
7494 field, and sometimes the info field. */
7496 bfd_boolean
7497 _bfd_elf_copy_private_section_data (bfd *ibfd,
7498 asection *isec,
7499 bfd *obfd,
7500 asection *osec)
7502 Elf_Internal_Shdr *ihdr, *ohdr;
7504 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7505 || obfd->xvec->flavour != bfd_target_elf_flavour)
7506 return TRUE;
7508 ihdr = &elf_section_data (isec)->this_hdr;
7509 ohdr = &elf_section_data (osec)->this_hdr;
7511 ohdr->sh_entsize = ihdr->sh_entsize;
7513 if (ihdr->sh_type == SHT_SYMTAB
7514 || ihdr->sh_type == SHT_DYNSYM
7515 || ihdr->sh_type == SHT_GNU_verneed
7516 || ihdr->sh_type == SHT_GNU_verdef)
7517 ohdr->sh_info = ihdr->sh_info;
7519 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7520 NULL);
7523 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7524 necessary if we are removing either the SHT_GROUP section or any of
7525 the group member sections. DISCARDED is the value that a section's
7526 output_section has if the section will be discarded, NULL when this
7527 function is called from objcopy, bfd_abs_section_ptr when called
7528 from the linker. */
7530 bfd_boolean
7531 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7533 asection *isec;
7535 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7536 if (elf_section_type (isec) == SHT_GROUP)
7538 asection *first = elf_next_in_group (isec);
7539 asection *s = first;
7540 bfd_size_type removed = 0;
7542 while (s != NULL)
7544 /* If this member section is being output but the
7545 SHT_GROUP section is not, then clear the group info
7546 set up by _bfd_elf_copy_private_section_data. */
7547 if (s->output_section != discarded
7548 && isec->output_section == discarded)
7550 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7551 elf_group_name (s->output_section) = NULL;
7553 /* Conversely, if the member section is not being output
7554 but the SHT_GROUP section is, then adjust its size. */
7555 else if (s->output_section == discarded
7556 && isec->output_section != discarded)
7557 removed += 4;
7558 s = elf_next_in_group (s);
7559 if (s == first)
7560 break;
7562 if (removed != 0)
7564 if (discarded != NULL)
7566 /* If we've been called for ld -r, then we need to
7567 adjust the input section size. This function may
7568 be called multiple times, so save the original
7569 size. */
7570 if (isec->rawsize == 0)
7571 isec->rawsize = isec->size;
7572 isec->size = isec->rawsize - removed;
7574 else
7576 /* Adjust the output section size when called from
7577 objcopy. */
7578 isec->output_section->size -= removed;
7583 return TRUE;
7586 /* Copy private header information. */
7588 bfd_boolean
7589 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7591 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7592 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7593 return TRUE;
7595 /* Copy over private BFD data if it has not already been copied.
7596 This must be done here, rather than in the copy_private_bfd_data
7597 entry point, because the latter is called after the section
7598 contents have been set, which means that the program headers have
7599 already been worked out. */
7600 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
7602 if (! copy_private_bfd_data (ibfd, obfd))
7603 return FALSE;
7606 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7609 /* Copy private symbol information. If this symbol is in a section
7610 which we did not map into a BFD section, try to map the section
7611 index correctly. We use special macro definitions for the mapped
7612 section indices; these definitions are interpreted by the
7613 swap_out_syms function. */
7615 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7616 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7617 #define MAP_STRTAB (SHN_HIOS + 3)
7618 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7619 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7621 bfd_boolean
7622 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
7623 asymbol *isymarg,
7624 bfd *obfd,
7625 asymbol *osymarg)
7627 elf_symbol_type *isym, *osym;
7629 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7630 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7631 return TRUE;
7633 isym = elf_symbol_from (ibfd, isymarg);
7634 osym = elf_symbol_from (obfd, osymarg);
7636 if (isym != NULL
7637 && isym->internal_elf_sym.st_shndx != 0
7638 && osym != NULL
7639 && bfd_is_abs_section (isym->symbol.section))
7641 unsigned int shndx;
7643 shndx = isym->internal_elf_sym.st_shndx;
7644 if (shndx == elf_onesymtab (ibfd))
7645 shndx = MAP_ONESYMTAB;
7646 else if (shndx == elf_dynsymtab (ibfd))
7647 shndx = MAP_DYNSYMTAB;
7648 else if (shndx == elf_strtab_sec (ibfd))
7649 shndx = MAP_STRTAB;
7650 else if (shndx == elf_shstrtab_sec (ibfd))
7651 shndx = MAP_SHSTRTAB;
7652 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
7653 shndx = MAP_SYM_SHNDX;
7654 osym->internal_elf_sym.st_shndx = shndx;
7657 return TRUE;
7660 /* Swap out the symbols. */
7662 static bfd_boolean
7663 swap_out_syms (bfd *abfd,
7664 struct elf_strtab_hash **sttp,
7665 int relocatable_p)
7667 const struct elf_backend_data *bed;
7668 int symcount;
7669 asymbol **syms;
7670 struct elf_strtab_hash *stt;
7671 Elf_Internal_Shdr *symtab_hdr;
7672 Elf_Internal_Shdr *symtab_shndx_hdr;
7673 Elf_Internal_Shdr *symstrtab_hdr;
7674 struct elf_sym_strtab *symstrtab;
7675 bfd_byte *outbound_syms;
7676 bfd_byte *outbound_shndx;
7677 unsigned long outbound_syms_index;
7678 unsigned long outbound_shndx_index;
7679 int idx;
7680 unsigned int num_locals;
7681 bfd_size_type amt;
7682 bfd_boolean name_local_sections;
7684 if (!elf_map_symbols (abfd, &num_locals))
7685 return FALSE;
7687 /* Dump out the symtabs. */
7688 stt = _bfd_elf_strtab_init ();
7689 if (stt == NULL)
7690 return FALSE;
7692 bed = get_elf_backend_data (abfd);
7693 symcount = bfd_get_symcount (abfd);
7694 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7695 symtab_hdr->sh_type = SHT_SYMTAB;
7696 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7697 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
7698 symtab_hdr->sh_info = num_locals + 1;
7699 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
7701 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7702 symstrtab_hdr->sh_type = SHT_STRTAB;
7704 /* Allocate buffer to swap out the .strtab section. */
7705 symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1)
7706 * sizeof (*symstrtab));
7707 if (symstrtab == NULL)
7709 _bfd_elf_strtab_free (stt);
7710 return FALSE;
7713 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
7714 bed->s->sizeof_sym);
7715 if (outbound_syms == NULL)
7717 error_return:
7718 _bfd_elf_strtab_free (stt);
7719 free (symstrtab);
7720 return FALSE;
7722 symtab_hdr->contents = outbound_syms;
7723 outbound_syms_index = 0;
7725 outbound_shndx = NULL;
7726 outbound_shndx_index = 0;
7728 if (elf_symtab_shndx_list (abfd))
7730 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
7731 if (symtab_shndx_hdr->sh_name != 0)
7733 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7734 outbound_shndx = (bfd_byte *)
7735 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7736 if (outbound_shndx == NULL)
7737 goto error_return;
7739 symtab_shndx_hdr->contents = outbound_shndx;
7740 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7741 symtab_shndx_hdr->sh_size = amt;
7742 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7743 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7745 /* FIXME: What about any other headers in the list ? */
7748 /* Now generate the data (for "contents"). */
7750 /* Fill in zeroth symbol and swap it out. */
7751 Elf_Internal_Sym sym;
7752 sym.st_name = 0;
7753 sym.st_value = 0;
7754 sym.st_size = 0;
7755 sym.st_info = 0;
7756 sym.st_other = 0;
7757 sym.st_shndx = SHN_UNDEF;
7758 sym.st_target_internal = 0;
7759 symstrtab[0].sym = sym;
7760 symstrtab[0].dest_index = outbound_syms_index;
7761 symstrtab[0].destshndx_index = outbound_shndx_index;
7762 outbound_syms_index++;
7763 if (outbound_shndx != NULL)
7764 outbound_shndx_index++;
7767 name_local_sections
7768 = (bed->elf_backend_name_local_section_symbols
7769 && bed->elf_backend_name_local_section_symbols (abfd));
7771 syms = bfd_get_outsymbols (abfd);
7772 for (idx = 0; idx < symcount;)
7774 Elf_Internal_Sym sym;
7775 bfd_vma value = syms[idx]->value;
7776 elf_symbol_type *type_ptr;
7777 flagword flags = syms[idx]->flags;
7778 int type;
7780 if (!name_local_sections
7781 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
7783 /* Local section symbols have no name. */
7784 sym.st_name = (unsigned long) -1;
7786 else
7788 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7789 to get the final offset for st_name. */
7790 sym.st_name
7791 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
7792 FALSE);
7793 if (sym.st_name == (unsigned long) -1)
7794 goto error_return;
7797 type_ptr = elf_symbol_from (abfd, syms[idx]);
7799 if ((flags & BSF_SECTION_SYM) == 0
7800 && bfd_is_com_section (syms[idx]->section))
7802 /* ELF common symbols put the alignment into the `value' field,
7803 and the size into the `size' field. This is backwards from
7804 how BFD handles it, so reverse it here. */
7805 sym.st_size = value;
7806 if (type_ptr == NULL
7807 || type_ptr->internal_elf_sym.st_value == 0)
7808 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
7809 else
7810 sym.st_value = type_ptr->internal_elf_sym.st_value;
7811 sym.st_shndx = _bfd_elf_section_from_bfd_section
7812 (abfd, syms[idx]->section);
7814 else
7816 asection *sec = syms[idx]->section;
7817 unsigned int shndx;
7819 if (sec->output_section)
7821 value += sec->output_offset;
7822 sec = sec->output_section;
7825 /* Don't add in the section vma for relocatable output. */
7826 if (! relocatable_p)
7827 value += sec->vma;
7828 sym.st_value = value;
7829 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
7831 if (bfd_is_abs_section (sec)
7832 && type_ptr != NULL
7833 && type_ptr->internal_elf_sym.st_shndx != 0)
7835 /* This symbol is in a real ELF section which we did
7836 not create as a BFD section. Undo the mapping done
7837 by copy_private_symbol_data. */
7838 shndx = type_ptr->internal_elf_sym.st_shndx;
7839 switch (shndx)
7841 case MAP_ONESYMTAB:
7842 shndx = elf_onesymtab (abfd);
7843 break;
7844 case MAP_DYNSYMTAB:
7845 shndx = elf_dynsymtab (abfd);
7846 break;
7847 case MAP_STRTAB:
7848 shndx = elf_strtab_sec (abfd);
7849 break;
7850 case MAP_SHSTRTAB:
7851 shndx = elf_shstrtab_sec (abfd);
7852 break;
7853 case MAP_SYM_SHNDX:
7854 if (elf_symtab_shndx_list (abfd))
7855 shndx = elf_symtab_shndx_list (abfd)->ndx;
7856 break;
7857 default:
7858 shndx = SHN_ABS;
7859 break;
7862 else
7864 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
7866 if (shndx == SHN_BAD)
7868 asection *sec2;
7870 /* Writing this would be a hell of a lot easier if
7871 we had some decent documentation on bfd, and
7872 knew what to expect of the library, and what to
7873 demand of applications. For example, it
7874 appears that `objcopy' might not set the
7875 section of a symbol to be a section that is
7876 actually in the output file. */
7877 sec2 = bfd_get_section_by_name (abfd, sec->name);
7878 if (sec2 != NULL)
7879 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
7880 if (shndx == SHN_BAD)
7882 /* xgettext:c-format */
7883 _bfd_error_handler (_("\
7884 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7885 syms[idx]->name ? syms[idx]->name : "<Local sym>",
7886 sec->name);
7887 bfd_set_error (bfd_error_invalid_operation);
7888 goto error_return;
7893 sym.st_shndx = shndx;
7896 if ((flags & BSF_THREAD_LOCAL) != 0)
7897 type = STT_TLS;
7898 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
7899 type = STT_GNU_IFUNC;
7900 else if ((flags & BSF_FUNCTION) != 0)
7901 type = STT_FUNC;
7902 else if ((flags & BSF_OBJECT) != 0)
7903 type = STT_OBJECT;
7904 else if ((flags & BSF_RELC) != 0)
7905 type = STT_RELC;
7906 else if ((flags & BSF_SRELC) != 0)
7907 type = STT_SRELC;
7908 else
7909 type = STT_NOTYPE;
7911 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
7912 type = STT_TLS;
7914 /* Processor-specific types. */
7915 if (type_ptr != NULL
7916 && bed->elf_backend_get_symbol_type)
7917 type = ((*bed->elf_backend_get_symbol_type)
7918 (&type_ptr->internal_elf_sym, type));
7920 if (flags & BSF_SECTION_SYM)
7922 if (flags & BSF_GLOBAL)
7923 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7924 else
7925 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7927 else if (bfd_is_com_section (syms[idx]->section))
7929 if (type != STT_TLS)
7931 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
7932 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
7933 ? STT_COMMON : STT_OBJECT);
7934 else
7935 type = ((flags & BSF_ELF_COMMON) != 0
7936 ? STT_COMMON : STT_OBJECT);
7938 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
7940 else if (bfd_is_und_section (syms[idx]->section))
7941 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
7942 ? STB_WEAK
7943 : STB_GLOBAL),
7944 type);
7945 else if (flags & BSF_FILE)
7946 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7947 else
7949 int bind = STB_LOCAL;
7951 if (flags & BSF_LOCAL)
7952 bind = STB_LOCAL;
7953 else if (flags & BSF_GNU_UNIQUE)
7954 bind = STB_GNU_UNIQUE;
7955 else if (flags & BSF_WEAK)
7956 bind = STB_WEAK;
7957 else if (flags & BSF_GLOBAL)
7958 bind = STB_GLOBAL;
7960 sym.st_info = ELF_ST_INFO (bind, type);
7963 if (type_ptr != NULL)
7965 sym.st_other = type_ptr->internal_elf_sym.st_other;
7966 sym.st_target_internal
7967 = type_ptr->internal_elf_sym.st_target_internal;
7969 else
7971 sym.st_other = 0;
7972 sym.st_target_internal = 0;
7975 idx++;
7976 symstrtab[idx].sym = sym;
7977 symstrtab[idx].dest_index = outbound_syms_index;
7978 symstrtab[idx].destshndx_index = outbound_shndx_index;
7980 outbound_syms_index++;
7981 if (outbound_shndx != NULL)
7982 outbound_shndx_index++;
7985 /* Finalize the .strtab section. */
7986 _bfd_elf_strtab_finalize (stt);
7988 /* Swap out the .strtab section. */
7989 for (idx = 0; idx <= symcount; idx++)
7991 struct elf_sym_strtab *elfsym = &symstrtab[idx];
7992 if (elfsym->sym.st_name == (unsigned long) -1)
7993 elfsym->sym.st_name = 0;
7994 else
7995 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
7996 elfsym->sym.st_name);
7997 bed->s->swap_symbol_out (abfd, &elfsym->sym,
7998 (outbound_syms
7999 + (elfsym->dest_index
8000 * bed->s->sizeof_sym)),
8001 (outbound_shndx
8002 + (elfsym->destshndx_index
8003 * sizeof (Elf_External_Sym_Shndx))));
8005 free (symstrtab);
8007 *sttp = stt;
8008 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8009 symstrtab_hdr->sh_type = SHT_STRTAB;
8010 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8011 symstrtab_hdr->sh_addr = 0;
8012 symstrtab_hdr->sh_entsize = 0;
8013 symstrtab_hdr->sh_link = 0;
8014 symstrtab_hdr->sh_info = 0;
8015 symstrtab_hdr->sh_addralign = 1;
8017 return TRUE;
8020 /* Return the number of bytes required to hold the symtab vector.
8022 Note that we base it on the count plus 1, since we will null terminate
8023 the vector allocated based on this size. However, the ELF symbol table
8024 always has a dummy entry as symbol #0, so it ends up even. */
8026 long
8027 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8029 long symcount;
8030 long symtab_size;
8031 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8033 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8034 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8035 if (symcount > 0)
8036 symtab_size -= sizeof (asymbol *);
8038 return symtab_size;
8041 long
8042 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8044 long symcount;
8045 long symtab_size;
8046 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8048 if (elf_dynsymtab (abfd) == 0)
8050 bfd_set_error (bfd_error_invalid_operation);
8051 return -1;
8054 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8055 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8056 if (symcount > 0)
8057 symtab_size -= sizeof (asymbol *);
8059 return symtab_size;
8062 long
8063 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8064 sec_ptr asect)
8066 return (asect->reloc_count + 1) * sizeof (arelent *);
8069 /* Canonicalize the relocs. */
8071 long
8072 _bfd_elf_canonicalize_reloc (bfd *abfd,
8073 sec_ptr section,
8074 arelent **relptr,
8075 asymbol **symbols)
8077 arelent *tblptr;
8078 unsigned int i;
8079 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8081 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
8082 return -1;
8084 tblptr = section->relocation;
8085 for (i = 0; i < section->reloc_count; i++)
8086 *relptr++ = tblptr++;
8088 *relptr = NULL;
8090 return section->reloc_count;
8093 long
8094 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8096 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8097 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
8099 if (symcount >= 0)
8100 bfd_get_symcount (abfd) = symcount;
8101 return symcount;
8104 long
8105 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8106 asymbol **allocation)
8108 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8109 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
8111 if (symcount >= 0)
8112 bfd_get_dynamic_symcount (abfd) = symcount;
8113 return symcount;
8116 /* Return the size required for the dynamic reloc entries. Any loadable
8117 section that was actually installed in the BFD, and has type SHT_REL
8118 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8119 dynamic reloc section. */
8121 long
8122 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8124 long ret;
8125 asection *s;
8127 if (elf_dynsymtab (abfd) == 0)
8129 bfd_set_error (bfd_error_invalid_operation);
8130 return -1;
8133 ret = sizeof (arelent *);
8134 for (s = abfd->sections; s != NULL; s = s->next)
8135 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8136 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8137 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8138 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
8139 * sizeof (arelent *));
8141 return ret;
8144 /* Canonicalize the dynamic relocation entries. Note that we return the
8145 dynamic relocations as a single block, although they are actually
8146 associated with particular sections; the interface, which was
8147 designed for SunOS style shared libraries, expects that there is only
8148 one set of dynamic relocs. Any loadable section that was actually
8149 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8150 dynamic symbol table, is considered to be a dynamic reloc section. */
8152 long
8153 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8154 arelent **storage,
8155 asymbol **syms)
8157 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8158 asection *s;
8159 long ret;
8161 if (elf_dynsymtab (abfd) == 0)
8163 bfd_set_error (bfd_error_invalid_operation);
8164 return -1;
8167 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8168 ret = 0;
8169 for (s = abfd->sections; s != NULL; s = s->next)
8171 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8172 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8173 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8175 arelent *p;
8176 long count, i;
8178 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
8179 return -1;
8180 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8181 p = s->relocation;
8182 for (i = 0; i < count; i++)
8183 *storage++ = p++;
8184 ret += count;
8188 *storage = NULL;
8190 return ret;
8193 /* Read in the version information. */
8195 bfd_boolean
8196 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
8198 bfd_byte *contents = NULL;
8199 unsigned int freeidx = 0;
8201 if (elf_dynverref (abfd) != 0)
8203 Elf_Internal_Shdr *hdr;
8204 Elf_External_Verneed *everneed;
8205 Elf_Internal_Verneed *iverneed;
8206 unsigned int i;
8207 bfd_byte *contents_end;
8209 hdr = &elf_tdata (abfd)->dynverref_hdr;
8211 if (hdr->sh_info == 0
8212 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8214 error_return_bad_verref:
8215 _bfd_error_handler
8216 (_("%B: .gnu.version_r invalid entry"), abfd);
8217 bfd_set_error (bfd_error_bad_value);
8218 error_return_verref:
8219 elf_tdata (abfd)->verref = NULL;
8220 elf_tdata (abfd)->cverrefs = 0;
8221 goto error_return;
8224 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8225 if (contents == NULL)
8226 goto error_return_verref;
8228 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8229 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8230 goto error_return_verref;
8232 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
8233 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
8235 if (elf_tdata (abfd)->verref == NULL)
8236 goto error_return_verref;
8238 BFD_ASSERT (sizeof (Elf_External_Verneed)
8239 == sizeof (Elf_External_Vernaux));
8240 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8241 everneed = (Elf_External_Verneed *) contents;
8242 iverneed = elf_tdata (abfd)->verref;
8243 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8245 Elf_External_Vernaux *evernaux;
8246 Elf_Internal_Vernaux *ivernaux;
8247 unsigned int j;
8249 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8251 iverneed->vn_bfd = abfd;
8253 iverneed->vn_filename =
8254 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8255 iverneed->vn_file);
8256 if (iverneed->vn_filename == NULL)
8257 goto error_return_bad_verref;
8259 if (iverneed->vn_cnt == 0)
8260 iverneed->vn_auxptr = NULL;
8261 else
8263 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8264 bfd_alloc2 (abfd, iverneed->vn_cnt,
8265 sizeof (Elf_Internal_Vernaux));
8266 if (iverneed->vn_auxptr == NULL)
8267 goto error_return_verref;
8270 if (iverneed->vn_aux
8271 > (size_t) (contents_end - (bfd_byte *) everneed))
8272 goto error_return_bad_verref;
8274 evernaux = ((Elf_External_Vernaux *)
8275 ((bfd_byte *) everneed + iverneed->vn_aux));
8276 ivernaux = iverneed->vn_auxptr;
8277 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8279 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8281 ivernaux->vna_nodename =
8282 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8283 ivernaux->vna_name);
8284 if (ivernaux->vna_nodename == NULL)
8285 goto error_return_bad_verref;
8287 if (ivernaux->vna_other > freeidx)
8288 freeidx = ivernaux->vna_other;
8290 ivernaux->vna_nextptr = NULL;
8291 if (ivernaux->vna_next == 0)
8293 iverneed->vn_cnt = j + 1;
8294 break;
8296 if (j + 1 < iverneed->vn_cnt)
8297 ivernaux->vna_nextptr = ivernaux + 1;
8299 if (ivernaux->vna_next
8300 > (size_t) (contents_end - (bfd_byte *) evernaux))
8301 goto error_return_bad_verref;
8303 evernaux = ((Elf_External_Vernaux *)
8304 ((bfd_byte *) evernaux + ivernaux->vna_next));
8307 iverneed->vn_nextref = NULL;
8308 if (iverneed->vn_next == 0)
8309 break;
8310 if (i + 1 < hdr->sh_info)
8311 iverneed->vn_nextref = iverneed + 1;
8313 if (iverneed->vn_next
8314 > (size_t) (contents_end - (bfd_byte *) everneed))
8315 goto error_return_bad_verref;
8317 everneed = ((Elf_External_Verneed *)
8318 ((bfd_byte *) everneed + iverneed->vn_next));
8320 elf_tdata (abfd)->cverrefs = i;
8322 free (contents);
8323 contents = NULL;
8326 if (elf_dynverdef (abfd) != 0)
8328 Elf_Internal_Shdr *hdr;
8329 Elf_External_Verdef *everdef;
8330 Elf_Internal_Verdef *iverdef;
8331 Elf_Internal_Verdef *iverdefarr;
8332 Elf_Internal_Verdef iverdefmem;
8333 unsigned int i;
8334 unsigned int maxidx;
8335 bfd_byte *contents_end_def, *contents_end_aux;
8337 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8339 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8341 error_return_bad_verdef:
8342 _bfd_error_handler
8343 (_("%B: .gnu.version_d invalid entry"), abfd);
8344 bfd_set_error (bfd_error_bad_value);
8345 error_return_verdef:
8346 elf_tdata (abfd)->verdef = NULL;
8347 elf_tdata (abfd)->cverdefs = 0;
8348 goto error_return;
8351 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8352 if (contents == NULL)
8353 goto error_return_verdef;
8354 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8355 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
8356 goto error_return_verdef;
8358 BFD_ASSERT (sizeof (Elf_External_Verdef)
8359 >= sizeof (Elf_External_Verdaux));
8360 contents_end_def = contents + hdr->sh_size
8361 - sizeof (Elf_External_Verdef);
8362 contents_end_aux = contents + hdr->sh_size
8363 - sizeof (Elf_External_Verdaux);
8365 /* We know the number of entries in the section but not the maximum
8366 index. Therefore we have to run through all entries and find
8367 the maximum. */
8368 everdef = (Elf_External_Verdef *) contents;
8369 maxidx = 0;
8370 for (i = 0; i < hdr->sh_info; ++i)
8372 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8374 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8375 goto error_return_bad_verdef;
8376 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8377 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
8379 if (iverdefmem.vd_next == 0)
8380 break;
8382 if (iverdefmem.vd_next
8383 > (size_t) (contents_end_def - (bfd_byte *) everdef))
8384 goto error_return_bad_verdef;
8386 everdef = ((Elf_External_Verdef *)
8387 ((bfd_byte *) everdef + iverdefmem.vd_next));
8390 if (default_imported_symver)
8392 if (freeidx > maxidx)
8393 maxidx = ++freeidx;
8394 else
8395 freeidx = ++maxidx;
8398 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8399 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
8400 if (elf_tdata (abfd)->verdef == NULL)
8401 goto error_return_verdef;
8403 elf_tdata (abfd)->cverdefs = maxidx;
8405 everdef = (Elf_External_Verdef *) contents;
8406 iverdefarr = elf_tdata (abfd)->verdef;
8407 for (i = 0; i < hdr->sh_info; i++)
8409 Elf_External_Verdaux *everdaux;
8410 Elf_Internal_Verdaux *iverdaux;
8411 unsigned int j;
8413 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8415 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
8416 goto error_return_bad_verdef;
8418 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
8419 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
8421 iverdef->vd_bfd = abfd;
8423 if (iverdef->vd_cnt == 0)
8424 iverdef->vd_auxptr = NULL;
8425 else
8427 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
8428 bfd_alloc2 (abfd, iverdef->vd_cnt,
8429 sizeof (Elf_Internal_Verdaux));
8430 if (iverdef->vd_auxptr == NULL)
8431 goto error_return_verdef;
8434 if (iverdef->vd_aux
8435 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
8436 goto error_return_bad_verdef;
8438 everdaux = ((Elf_External_Verdaux *)
8439 ((bfd_byte *) everdef + iverdef->vd_aux));
8440 iverdaux = iverdef->vd_auxptr;
8441 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8443 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8445 iverdaux->vda_nodename =
8446 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8447 iverdaux->vda_name);
8448 if (iverdaux->vda_nodename == NULL)
8449 goto error_return_bad_verdef;
8451 iverdaux->vda_nextptr = NULL;
8452 if (iverdaux->vda_next == 0)
8454 iverdef->vd_cnt = j + 1;
8455 break;
8457 if (j + 1 < iverdef->vd_cnt)
8458 iverdaux->vda_nextptr = iverdaux + 1;
8460 if (iverdaux->vda_next
8461 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
8462 goto error_return_bad_verdef;
8464 everdaux = ((Elf_External_Verdaux *)
8465 ((bfd_byte *) everdaux + iverdaux->vda_next));
8468 iverdef->vd_nodename = NULL;
8469 if (iverdef->vd_cnt)
8470 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
8472 iverdef->vd_nextdef = NULL;
8473 if (iverdef->vd_next == 0)
8474 break;
8475 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
8476 iverdef->vd_nextdef = iverdef + 1;
8478 everdef = ((Elf_External_Verdef *)
8479 ((bfd_byte *) everdef + iverdef->vd_next));
8482 free (contents);
8483 contents = NULL;
8485 else if (default_imported_symver)
8487 if (freeidx < 3)
8488 freeidx = 3;
8489 else
8490 freeidx++;
8492 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8493 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
8494 if (elf_tdata (abfd)->verdef == NULL)
8495 goto error_return;
8497 elf_tdata (abfd)->cverdefs = freeidx;
8500 /* Create a default version based on the soname. */
8501 if (default_imported_symver)
8503 Elf_Internal_Verdef *iverdef;
8504 Elf_Internal_Verdaux *iverdaux;
8506 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
8508 iverdef->vd_version = VER_DEF_CURRENT;
8509 iverdef->vd_flags = 0;
8510 iverdef->vd_ndx = freeidx;
8511 iverdef->vd_cnt = 1;
8513 iverdef->vd_bfd = abfd;
8515 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8516 if (iverdef->vd_nodename == NULL)
8517 goto error_return_verdef;
8518 iverdef->vd_nextdef = NULL;
8519 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8520 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
8521 if (iverdef->vd_auxptr == NULL)
8522 goto error_return_verdef;
8524 iverdaux = iverdef->vd_auxptr;
8525 iverdaux->vda_nodename = iverdef->vd_nodename;
8528 return TRUE;
8530 error_return:
8531 if (contents != NULL)
8532 free (contents);
8533 return FALSE;
8536 asymbol *
8537 _bfd_elf_make_empty_symbol (bfd *abfd)
8539 elf_symbol_type *newsym;
8541 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
8542 if (!newsym)
8543 return NULL;
8544 newsym->symbol.the_bfd = abfd;
8545 return &newsym->symbol;
8548 void
8549 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8550 asymbol *symbol,
8551 symbol_info *ret)
8553 bfd_symbol_info (symbol, ret);
8556 /* Return whether a symbol name implies a local symbol. Most targets
8557 use this function for the is_local_label_name entry point, but some
8558 override it. */
8560 bfd_boolean
8561 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8562 const char *name)
8564 /* Normal local symbols start with ``.L''. */
8565 if (name[0] == '.' && name[1] == 'L')
8566 return TRUE;
8568 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8569 DWARF debugging symbols starting with ``..''. */
8570 if (name[0] == '.' && name[1] == '.')
8571 return TRUE;
8573 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8574 emitting DWARF debugging output. I suspect this is actually a
8575 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8576 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8577 underscore to be emitted on some ELF targets). For ease of use,
8578 we treat such symbols as local. */
8579 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
8580 return TRUE;
8582 /* Treat assembler generated fake symbols, dollar local labels and
8583 forward-backward labels (aka local labels) as locals.
8584 These labels have the form:
8586 L0^A.* (fake symbols)
8588 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8590 Versions which start with .L will have already been matched above,
8591 so we only need to match the rest. */
8592 if (name[0] == 'L' && ISDIGIT (name[1]))
8594 bfd_boolean ret = FALSE;
8595 const char * p;
8596 char c;
8598 for (p = name + 2; (c = *p); p++)
8600 if (c == 1 || c == 2)
8602 if (c == 1 && p == name + 2)
8603 /* A fake symbol. */
8604 return TRUE;
8606 /* FIXME: We are being paranoid here and treating symbols like
8607 L0^Bfoo as if there were non-local, on the grounds that the
8608 assembler will never generate them. But can any symbol
8609 containing an ASCII value in the range 1-31 ever be anything
8610 other than some kind of local ? */
8611 ret = TRUE;
8614 if (! ISDIGIT (c))
8616 ret = FALSE;
8617 break;
8620 return ret;
8623 return FALSE;
8626 alent *
8627 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8628 asymbol *symbol ATTRIBUTE_UNUSED)
8630 abort ();
8631 return NULL;
8634 bfd_boolean
8635 _bfd_elf_set_arch_mach (bfd *abfd,
8636 enum bfd_architecture arch,
8637 unsigned long machine)
8639 /* If this isn't the right architecture for this backend, and this
8640 isn't the generic backend, fail. */
8641 if (arch != get_elf_backend_data (abfd)->arch
8642 && arch != bfd_arch_unknown
8643 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
8644 return FALSE;
8646 return bfd_default_set_arch_mach (abfd, arch, machine);
8649 /* Find the nearest line to a particular section and offset,
8650 for error reporting. */
8652 bfd_boolean
8653 _bfd_elf_find_nearest_line (bfd *abfd,
8654 asymbol **symbols,
8655 asection *section,
8656 bfd_vma offset,
8657 const char **filename_ptr,
8658 const char **functionname_ptr,
8659 unsigned int *line_ptr,
8660 unsigned int *discriminator_ptr)
8662 bfd_boolean found;
8664 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
8665 filename_ptr, functionname_ptr,
8666 line_ptr, discriminator_ptr,
8667 dwarf_debug_sections, 0,
8668 &elf_tdata (abfd)->dwarf2_find_line_info)
8669 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8670 filename_ptr, functionname_ptr,
8671 line_ptr))
8673 if (!*functionname_ptr)
8674 _bfd_elf_find_function (abfd, symbols, section, offset,
8675 *filename_ptr ? NULL : filename_ptr,
8676 functionname_ptr);
8677 return TRUE;
8680 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
8681 &found, filename_ptr,
8682 functionname_ptr, line_ptr,
8683 &elf_tdata (abfd)->line_info))
8684 return FALSE;
8685 if (found && (*functionname_ptr || *line_ptr))
8686 return TRUE;
8688 if (symbols == NULL)
8689 return FALSE;
8691 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
8692 filename_ptr, functionname_ptr))
8693 return FALSE;
8695 *line_ptr = 0;
8696 return TRUE;
8699 /* Find the line for a symbol. */
8701 bfd_boolean
8702 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
8703 const char **filename_ptr, unsigned int *line_ptr)
8705 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
8706 filename_ptr, NULL, line_ptr, NULL,
8707 dwarf_debug_sections, 0,
8708 &elf_tdata (abfd)->dwarf2_find_line_info);
8711 /* After a call to bfd_find_nearest_line, successive calls to
8712 bfd_find_inliner_info can be used to get source information about
8713 each level of function inlining that terminated at the address
8714 passed to bfd_find_nearest_line. Currently this is only supported
8715 for DWARF2 with appropriate DWARF3 extensions. */
8717 bfd_boolean
8718 _bfd_elf_find_inliner_info (bfd *abfd,
8719 const char **filename_ptr,
8720 const char **functionname_ptr,
8721 unsigned int *line_ptr)
8723 bfd_boolean found;
8724 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
8725 functionname_ptr, line_ptr,
8726 & elf_tdata (abfd)->dwarf2_find_line_info);
8727 return found;
8731 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
8733 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8734 int ret = bed->s->sizeof_ehdr;
8736 if (!bfd_link_relocatable (info))
8738 bfd_size_type phdr_size = elf_program_header_size (abfd);
8740 if (phdr_size == (bfd_size_type) -1)
8742 struct elf_segment_map *m;
8744 phdr_size = 0;
8745 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
8746 phdr_size += bed->s->sizeof_phdr;
8748 if (phdr_size == 0)
8749 phdr_size = get_program_header_size (abfd, info);
8752 elf_program_header_size (abfd) = phdr_size;
8753 ret += phdr_size;
8756 return ret;
8759 bfd_boolean
8760 _bfd_elf_set_section_contents (bfd *abfd,
8761 sec_ptr section,
8762 const void *location,
8763 file_ptr offset,
8764 bfd_size_type count)
8766 Elf_Internal_Shdr *hdr;
8767 file_ptr pos;
8769 if (! abfd->output_has_begun
8770 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
8771 return FALSE;
8773 if (!count)
8774 return TRUE;
8776 hdr = &elf_section_data (section)->this_hdr;
8777 if (hdr->sh_offset == (file_ptr) -1)
8779 /* We must compress this section. Write output to the buffer. */
8780 unsigned char *contents = hdr->contents;
8781 if ((offset + count) > hdr->sh_size
8782 || (section->flags & SEC_ELF_COMPRESS) == 0
8783 || contents == NULL)
8784 abort ();
8785 memcpy (contents + offset, location, count);
8786 return TRUE;
8788 pos = hdr->sh_offset + offset;
8789 if (bfd_seek (abfd, pos, SEEK_SET) != 0
8790 || bfd_bwrite (location, count, abfd) != count)
8791 return FALSE;
8793 return TRUE;
8796 void
8797 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
8798 arelent *cache_ptr ATTRIBUTE_UNUSED,
8799 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
8801 abort ();
8804 /* Try to convert a non-ELF reloc into an ELF one. */
8806 bfd_boolean
8807 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
8809 /* Check whether we really have an ELF howto. */
8811 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
8813 bfd_reloc_code_real_type code;
8814 reloc_howto_type *howto;
8816 /* Alien reloc: Try to determine its type to replace it with an
8817 equivalent ELF reloc. */
8819 if (areloc->howto->pc_relative)
8821 switch (areloc->howto->bitsize)
8823 case 8:
8824 code = BFD_RELOC_8_PCREL;
8825 break;
8826 case 12:
8827 code = BFD_RELOC_12_PCREL;
8828 break;
8829 case 16:
8830 code = BFD_RELOC_16_PCREL;
8831 break;
8832 case 24:
8833 code = BFD_RELOC_24_PCREL;
8834 break;
8835 case 32:
8836 code = BFD_RELOC_32_PCREL;
8837 break;
8838 case 64:
8839 code = BFD_RELOC_64_PCREL;
8840 break;
8841 default:
8842 goto fail;
8845 howto = bfd_reloc_type_lookup (abfd, code);
8847 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
8849 if (howto->pcrel_offset)
8850 areloc->addend += areloc->address;
8851 else
8852 areloc->addend -= areloc->address; /* addend is unsigned!! */
8855 else
8857 switch (areloc->howto->bitsize)
8859 case 8:
8860 code = BFD_RELOC_8;
8861 break;
8862 case 14:
8863 code = BFD_RELOC_14;
8864 break;
8865 case 16:
8866 code = BFD_RELOC_16;
8867 break;
8868 case 26:
8869 code = BFD_RELOC_26;
8870 break;
8871 case 32:
8872 code = BFD_RELOC_32;
8873 break;
8874 case 64:
8875 code = BFD_RELOC_64;
8876 break;
8877 default:
8878 goto fail;
8881 howto = bfd_reloc_type_lookup (abfd, code);
8884 if (howto)
8885 areloc->howto = howto;
8886 else
8887 goto fail;
8890 return TRUE;
8892 fail:
8893 _bfd_error_handler
8894 /* xgettext:c-format */
8895 (_("%B: unsupported relocation type %s"),
8896 abfd, areloc->howto->name);
8897 bfd_set_error (bfd_error_bad_value);
8898 return FALSE;
8901 bfd_boolean
8902 _bfd_elf_close_and_cleanup (bfd *abfd)
8904 struct elf_obj_tdata *tdata = elf_tdata (abfd);
8905 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
8907 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
8908 _bfd_elf_strtab_free (elf_shstrtab (abfd));
8909 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
8912 return _bfd_generic_close_and_cleanup (abfd);
8915 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8916 in the relocation's offset. Thus we cannot allow any sort of sanity
8917 range-checking to interfere. There is nothing else to do in processing
8918 this reloc. */
8920 bfd_reloc_status_type
8921 _bfd_elf_rel_vtable_reloc_fn
8922 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
8923 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
8924 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
8925 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
8927 return bfd_reloc_ok;
8930 /* Elf core file support. Much of this only works on native
8931 toolchains, since we rely on knowing the
8932 machine-dependent procfs structure in order to pick
8933 out details about the corefile. */
8935 #ifdef HAVE_SYS_PROCFS_H
8936 /* Needed for new procfs interface on sparc-solaris. */
8937 # define _STRUCTURED_PROC 1
8938 # include <sys/procfs.h>
8939 #endif
8941 /* Return a PID that identifies a "thread" for threaded cores, or the
8942 PID of the main process for non-threaded cores. */
8944 static int
8945 elfcore_make_pid (bfd *abfd)
8947 int pid;
8949 pid = elf_tdata (abfd)->core->lwpid;
8950 if (pid == 0)
8951 pid = elf_tdata (abfd)->core->pid;
8953 return pid;
8956 /* If there isn't a section called NAME, make one, using
8957 data from SECT. Note, this function will generate a
8958 reference to NAME, so you shouldn't deallocate or
8959 overwrite it. */
8961 static bfd_boolean
8962 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
8964 asection *sect2;
8966 if (bfd_get_section_by_name (abfd, name) != NULL)
8967 return TRUE;
8969 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
8970 if (sect2 == NULL)
8971 return FALSE;
8973 sect2->size = sect->size;
8974 sect2->filepos = sect->filepos;
8975 sect2->alignment_power = sect->alignment_power;
8976 return TRUE;
8979 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8980 actually creates up to two pseudosections:
8981 - For the single-threaded case, a section named NAME, unless
8982 such a section already exists.
8983 - For the multi-threaded case, a section named "NAME/PID", where
8984 PID is elfcore_make_pid (abfd).
8985 Both pseudosections have identical contents. */
8986 bfd_boolean
8987 _bfd_elfcore_make_pseudosection (bfd *abfd,
8988 char *name,
8989 size_t size,
8990 ufile_ptr filepos)
8992 char buf[100];
8993 char *threaded_name;
8994 size_t len;
8995 asection *sect;
8997 /* Build the section name. */
8999 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9000 len = strlen (buf) + 1;
9001 threaded_name = (char *) bfd_alloc (abfd, len);
9002 if (threaded_name == NULL)
9003 return FALSE;
9004 memcpy (threaded_name, buf, len);
9006 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9007 SEC_HAS_CONTENTS);
9008 if (sect == NULL)
9009 return FALSE;
9010 sect->size = size;
9011 sect->filepos = filepos;
9012 sect->alignment_power = 2;
9014 return elfcore_maybe_make_sect (abfd, name, sect);
9017 /* prstatus_t exists on:
9018 solaris 2.5+
9019 linux 2.[01] + glibc
9020 unixware 4.2
9023 #if defined (HAVE_PRSTATUS_T)
9025 static bfd_boolean
9026 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9028 size_t size;
9029 int offset;
9031 if (note->descsz == sizeof (prstatus_t))
9033 prstatus_t prstat;
9035 size = sizeof (prstat.pr_reg);
9036 offset = offsetof (prstatus_t, pr_reg);
9037 memcpy (&prstat, note->descdata, sizeof (prstat));
9039 /* Do not overwrite the core signal if it
9040 has already been set by another thread. */
9041 if (elf_tdata (abfd)->core->signal == 0)
9042 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9043 if (elf_tdata (abfd)->core->pid == 0)
9044 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9046 /* pr_who exists on:
9047 solaris 2.5+
9048 unixware 4.2
9049 pr_who doesn't exist on:
9050 linux 2.[01]
9052 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9053 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9054 #else
9055 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9056 #endif
9058 #if defined (HAVE_PRSTATUS32_T)
9059 else if (note->descsz == sizeof (prstatus32_t))
9061 /* 64-bit host, 32-bit corefile */
9062 prstatus32_t prstat;
9064 size = sizeof (prstat.pr_reg);
9065 offset = offsetof (prstatus32_t, pr_reg);
9066 memcpy (&prstat, note->descdata, sizeof (prstat));
9068 /* Do not overwrite the core signal if it
9069 has already been set by another thread. */
9070 if (elf_tdata (abfd)->core->signal == 0)
9071 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9072 if (elf_tdata (abfd)->core->pid == 0)
9073 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9075 /* pr_who exists on:
9076 solaris 2.5+
9077 unixware 4.2
9078 pr_who doesn't exist on:
9079 linux 2.[01]
9081 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9082 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9083 #else
9084 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9085 #endif
9087 #endif /* HAVE_PRSTATUS32_T */
9088 else
9090 /* Fail - we don't know how to handle any other
9091 note size (ie. data object type). */
9092 return TRUE;
9095 /* Make a ".reg/999" section and a ".reg" section. */
9096 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9097 size, note->descpos + offset);
9099 #endif /* defined (HAVE_PRSTATUS_T) */
9101 /* Create a pseudosection containing the exact contents of NOTE. */
9102 static bfd_boolean
9103 elfcore_make_note_pseudosection (bfd *abfd,
9104 char *name,
9105 Elf_Internal_Note *note)
9107 return _bfd_elfcore_make_pseudosection (abfd, name,
9108 note->descsz, note->descpos);
9111 /* There isn't a consistent prfpregset_t across platforms,
9112 but it doesn't matter, because we don't have to pick this
9113 data structure apart. */
9115 static bfd_boolean
9116 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9118 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9121 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9122 type of NT_PRXFPREG. Just include the whole note's contents
9123 literally. */
9125 static bfd_boolean
9126 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9128 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9131 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9132 with a note type of NT_X86_XSTATE. Just include the whole note's
9133 contents literally. */
9135 static bfd_boolean
9136 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9138 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9141 static bfd_boolean
9142 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9144 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9147 static bfd_boolean
9148 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9150 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9153 static bfd_boolean
9154 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9156 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9159 static bfd_boolean
9160 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9162 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9165 static bfd_boolean
9166 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9168 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9171 static bfd_boolean
9172 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9174 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9177 static bfd_boolean
9178 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9180 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9183 static bfd_boolean
9184 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9186 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9189 static bfd_boolean
9190 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9192 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9195 static bfd_boolean
9196 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9198 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9201 static bfd_boolean
9202 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9204 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9207 static bfd_boolean
9208 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9210 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9213 static bfd_boolean
9214 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9216 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9219 static bfd_boolean
9220 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9222 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9225 static bfd_boolean
9226 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9228 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9231 static bfd_boolean
9232 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9234 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9237 static bfd_boolean
9238 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9240 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9243 static bfd_boolean
9244 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9246 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9249 static bfd_boolean
9250 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9252 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9255 #if defined (HAVE_PRPSINFO_T)
9256 typedef prpsinfo_t elfcore_psinfo_t;
9257 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9258 typedef prpsinfo32_t elfcore_psinfo32_t;
9259 #endif
9260 #endif
9262 #if defined (HAVE_PSINFO_T)
9263 typedef psinfo_t elfcore_psinfo_t;
9264 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9265 typedef psinfo32_t elfcore_psinfo32_t;
9266 #endif
9267 #endif
9269 /* return a malloc'ed copy of a string at START which is at
9270 most MAX bytes long, possibly without a terminating '\0'.
9271 the copy will always have a terminating '\0'. */
9273 char *
9274 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
9276 char *dups;
9277 char *end = (char *) memchr (start, '\0', max);
9278 size_t len;
9280 if (end == NULL)
9281 len = max;
9282 else
9283 len = end - start;
9285 dups = (char *) bfd_alloc (abfd, len + 1);
9286 if (dups == NULL)
9287 return NULL;
9289 memcpy (dups, start, len);
9290 dups[len] = '\0';
9292 return dups;
9295 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9296 static bfd_boolean
9297 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
9299 if (note->descsz == sizeof (elfcore_psinfo_t))
9301 elfcore_psinfo_t psinfo;
9303 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9305 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9306 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9307 #endif
9308 elf_tdata (abfd)->core->program
9309 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9310 sizeof (psinfo.pr_fname));
9312 elf_tdata (abfd)->core->command
9313 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9314 sizeof (psinfo.pr_psargs));
9316 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9317 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9319 /* 64-bit host, 32-bit corefile */
9320 elfcore_psinfo32_t psinfo;
9322 memcpy (&psinfo, note->descdata, sizeof (psinfo));
9324 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9325 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
9326 #endif
9327 elf_tdata (abfd)->core->program
9328 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9329 sizeof (psinfo.pr_fname));
9331 elf_tdata (abfd)->core->command
9332 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9333 sizeof (psinfo.pr_psargs));
9335 #endif
9337 else
9339 /* Fail - we don't know how to handle any other
9340 note size (ie. data object type). */
9341 return TRUE;
9344 /* Note that for some reason, a spurious space is tacked
9345 onto the end of the args in some (at least one anyway)
9346 implementations, so strip it off if it exists. */
9349 char *command = elf_tdata (abfd)->core->command;
9350 int n = strlen (command);
9352 if (0 < n && command[n - 1] == ' ')
9353 command[n - 1] = '\0';
9356 return TRUE;
9358 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9360 #if defined (HAVE_PSTATUS_T)
9361 static bfd_boolean
9362 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
9364 if (note->descsz == sizeof (pstatus_t)
9365 #if defined (HAVE_PXSTATUS_T)
9366 || note->descsz == sizeof (pxstatus_t)
9367 #endif
9370 pstatus_t pstat;
9372 memcpy (&pstat, note->descdata, sizeof (pstat));
9374 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9376 #if defined (HAVE_PSTATUS32_T)
9377 else if (note->descsz == sizeof (pstatus32_t))
9379 /* 64-bit host, 32-bit corefile */
9380 pstatus32_t pstat;
9382 memcpy (&pstat, note->descdata, sizeof (pstat));
9384 elf_tdata (abfd)->core->pid = pstat.pr_pid;
9386 #endif
9387 /* Could grab some more details from the "representative"
9388 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9389 NT_LWPSTATUS note, presumably. */
9391 return TRUE;
9393 #endif /* defined (HAVE_PSTATUS_T) */
9395 #if defined (HAVE_LWPSTATUS_T)
9396 static bfd_boolean
9397 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
9399 lwpstatus_t lwpstat;
9400 char buf[100];
9401 char *name;
9402 size_t len;
9403 asection *sect;
9405 if (note->descsz != sizeof (lwpstat)
9406 #if defined (HAVE_LWPXSTATUS_T)
9407 && note->descsz != sizeof (lwpxstatus_t)
9408 #endif
9410 return TRUE;
9412 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9414 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
9415 /* Do not overwrite the core signal if it has already been set by
9416 another thread. */
9417 if (elf_tdata (abfd)->core->signal == 0)
9418 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
9420 /* Make a ".reg/999" section. */
9422 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
9423 len = strlen (buf) + 1;
9424 name = bfd_alloc (abfd, len);
9425 if (name == NULL)
9426 return FALSE;
9427 memcpy (name, buf, len);
9429 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9430 if (sect == NULL)
9431 return FALSE;
9433 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9434 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
9435 sect->filepos = note->descpos
9436 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9437 #endif
9439 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9440 sect->size = sizeof (lwpstat.pr_reg);
9441 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9442 #endif
9444 sect->alignment_power = 2;
9446 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
9447 return FALSE;
9449 /* Make a ".reg2/999" section */
9451 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
9452 len = strlen (buf) + 1;
9453 name = bfd_alloc (abfd, len);
9454 if (name == NULL)
9455 return FALSE;
9456 memcpy (name, buf, len);
9458 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9459 if (sect == NULL)
9460 return FALSE;
9462 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9463 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
9464 sect->filepos = note->descpos
9465 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9466 #endif
9468 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9469 sect->size = sizeof (lwpstat.pr_fpreg);
9470 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9471 #endif
9473 sect->alignment_power = 2;
9475 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
9477 #endif /* defined (HAVE_LWPSTATUS_T) */
9479 static bfd_boolean
9480 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
9482 char buf[30];
9483 char *name;
9484 size_t len;
9485 asection *sect;
9486 int type;
9487 int is_active_thread;
9488 bfd_vma base_addr;
9490 if (note->descsz < 728)
9491 return TRUE;
9493 if (! CONST_STRNEQ (note->namedata, "win32"))
9494 return TRUE;
9496 type = bfd_get_32 (abfd, note->descdata);
9498 switch (type)
9500 case 1 /* NOTE_INFO_PROCESS */:
9501 /* FIXME: need to add ->core->command. */
9502 /* process_info.pid */
9503 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
9504 /* process_info.signal */
9505 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
9506 break;
9508 case 2 /* NOTE_INFO_THREAD */:
9509 /* Make a ".reg/999" section. */
9510 /* thread_info.tid */
9511 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
9513 len = strlen (buf) + 1;
9514 name = (char *) bfd_alloc (abfd, len);
9515 if (name == NULL)
9516 return FALSE;
9518 memcpy (name, buf, len);
9520 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9521 if (sect == NULL)
9522 return FALSE;
9524 /* sizeof (thread_info.thread_context) */
9525 sect->size = 716;
9526 /* offsetof (thread_info.thread_context) */
9527 sect->filepos = note->descpos + 12;
9528 sect->alignment_power = 2;
9530 /* thread_info.is_active_thread */
9531 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9533 if (is_active_thread)
9534 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
9535 return FALSE;
9536 break;
9538 case 3 /* NOTE_INFO_MODULE */:
9539 /* Make a ".module/xxxxxxxx" section. */
9540 /* module_info.base_address */
9541 base_addr = bfd_get_32 (abfd, note->descdata + 4);
9542 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
9544 len = strlen (buf) + 1;
9545 name = (char *) bfd_alloc (abfd, len);
9546 if (name == NULL)
9547 return FALSE;
9549 memcpy (name, buf, len);
9551 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9553 if (sect == NULL)
9554 return FALSE;
9556 sect->size = note->descsz;
9557 sect->filepos = note->descpos;
9558 sect->alignment_power = 2;
9559 break;
9561 default:
9562 return TRUE;
9565 return TRUE;
9568 static bfd_boolean
9569 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
9571 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9573 switch (note->type)
9575 default:
9576 return TRUE;
9578 case NT_PRSTATUS:
9579 if (bed->elf_backend_grok_prstatus)
9580 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
9581 return TRUE;
9582 #if defined (HAVE_PRSTATUS_T)
9583 return elfcore_grok_prstatus (abfd, note);
9584 #else
9585 return TRUE;
9586 #endif
9588 #if defined (HAVE_PSTATUS_T)
9589 case NT_PSTATUS:
9590 return elfcore_grok_pstatus (abfd, note);
9591 #endif
9593 #if defined (HAVE_LWPSTATUS_T)
9594 case NT_LWPSTATUS:
9595 return elfcore_grok_lwpstatus (abfd, note);
9596 #endif
9598 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
9599 return elfcore_grok_prfpreg (abfd, note);
9601 case NT_WIN32PSTATUS:
9602 return elfcore_grok_win32pstatus (abfd, note);
9604 case NT_PRXFPREG: /* Linux SSE extension */
9605 if (note->namesz == 6
9606 && strcmp (note->namedata, "LINUX") == 0)
9607 return elfcore_grok_prxfpreg (abfd, note);
9608 else
9609 return TRUE;
9611 case NT_X86_XSTATE: /* Linux XSAVE extension */
9612 if (note->namesz == 6
9613 && strcmp (note->namedata, "LINUX") == 0)
9614 return elfcore_grok_xstatereg (abfd, note);
9615 else
9616 return TRUE;
9618 case NT_PPC_VMX:
9619 if (note->namesz == 6
9620 && strcmp (note->namedata, "LINUX") == 0)
9621 return elfcore_grok_ppc_vmx (abfd, note);
9622 else
9623 return TRUE;
9625 case NT_PPC_VSX:
9626 if (note->namesz == 6
9627 && strcmp (note->namedata, "LINUX") == 0)
9628 return elfcore_grok_ppc_vsx (abfd, note);
9629 else
9630 return TRUE;
9632 case NT_S390_HIGH_GPRS:
9633 if (note->namesz == 6
9634 && strcmp (note->namedata, "LINUX") == 0)
9635 return elfcore_grok_s390_high_gprs (abfd, note);
9636 else
9637 return TRUE;
9639 case NT_S390_TIMER:
9640 if (note->namesz == 6
9641 && strcmp (note->namedata, "LINUX") == 0)
9642 return elfcore_grok_s390_timer (abfd, note);
9643 else
9644 return TRUE;
9646 case NT_S390_TODCMP:
9647 if (note->namesz == 6
9648 && strcmp (note->namedata, "LINUX") == 0)
9649 return elfcore_grok_s390_todcmp (abfd, note);
9650 else
9651 return TRUE;
9653 case NT_S390_TODPREG:
9654 if (note->namesz == 6
9655 && strcmp (note->namedata, "LINUX") == 0)
9656 return elfcore_grok_s390_todpreg (abfd, note);
9657 else
9658 return TRUE;
9660 case NT_S390_CTRS:
9661 if (note->namesz == 6
9662 && strcmp (note->namedata, "LINUX") == 0)
9663 return elfcore_grok_s390_ctrs (abfd, note);
9664 else
9665 return TRUE;
9667 case NT_S390_PREFIX:
9668 if (note->namesz == 6
9669 && strcmp (note->namedata, "LINUX") == 0)
9670 return elfcore_grok_s390_prefix (abfd, note);
9671 else
9672 return TRUE;
9674 case NT_S390_LAST_BREAK:
9675 if (note->namesz == 6
9676 && strcmp (note->namedata, "LINUX") == 0)
9677 return elfcore_grok_s390_last_break (abfd, note);
9678 else
9679 return TRUE;
9681 case NT_S390_SYSTEM_CALL:
9682 if (note->namesz == 6
9683 && strcmp (note->namedata, "LINUX") == 0)
9684 return elfcore_grok_s390_system_call (abfd, note);
9685 else
9686 return TRUE;
9688 case NT_S390_TDB:
9689 if (note->namesz == 6
9690 && strcmp (note->namedata, "LINUX") == 0)
9691 return elfcore_grok_s390_tdb (abfd, note);
9692 else
9693 return TRUE;
9695 case NT_S390_VXRS_LOW:
9696 if (note->namesz == 6
9697 && strcmp (note->namedata, "LINUX") == 0)
9698 return elfcore_grok_s390_vxrs_low (abfd, note);
9699 else
9700 return TRUE;
9702 case NT_S390_VXRS_HIGH:
9703 if (note->namesz == 6
9704 && strcmp (note->namedata, "LINUX") == 0)
9705 return elfcore_grok_s390_vxrs_high (abfd, note);
9706 else
9707 return TRUE;
9709 case NT_S390_GS_CB:
9710 if (note->namesz == 6
9711 && strcmp (note->namedata, "LINUX") == 0)
9712 return elfcore_grok_s390_gs_cb (abfd, note);
9713 else
9714 return TRUE;
9716 case NT_S390_GS_BC:
9717 if (note->namesz == 6
9718 && strcmp (note->namedata, "LINUX") == 0)
9719 return elfcore_grok_s390_gs_bc (abfd, note);
9720 else
9721 return TRUE;
9723 case NT_ARM_VFP:
9724 if (note->namesz == 6
9725 && strcmp (note->namedata, "LINUX") == 0)
9726 return elfcore_grok_arm_vfp (abfd, note);
9727 else
9728 return TRUE;
9730 case NT_ARM_TLS:
9731 if (note->namesz == 6
9732 && strcmp (note->namedata, "LINUX") == 0)
9733 return elfcore_grok_aarch_tls (abfd, note);
9734 else
9735 return TRUE;
9737 case NT_ARM_HW_BREAK:
9738 if (note->namesz == 6
9739 && strcmp (note->namedata, "LINUX") == 0)
9740 return elfcore_grok_aarch_hw_break (abfd, note);
9741 else
9742 return TRUE;
9744 case NT_ARM_HW_WATCH:
9745 if (note->namesz == 6
9746 && strcmp (note->namedata, "LINUX") == 0)
9747 return elfcore_grok_aarch_hw_watch (abfd, note);
9748 else
9749 return TRUE;
9751 case NT_PRPSINFO:
9752 case NT_PSINFO:
9753 if (bed->elf_backend_grok_psinfo)
9754 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
9755 return TRUE;
9756 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9757 return elfcore_grok_psinfo (abfd, note);
9758 #else
9759 return TRUE;
9760 #endif
9762 case NT_AUXV:
9764 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9765 SEC_HAS_CONTENTS);
9767 if (sect == NULL)
9768 return FALSE;
9769 sect->size = note->descsz;
9770 sect->filepos = note->descpos;
9771 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9773 return TRUE;
9776 case NT_FILE:
9777 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
9778 note);
9780 case NT_SIGINFO:
9781 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
9782 note);
9787 static bfd_boolean
9788 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
9790 struct bfd_build_id* build_id;
9792 if (note->descsz == 0)
9793 return FALSE;
9795 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
9796 if (build_id == NULL)
9797 return FALSE;
9799 build_id->size = note->descsz;
9800 memcpy (build_id->data, note->descdata, note->descsz);
9801 abfd->build_id = build_id;
9803 return TRUE;
9806 static bfd_boolean
9807 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
9809 switch (note->type)
9811 default:
9812 return TRUE;
9814 case NT_GNU_PROPERTY_TYPE_0:
9815 return _bfd_elf_parse_gnu_properties (abfd, note);
9817 case NT_GNU_BUILD_ID:
9818 return elfobj_grok_gnu_build_id (abfd, note);
9822 static bfd_boolean
9823 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
9825 struct sdt_note *cur =
9826 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
9827 + note->descsz);
9829 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
9830 cur->size = (bfd_size_type) note->descsz;
9831 memcpy (cur->data, note->descdata, note->descsz);
9833 elf_tdata (abfd)->sdt_note_head = cur;
9835 return TRUE;
9838 static bfd_boolean
9839 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
9841 switch (note->type)
9843 case NT_STAPSDT:
9844 return elfobj_grok_stapsdt_note_1 (abfd, note);
9846 default:
9847 return TRUE;
9851 static bfd_boolean
9852 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
9854 size_t offset;
9856 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
9858 case ELFCLASS32:
9859 if (note->descsz < 108)
9860 return FALSE;
9861 break;
9863 case ELFCLASS64:
9864 if (note->descsz < 120)
9865 return FALSE;
9866 break;
9868 default:
9869 return FALSE;
9872 /* Check for version 1 in pr_version. */
9873 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
9874 return FALSE;
9875 offset = 4;
9877 /* Skip over pr_psinfosz. */
9878 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
9879 offset += 4;
9880 else
9882 offset += 4; /* Padding before pr_psinfosz. */
9883 offset += 8;
9886 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
9887 elf_tdata (abfd)->core->program
9888 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
9889 offset += 17;
9891 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
9892 elf_tdata (abfd)->core->command
9893 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
9894 offset += 81;
9896 /* Padding before pr_pid. */
9897 offset += 2;
9899 /* The pr_pid field was added in version "1a". */
9900 if (note->descsz < offset + 4)
9901 return TRUE;
9903 elf_tdata (abfd)->core->pid
9904 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
9906 return TRUE;
9909 static bfd_boolean
9910 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
9912 size_t offset;
9913 size_t size;
9914 size_t min_size;
9916 /* Compute offset of pr_getregsz, skipping over pr_statussz.
9917 Also compute minimum size of this note. */
9918 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
9920 case ELFCLASS32:
9921 offset = 4 + 4;
9922 min_size = offset + (4 * 2) + 4 + 4 + 4;
9923 break;
9925 case ELFCLASS64:
9926 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
9927 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
9928 break;
9930 default:
9931 return FALSE;
9934 if (note->descsz < min_size)
9935 return FALSE;
9937 /* Check for version 1 in pr_version. */
9938 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
9939 return FALSE;
9941 /* Extract size of pr_reg from pr_gregsetsz. */
9942 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
9943 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
9945 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
9946 offset += 4 * 2;
9948 else
9950 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
9951 offset += 8 * 2;
9954 /* Skip over pr_osreldate. */
9955 offset += 4;
9957 /* Read signal from pr_cursig. */
9958 if (elf_tdata (abfd)->core->signal == 0)
9959 elf_tdata (abfd)->core->signal
9960 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
9961 offset += 4;
9963 /* Read TID from pr_pid. */
9964 elf_tdata (abfd)->core->lwpid
9965 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
9966 offset += 4;
9968 /* Padding before pr_reg. */
9969 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
9970 offset += 4;
9972 /* Make sure that there is enough data remaining in the note. */
9973 if ((note->descsz - offset) < size)
9974 return FALSE;
9976 /* Make a ".reg/999" section and a ".reg" section. */
9977 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9978 size, note->descpos + offset);
9981 static bfd_boolean
9982 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
9984 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9986 switch (note->type)
9988 case NT_PRSTATUS:
9989 if (bed->elf_backend_grok_freebsd_prstatus)
9990 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
9991 return TRUE;
9992 return elfcore_grok_freebsd_prstatus (abfd, note);
9994 case NT_FPREGSET:
9995 return elfcore_grok_prfpreg (abfd, note);
9997 case NT_PRPSINFO:
9998 return elfcore_grok_freebsd_psinfo (abfd, note);
10000 case NT_FREEBSD_THRMISC:
10001 if (note->namesz == 8)
10002 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10003 else
10004 return TRUE;
10006 case NT_FREEBSD_PROCSTAT_AUXV:
10008 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10009 SEC_HAS_CONTENTS);
10011 if (sect == NULL)
10012 return FALSE;
10013 sect->size = note->descsz - 4;
10014 sect->filepos = note->descpos + 4;
10015 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10017 return TRUE;
10020 case NT_X86_XSTATE:
10021 if (note->namesz == 8)
10022 return elfcore_grok_xstatereg (abfd, note);
10023 else
10024 return TRUE;
10026 case NT_FREEBSD_PTLWPINFO:
10027 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10028 note);
10030 case NT_ARM_VFP:
10031 return elfcore_grok_arm_vfp (abfd, note);
10033 default:
10034 return TRUE;
10038 static bfd_boolean
10039 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
10041 char *cp;
10043 cp = strchr (note->namedata, '@');
10044 if (cp != NULL)
10046 *lwpidp = atoi(cp + 1);
10047 return TRUE;
10049 return FALSE;
10052 static bfd_boolean
10053 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10055 /* Signal number at offset 0x08. */
10056 elf_tdata (abfd)->core->signal
10057 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10059 /* Process ID at offset 0x50. */
10060 elf_tdata (abfd)->core->pid
10061 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10063 /* Command name at 0x7c (max 32 bytes, including nul). */
10064 elf_tdata (abfd)->core->command
10065 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10067 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10068 note);
10071 static bfd_boolean
10072 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
10074 int lwp;
10076 if (elfcore_netbsd_get_lwpid (note, &lwp))
10077 elf_tdata (abfd)->core->lwpid = lwp;
10079 if (note->type == NT_NETBSDCORE_PROCINFO)
10081 /* NetBSD-specific core "procinfo". Note that we expect to
10082 find this note before any of the others, which is fine,
10083 since the kernel writes this note out first when it
10084 creates a core file. */
10086 return elfcore_grok_netbsd_procinfo (abfd, note);
10089 /* As of Jan 2002 there are no other machine-independent notes
10090 defined for NetBSD core files. If the note type is less
10091 than the start of the machine-dependent note types, we don't
10092 understand it. */
10094 if (note->type < NT_NETBSDCORE_FIRSTMACH)
10095 return TRUE;
10098 switch (bfd_get_arch (abfd))
10100 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10101 PT_GETFPREGS == mach+2. */
10103 case bfd_arch_alpha:
10104 case bfd_arch_sparc:
10105 switch (note->type)
10107 case NT_NETBSDCORE_FIRSTMACH+0:
10108 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10110 case NT_NETBSDCORE_FIRSTMACH+2:
10111 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10113 default:
10114 return TRUE;
10117 /* On all other arch's, PT_GETREGS == mach+1 and
10118 PT_GETFPREGS == mach+3. */
10120 default:
10121 switch (note->type)
10123 case NT_NETBSDCORE_FIRSTMACH+1:
10124 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10126 case NT_NETBSDCORE_FIRSTMACH+3:
10127 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10129 default:
10130 return TRUE;
10133 /* NOTREACHED */
10136 static bfd_boolean
10137 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10139 /* Signal number at offset 0x08. */
10140 elf_tdata (abfd)->core->signal
10141 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10143 /* Process ID at offset 0x20. */
10144 elf_tdata (abfd)->core->pid
10145 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10147 /* Command name at 0x48 (max 32 bytes, including nul). */
10148 elf_tdata (abfd)->core->command
10149 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10151 return TRUE;
10154 static bfd_boolean
10155 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10157 if (note->type == NT_OPENBSD_PROCINFO)
10158 return elfcore_grok_openbsd_procinfo (abfd, note);
10160 if (note->type == NT_OPENBSD_REGS)
10161 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10163 if (note->type == NT_OPENBSD_FPREGS)
10164 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10166 if (note->type == NT_OPENBSD_XFPREGS)
10167 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10169 if (note->type == NT_OPENBSD_AUXV)
10171 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10172 SEC_HAS_CONTENTS);
10174 if (sect == NULL)
10175 return FALSE;
10176 sect->size = note->descsz;
10177 sect->filepos = note->descpos;
10178 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10180 return TRUE;
10183 if (note->type == NT_OPENBSD_WCOOKIE)
10185 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10186 SEC_HAS_CONTENTS);
10188 if (sect == NULL)
10189 return FALSE;
10190 sect->size = note->descsz;
10191 sect->filepos = note->descpos;
10192 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10194 return TRUE;
10197 return TRUE;
10200 static bfd_boolean
10201 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
10203 void *ddata = note->descdata;
10204 char buf[100];
10205 char *name;
10206 asection *sect;
10207 short sig;
10208 unsigned flags;
10210 /* nto_procfs_status 'pid' field is at offset 0. */
10211 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
10213 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10214 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10216 /* nto_procfs_status 'flags' field is at offset 8. */
10217 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
10219 /* nto_procfs_status 'what' field is at offset 14. */
10220 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10222 elf_tdata (abfd)->core->signal = sig;
10223 elf_tdata (abfd)->core->lwpid = *tid;
10226 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10227 do not come from signals so we make sure we set the current
10228 thread just in case. */
10229 if (flags & 0x00000080)
10230 elf_tdata (abfd)->core->lwpid = *tid;
10232 /* Make a ".qnx_core_status/%d" section. */
10233 sprintf (buf, ".qnx_core_status/%ld", *tid);
10235 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10236 if (name == NULL)
10237 return FALSE;
10238 strcpy (name, buf);
10240 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10241 if (sect == NULL)
10242 return FALSE;
10244 sect->size = note->descsz;
10245 sect->filepos = note->descpos;
10246 sect->alignment_power = 2;
10248 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10251 static bfd_boolean
10252 elfcore_grok_nto_regs (bfd *abfd,
10253 Elf_Internal_Note *note,
10254 long tid,
10255 char *base)
10257 char buf[100];
10258 char *name;
10259 asection *sect;
10261 /* Make a "(base)/%d" section. */
10262 sprintf (buf, "%s/%ld", base, tid);
10264 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
10265 if (name == NULL)
10266 return FALSE;
10267 strcpy (name, buf);
10269 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10270 if (sect == NULL)
10271 return FALSE;
10273 sect->size = note->descsz;
10274 sect->filepos = note->descpos;
10275 sect->alignment_power = 2;
10277 /* This is the current thread. */
10278 if (elf_tdata (abfd)->core->lwpid == tid)
10279 return elfcore_maybe_make_sect (abfd, base, sect);
10281 return TRUE;
10284 #define BFD_QNT_CORE_INFO 7
10285 #define BFD_QNT_CORE_STATUS 8
10286 #define BFD_QNT_CORE_GREG 9
10287 #define BFD_QNT_CORE_FPREG 10
10289 static bfd_boolean
10290 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
10292 /* Every GREG section has a STATUS section before it. Store the
10293 tid from the previous call to pass down to the next gregs
10294 function. */
10295 static long tid = 1;
10297 switch (note->type)
10299 case BFD_QNT_CORE_INFO:
10300 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10301 case BFD_QNT_CORE_STATUS:
10302 return elfcore_grok_nto_status (abfd, note, &tid);
10303 case BFD_QNT_CORE_GREG:
10304 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10305 case BFD_QNT_CORE_FPREG:
10306 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10307 default:
10308 return TRUE;
10312 static bfd_boolean
10313 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10315 char *name;
10316 asection *sect;
10317 size_t len;
10319 /* Use note name as section name. */
10320 len = note->namesz;
10321 name = (char *) bfd_alloc (abfd, len);
10322 if (name == NULL)
10323 return FALSE;
10324 memcpy (name, note->namedata, len);
10325 name[len - 1] = '\0';
10327 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10328 if (sect == NULL)
10329 return FALSE;
10331 sect->size = note->descsz;
10332 sect->filepos = note->descpos;
10333 sect->alignment_power = 1;
10335 return TRUE;
10338 /* Function: elfcore_write_note
10340 Inputs:
10341 buffer to hold note, and current size of buffer
10342 name of note
10343 type of note
10344 data for note
10345 size of data for note
10347 Writes note to end of buffer. ELF64 notes are written exactly as
10348 for ELF32, despite the current (as of 2006) ELF gabi specifying
10349 that they ought to have 8-byte namesz and descsz field, and have
10350 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10352 Return:
10353 Pointer to realloc'd buffer, *BUFSIZ updated. */
10355 char *
10356 elfcore_write_note (bfd *abfd,
10357 char *buf,
10358 int *bufsiz,
10359 const char *name,
10360 int type,
10361 const void *input,
10362 int size)
10364 Elf_External_Note *xnp;
10365 size_t namesz;
10366 size_t newspace;
10367 char *dest;
10369 namesz = 0;
10370 if (name != NULL)
10371 namesz = strlen (name) + 1;
10373 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
10375 buf = (char *) realloc (buf, *bufsiz + newspace);
10376 if (buf == NULL)
10377 return buf;
10378 dest = buf + *bufsiz;
10379 *bufsiz += newspace;
10380 xnp = (Elf_External_Note *) dest;
10381 H_PUT_32 (abfd, namesz, xnp->namesz);
10382 H_PUT_32 (abfd, size, xnp->descsz);
10383 H_PUT_32 (abfd, type, xnp->type);
10384 dest = xnp->name;
10385 if (name != NULL)
10387 memcpy (dest, name, namesz);
10388 dest += namesz;
10389 while (namesz & 3)
10391 *dest++ = '\0';
10392 ++namesz;
10395 memcpy (dest, input, size);
10396 dest += size;
10397 while (size & 3)
10399 *dest++ = '\0';
10400 ++size;
10402 return buf;
10405 char *
10406 elfcore_write_prpsinfo (bfd *abfd,
10407 char *buf,
10408 int *bufsiz,
10409 const char *fname,
10410 const char *psargs)
10412 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10414 if (bed->elf_backend_write_core_note != NULL)
10416 char *ret;
10417 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10418 NT_PRPSINFO, fname, psargs);
10419 if (ret != NULL)
10420 return ret;
10423 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10424 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10425 if (bed->s->elfclass == ELFCLASS32)
10427 #if defined (HAVE_PSINFO32_T)
10428 psinfo32_t data;
10429 int note_type = NT_PSINFO;
10430 #else
10431 prpsinfo32_t data;
10432 int note_type = NT_PRPSINFO;
10433 #endif
10435 memset (&data, 0, sizeof (data));
10436 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10437 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10438 return elfcore_write_note (abfd, buf, bufsiz,
10439 "CORE", note_type, &data, sizeof (data));
10441 else
10442 #endif
10444 #if defined (HAVE_PSINFO_T)
10445 psinfo_t data;
10446 int note_type = NT_PSINFO;
10447 #else
10448 prpsinfo_t data;
10449 int note_type = NT_PRPSINFO;
10450 #endif
10452 memset (&data, 0, sizeof (data));
10453 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10454 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10455 return elfcore_write_note (abfd, buf, bufsiz,
10456 "CORE", note_type, &data, sizeof (data));
10458 #endif /* PSINFO_T or PRPSINFO_T */
10460 free (buf);
10461 return NULL;
10464 char *
10465 elfcore_write_linux_prpsinfo32
10466 (bfd *abfd, char *buf, int *bufsiz,
10467 const struct elf_internal_linux_prpsinfo *prpsinfo)
10469 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
10471 struct elf_external_linux_prpsinfo32_ugid16 data;
10473 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
10474 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
10475 &data, sizeof (data));
10477 else
10479 struct elf_external_linux_prpsinfo32_ugid32 data;
10481 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
10482 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
10483 &data, sizeof (data));
10487 char *
10488 elfcore_write_linux_prpsinfo64
10489 (bfd *abfd, char *buf, int *bufsiz,
10490 const struct elf_internal_linux_prpsinfo *prpsinfo)
10492 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
10494 struct elf_external_linux_prpsinfo64_ugid16 data;
10496 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
10497 return elfcore_write_note (abfd, buf, bufsiz,
10498 "CORE", NT_PRPSINFO, &data, sizeof (data));
10500 else
10502 struct elf_external_linux_prpsinfo64_ugid32 data;
10504 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
10505 return elfcore_write_note (abfd, buf, bufsiz,
10506 "CORE", NT_PRPSINFO, &data, sizeof (data));
10510 char *
10511 elfcore_write_prstatus (bfd *abfd,
10512 char *buf,
10513 int *bufsiz,
10514 long pid,
10515 int cursig,
10516 const void *gregs)
10518 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10520 if (bed->elf_backend_write_core_note != NULL)
10522 char *ret;
10523 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10524 NT_PRSTATUS,
10525 pid, cursig, gregs);
10526 if (ret != NULL)
10527 return ret;
10530 #if defined (HAVE_PRSTATUS_T)
10531 #if defined (HAVE_PRSTATUS32_T)
10532 if (bed->s->elfclass == ELFCLASS32)
10534 prstatus32_t prstat;
10536 memset (&prstat, 0, sizeof (prstat));
10537 prstat.pr_pid = pid;
10538 prstat.pr_cursig = cursig;
10539 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
10540 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
10541 NT_PRSTATUS, &prstat, sizeof (prstat));
10543 else
10544 #endif
10546 prstatus_t prstat;
10548 memset (&prstat, 0, sizeof (prstat));
10549 prstat.pr_pid = pid;
10550 prstat.pr_cursig = cursig;
10551 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
10552 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
10553 NT_PRSTATUS, &prstat, sizeof (prstat));
10555 #endif /* HAVE_PRSTATUS_T */
10557 free (buf);
10558 return NULL;
10561 #if defined (HAVE_LWPSTATUS_T)
10562 char *
10563 elfcore_write_lwpstatus (bfd *abfd,
10564 char *buf,
10565 int *bufsiz,
10566 long pid,
10567 int cursig,
10568 const void *gregs)
10570 lwpstatus_t lwpstat;
10571 const char *note_name = "CORE";
10573 memset (&lwpstat, 0, sizeof (lwpstat));
10574 lwpstat.pr_lwpid = pid >> 16;
10575 lwpstat.pr_cursig = cursig;
10576 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10577 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
10578 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10579 #if !defined(gregs)
10580 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
10581 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
10582 #else
10583 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
10584 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
10585 #endif
10586 #endif
10587 return elfcore_write_note (abfd, buf, bufsiz, note_name,
10588 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
10590 #endif /* HAVE_LWPSTATUS_T */
10592 #if defined (HAVE_PSTATUS_T)
10593 char *
10594 elfcore_write_pstatus (bfd *abfd,
10595 char *buf,
10596 int *bufsiz,
10597 long pid,
10598 int cursig ATTRIBUTE_UNUSED,
10599 const void *gregs ATTRIBUTE_UNUSED)
10601 const char *note_name = "CORE";
10602 #if defined (HAVE_PSTATUS32_T)
10603 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10605 if (bed->s->elfclass == ELFCLASS32)
10607 pstatus32_t pstat;
10609 memset (&pstat, 0, sizeof (pstat));
10610 pstat.pr_pid = pid & 0xffff;
10611 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
10612 NT_PSTATUS, &pstat, sizeof (pstat));
10613 return buf;
10615 else
10616 #endif
10618 pstatus_t pstat;
10620 memset (&pstat, 0, sizeof (pstat));
10621 pstat.pr_pid = pid & 0xffff;
10622 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
10623 NT_PSTATUS, &pstat, sizeof (pstat));
10624 return buf;
10627 #endif /* HAVE_PSTATUS_T */
10629 char *
10630 elfcore_write_prfpreg (bfd *abfd,
10631 char *buf,
10632 int *bufsiz,
10633 const void *fpregs,
10634 int size)
10636 const char *note_name = "CORE";
10637 return elfcore_write_note (abfd, buf, bufsiz,
10638 note_name, NT_FPREGSET, fpregs, size);
10641 char *
10642 elfcore_write_prxfpreg (bfd *abfd,
10643 char *buf,
10644 int *bufsiz,
10645 const void *xfpregs,
10646 int size)
10648 char *note_name = "LINUX";
10649 return elfcore_write_note (abfd, buf, bufsiz,
10650 note_name, NT_PRXFPREG, xfpregs, size);
10653 char *
10654 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
10655 const void *xfpregs, int size)
10657 char *note_name;
10658 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
10659 note_name = "FreeBSD";
10660 else
10661 note_name = "LINUX";
10662 return elfcore_write_note (abfd, buf, bufsiz,
10663 note_name, NT_X86_XSTATE, xfpregs, size);
10666 char *
10667 elfcore_write_ppc_vmx (bfd *abfd,
10668 char *buf,
10669 int *bufsiz,
10670 const void *ppc_vmx,
10671 int size)
10673 char *note_name = "LINUX";
10674 return elfcore_write_note (abfd, buf, bufsiz,
10675 note_name, NT_PPC_VMX, ppc_vmx, size);
10678 char *
10679 elfcore_write_ppc_vsx (bfd *abfd,
10680 char *buf,
10681 int *bufsiz,
10682 const void *ppc_vsx,
10683 int size)
10685 char *note_name = "LINUX";
10686 return elfcore_write_note (abfd, buf, bufsiz,
10687 note_name, NT_PPC_VSX, ppc_vsx, size);
10690 static char *
10691 elfcore_write_s390_high_gprs (bfd *abfd,
10692 char *buf,
10693 int *bufsiz,
10694 const void *s390_high_gprs,
10695 int size)
10697 char *note_name = "LINUX";
10698 return elfcore_write_note (abfd, buf, bufsiz,
10699 note_name, NT_S390_HIGH_GPRS,
10700 s390_high_gprs, size);
10703 char *
10704 elfcore_write_s390_timer (bfd *abfd,
10705 char *buf,
10706 int *bufsiz,
10707 const void *s390_timer,
10708 int size)
10710 char *note_name = "LINUX";
10711 return elfcore_write_note (abfd, buf, bufsiz,
10712 note_name, NT_S390_TIMER, s390_timer, size);
10715 char *
10716 elfcore_write_s390_todcmp (bfd *abfd,
10717 char *buf,
10718 int *bufsiz,
10719 const void *s390_todcmp,
10720 int size)
10722 char *note_name = "LINUX";
10723 return elfcore_write_note (abfd, buf, bufsiz,
10724 note_name, NT_S390_TODCMP, s390_todcmp, size);
10727 char *
10728 elfcore_write_s390_todpreg (bfd *abfd,
10729 char *buf,
10730 int *bufsiz,
10731 const void *s390_todpreg,
10732 int size)
10734 char *note_name = "LINUX";
10735 return elfcore_write_note (abfd, buf, bufsiz,
10736 note_name, NT_S390_TODPREG, s390_todpreg, size);
10739 char *
10740 elfcore_write_s390_ctrs (bfd *abfd,
10741 char *buf,
10742 int *bufsiz,
10743 const void *s390_ctrs,
10744 int size)
10746 char *note_name = "LINUX";
10747 return elfcore_write_note (abfd, buf, bufsiz,
10748 note_name, NT_S390_CTRS, s390_ctrs, size);
10751 char *
10752 elfcore_write_s390_prefix (bfd *abfd,
10753 char *buf,
10754 int *bufsiz,
10755 const void *s390_prefix,
10756 int size)
10758 char *note_name = "LINUX";
10759 return elfcore_write_note (abfd, buf, bufsiz,
10760 note_name, NT_S390_PREFIX, s390_prefix, size);
10763 char *
10764 elfcore_write_s390_last_break (bfd *abfd,
10765 char *buf,
10766 int *bufsiz,
10767 const void *s390_last_break,
10768 int size)
10770 char *note_name = "LINUX";
10771 return elfcore_write_note (abfd, buf, bufsiz,
10772 note_name, NT_S390_LAST_BREAK,
10773 s390_last_break, size);
10776 char *
10777 elfcore_write_s390_system_call (bfd *abfd,
10778 char *buf,
10779 int *bufsiz,
10780 const void *s390_system_call,
10781 int size)
10783 char *note_name = "LINUX";
10784 return elfcore_write_note (abfd, buf, bufsiz,
10785 note_name, NT_S390_SYSTEM_CALL,
10786 s390_system_call, size);
10789 char *
10790 elfcore_write_s390_tdb (bfd *abfd,
10791 char *buf,
10792 int *bufsiz,
10793 const void *s390_tdb,
10794 int size)
10796 char *note_name = "LINUX";
10797 return elfcore_write_note (abfd, buf, bufsiz,
10798 note_name, NT_S390_TDB, s390_tdb, size);
10801 char *
10802 elfcore_write_s390_vxrs_low (bfd *abfd,
10803 char *buf,
10804 int *bufsiz,
10805 const void *s390_vxrs_low,
10806 int size)
10808 char *note_name = "LINUX";
10809 return elfcore_write_note (abfd, buf, bufsiz,
10810 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
10813 char *
10814 elfcore_write_s390_vxrs_high (bfd *abfd,
10815 char *buf,
10816 int *bufsiz,
10817 const void *s390_vxrs_high,
10818 int size)
10820 char *note_name = "LINUX";
10821 return elfcore_write_note (abfd, buf, bufsiz,
10822 note_name, NT_S390_VXRS_HIGH,
10823 s390_vxrs_high, size);
10826 char *
10827 elfcore_write_s390_gs_cb (bfd *abfd,
10828 char *buf,
10829 int *bufsiz,
10830 const void *s390_gs_cb,
10831 int size)
10833 char *note_name = "LINUX";
10834 return elfcore_write_note (abfd, buf, bufsiz,
10835 note_name, NT_S390_GS_CB,
10836 s390_gs_cb, size);
10839 char *
10840 elfcore_write_s390_gs_bc (bfd *abfd,
10841 char *buf,
10842 int *bufsiz,
10843 const void *s390_gs_bc,
10844 int size)
10846 char *note_name = "LINUX";
10847 return elfcore_write_note (abfd, buf, bufsiz,
10848 note_name, NT_S390_GS_BC,
10849 s390_gs_bc, size);
10852 char *
10853 elfcore_write_arm_vfp (bfd *abfd,
10854 char *buf,
10855 int *bufsiz,
10856 const void *arm_vfp,
10857 int size)
10859 char *note_name = "LINUX";
10860 return elfcore_write_note (abfd, buf, bufsiz,
10861 note_name, NT_ARM_VFP, arm_vfp, size);
10864 char *
10865 elfcore_write_aarch_tls (bfd *abfd,
10866 char *buf,
10867 int *bufsiz,
10868 const void *aarch_tls,
10869 int size)
10871 char *note_name = "LINUX";
10872 return elfcore_write_note (abfd, buf, bufsiz,
10873 note_name, NT_ARM_TLS, aarch_tls, size);
10876 char *
10877 elfcore_write_aarch_hw_break (bfd *abfd,
10878 char *buf,
10879 int *bufsiz,
10880 const void *aarch_hw_break,
10881 int size)
10883 char *note_name = "LINUX";
10884 return elfcore_write_note (abfd, buf, bufsiz,
10885 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
10888 char *
10889 elfcore_write_aarch_hw_watch (bfd *abfd,
10890 char *buf,
10891 int *bufsiz,
10892 const void *aarch_hw_watch,
10893 int size)
10895 char *note_name = "LINUX";
10896 return elfcore_write_note (abfd, buf, bufsiz,
10897 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
10900 char *
10901 elfcore_write_register_note (bfd *abfd,
10902 char *buf,
10903 int *bufsiz,
10904 const char *section,
10905 const void *data,
10906 int size)
10908 if (strcmp (section, ".reg2") == 0)
10909 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
10910 if (strcmp (section, ".reg-xfp") == 0)
10911 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
10912 if (strcmp (section, ".reg-xstate") == 0)
10913 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
10914 if (strcmp (section, ".reg-ppc-vmx") == 0)
10915 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
10916 if (strcmp (section, ".reg-ppc-vsx") == 0)
10917 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
10918 if (strcmp (section, ".reg-s390-high-gprs") == 0)
10919 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
10920 if (strcmp (section, ".reg-s390-timer") == 0)
10921 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
10922 if (strcmp (section, ".reg-s390-todcmp") == 0)
10923 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
10924 if (strcmp (section, ".reg-s390-todpreg") == 0)
10925 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
10926 if (strcmp (section, ".reg-s390-ctrs") == 0)
10927 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
10928 if (strcmp (section, ".reg-s390-prefix") == 0)
10929 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
10930 if (strcmp (section, ".reg-s390-last-break") == 0)
10931 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
10932 if (strcmp (section, ".reg-s390-system-call") == 0)
10933 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
10934 if (strcmp (section, ".reg-s390-tdb") == 0)
10935 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
10936 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
10937 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
10938 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
10939 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
10940 if (strcmp (section, ".reg-s390-gs-cb") == 0)
10941 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
10942 if (strcmp (section, ".reg-s390-gs-bc") == 0)
10943 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
10944 if (strcmp (section, ".reg-arm-vfp") == 0)
10945 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
10946 if (strcmp (section, ".reg-aarch-tls") == 0)
10947 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
10948 if (strcmp (section, ".reg-aarch-hw-break") == 0)
10949 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
10950 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
10951 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
10952 return NULL;
10955 static bfd_boolean
10956 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
10958 char *p;
10960 p = buf;
10961 while (p < buf + size)
10963 /* FIXME: bad alignment assumption. */
10964 Elf_External_Note *xnp = (Elf_External_Note *) p;
10965 Elf_Internal_Note in;
10967 if (offsetof (Elf_External_Note, name) > buf - p + size)
10968 return FALSE;
10970 in.type = H_GET_32 (abfd, xnp->type);
10972 in.namesz = H_GET_32 (abfd, xnp->namesz);
10973 in.namedata = xnp->name;
10974 if (in.namesz > buf - in.namedata + size)
10975 return FALSE;
10977 in.descsz = H_GET_32 (abfd, xnp->descsz);
10978 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
10979 in.descpos = offset + (in.descdata - buf);
10980 if (in.descsz != 0
10981 && (in.descdata >= buf + size
10982 || in.descsz > buf - in.descdata + size))
10983 return FALSE;
10985 switch (bfd_get_format (abfd))
10987 default:
10988 return TRUE;
10990 case bfd_core:
10992 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10993 struct
10995 const char * string;
10996 size_t len;
10997 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
10999 grokers[] =
11001 GROKER_ELEMENT ("", elfcore_grok_note),
11002 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
11003 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11004 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11005 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11006 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
11008 #undef GROKER_ELEMENT
11009 int i;
11011 for (i = ARRAY_SIZE (grokers); i--;)
11013 if (in.namesz >= grokers[i].len
11014 && strncmp (in.namedata, grokers[i].string,
11015 grokers[i].len) == 0)
11017 if (! grokers[i].func (abfd, & in))
11018 return FALSE;
11019 break;
11022 break;
11025 case bfd_object:
11026 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11028 if (! elfobj_grok_gnu_note (abfd, &in))
11029 return FALSE;
11031 else if (in.namesz == sizeof "stapsdt"
11032 && strcmp (in.namedata, "stapsdt") == 0)
11034 if (! elfobj_grok_stapsdt_note (abfd, &in))
11035 return FALSE;
11037 break;
11040 p = in.descdata + BFD_ALIGN (in.descsz, 4);
11043 return TRUE;
11046 static bfd_boolean
11047 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
11049 char *buf;
11051 if (size == 0 || (size + 1) == 0)
11052 return TRUE;
11054 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11055 return FALSE;
11057 buf = (char *) bfd_malloc (size + 1);
11058 if (buf == NULL)
11059 return FALSE;
11061 /* PR 17512: file: ec08f814
11062 0-termintate the buffer so that string searches will not overflow. */
11063 buf[size] = 0;
11065 if (bfd_bread (buf, size, abfd) != size
11066 || !elf_parse_notes (abfd, buf, size, offset))
11068 free (buf);
11069 return FALSE;
11072 free (buf);
11073 return TRUE;
11076 /* Providing external access to the ELF program header table. */
11078 /* Return an upper bound on the number of bytes required to store a
11079 copy of ABFD's program header table entries. Return -1 if an error
11080 occurs; bfd_get_error will return an appropriate code. */
11082 long
11083 bfd_get_elf_phdr_upper_bound (bfd *abfd)
11085 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11087 bfd_set_error (bfd_error_wrong_format);
11088 return -1;
11091 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
11094 /* Copy ABFD's program header table entries to *PHDRS. The entries
11095 will be stored as an array of Elf_Internal_Phdr structures, as
11096 defined in include/elf/internal.h. To find out how large the
11097 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11099 Return the number of program header table entries read, or -1 if an
11100 error occurs; bfd_get_error will return an appropriate code. */
11103 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
11105 int num_phdrs;
11107 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11109 bfd_set_error (bfd_error_wrong_format);
11110 return -1;
11113 num_phdrs = elf_elfheader (abfd)->e_phnum;
11114 memcpy (phdrs, elf_tdata (abfd)->phdr,
11115 num_phdrs * sizeof (Elf_Internal_Phdr));
11117 return num_phdrs;
11120 enum elf_reloc_type_class
11121 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11122 const asection *rel_sec ATTRIBUTE_UNUSED,
11123 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
11125 return reloc_class_normal;
11128 /* For RELA architectures, return the relocation value for a
11129 relocation against a local symbol. */
11131 bfd_vma
11132 _bfd_elf_rela_local_sym (bfd *abfd,
11133 Elf_Internal_Sym *sym,
11134 asection **psec,
11135 Elf_Internal_Rela *rel)
11137 asection *sec = *psec;
11138 bfd_vma relocation;
11140 relocation = (sec->output_section->vma
11141 + sec->output_offset
11142 + sym->st_value);
11143 if ((sec->flags & SEC_MERGE)
11144 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
11145 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
11147 rel->r_addend =
11148 _bfd_merged_section_offset (abfd, psec,
11149 elf_section_data (sec)->sec_info,
11150 sym->st_value + rel->r_addend);
11151 if (sec != *psec)
11153 /* If we have changed the section, and our original section is
11154 marked with SEC_EXCLUDE, it means that the original
11155 SEC_MERGE section has been completely subsumed in some
11156 other SEC_MERGE section. In this case, we need to leave
11157 some info around for --emit-relocs. */
11158 if ((sec->flags & SEC_EXCLUDE) != 0)
11159 sec->kept_section = *psec;
11160 sec = *psec;
11162 rel->r_addend -= relocation;
11163 rel->r_addend += sec->output_section->vma + sec->output_offset;
11165 return relocation;
11168 bfd_vma
11169 _bfd_elf_rel_local_sym (bfd *abfd,
11170 Elf_Internal_Sym *sym,
11171 asection **psec,
11172 bfd_vma addend)
11174 asection *sec = *psec;
11176 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
11177 return sym->st_value + addend;
11179 return _bfd_merged_section_offset (abfd, psec,
11180 elf_section_data (sec)->sec_info,
11181 sym->st_value + addend);
11184 /* Adjust an address within a section. Given OFFSET within SEC, return
11185 the new offset within the section, based upon changes made to the
11186 section. Returns -1 if the offset is now invalid.
11187 The offset (in abnd out) is in target sized bytes, however big a
11188 byte may be. */
11190 bfd_vma
11191 _bfd_elf_section_offset (bfd *abfd,
11192 struct bfd_link_info *info,
11193 asection *sec,
11194 bfd_vma offset)
11196 switch (sec->sec_info_type)
11198 case SEC_INFO_TYPE_STABS:
11199 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
11200 offset);
11201 case SEC_INFO_TYPE_EH_FRAME:
11202 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
11204 default:
11205 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
11207 /* Reverse the offset. */
11208 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11209 bfd_size_type address_size = bed->s->arch_size / 8;
11211 /* address_size and sec->size are in octets. Convert
11212 to bytes before subtracting the original offset. */
11213 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
11215 return offset;
11219 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
11220 reconstruct an ELF file by reading the segments out of remote memory
11221 based on the ELF file header at EHDR_VMA and the ELF program headers it
11222 points to. If not null, *LOADBASEP is filled in with the difference
11223 between the VMAs from which the segments were read, and the VMAs the
11224 file headers (and hence BFD's idea of each section's VMA) put them at.
11226 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11227 remote memory at target address VMA into the local buffer at MYADDR; it
11228 should return zero on success or an `errno' code on failure. TEMPL must
11229 be a BFD for an ELF target with the word size and byte order found in
11230 the remote memory. */
11232 bfd *
11233 bfd_elf_bfd_from_remote_memory
11234 (bfd *templ,
11235 bfd_vma ehdr_vma,
11236 bfd_size_type size,
11237 bfd_vma *loadbasep,
11238 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
11240 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
11241 (templ, ehdr_vma, size, loadbasep, target_read_memory);
11244 long
11245 _bfd_elf_get_synthetic_symtab (bfd *abfd,
11246 long symcount ATTRIBUTE_UNUSED,
11247 asymbol **syms ATTRIBUTE_UNUSED,
11248 long dynsymcount,
11249 asymbol **dynsyms,
11250 asymbol **ret)
11252 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11253 asection *relplt;
11254 asymbol *s;
11255 const char *relplt_name;
11256 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
11257 arelent *p;
11258 long count, i, n;
11259 size_t size;
11260 Elf_Internal_Shdr *hdr;
11261 char *names;
11262 asection *plt;
11264 *ret = NULL;
11266 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
11267 return 0;
11269 if (dynsymcount <= 0)
11270 return 0;
11272 if (!bed->plt_sym_val)
11273 return 0;
11275 relplt_name = bed->relplt_name;
11276 if (relplt_name == NULL)
11277 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
11278 relplt = bfd_get_section_by_name (abfd, relplt_name);
11279 if (relplt == NULL)
11280 return 0;
11282 hdr = &elf_section_data (relplt)->this_hdr;
11283 if (hdr->sh_link != elf_dynsymtab (abfd)
11284 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
11285 return 0;
11287 plt = bfd_get_section_by_name (abfd, ".plt");
11288 if (plt == NULL)
11289 return 0;
11291 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
11292 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
11293 return -1;
11295 count = relplt->size / hdr->sh_entsize;
11296 size = count * sizeof (asymbol);
11297 p = relplt->relocation;
11298 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
11300 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
11301 if (p->addend != 0)
11303 #ifdef BFD64
11304 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
11305 #else
11306 size += sizeof ("+0x") - 1 + 8;
11307 #endif
11311 s = *ret = (asymbol *) bfd_malloc (size);
11312 if (s == NULL)
11313 return -1;
11315 names = (char *) (s + count);
11316 p = relplt->relocation;
11317 n = 0;
11318 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
11320 size_t len;
11321 bfd_vma addr;
11323 addr = bed->plt_sym_val (i, plt, p);
11324 if (addr == (bfd_vma) -1)
11325 continue;
11327 *s = **p->sym_ptr_ptr;
11328 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11329 we are defining a symbol, ensure one of them is set. */
11330 if ((s->flags & BSF_LOCAL) == 0)
11331 s->flags |= BSF_GLOBAL;
11332 s->flags |= BSF_SYNTHETIC;
11333 s->section = plt;
11334 s->value = addr - plt->vma;
11335 s->name = names;
11336 s->udata.p = NULL;
11337 len = strlen ((*p->sym_ptr_ptr)->name);
11338 memcpy (names, (*p->sym_ptr_ptr)->name, len);
11339 names += len;
11340 if (p->addend != 0)
11342 char buf[30], *a;
11344 memcpy (names, "+0x", sizeof ("+0x") - 1);
11345 names += sizeof ("+0x") - 1;
11346 bfd_sprintf_vma (abfd, buf, p->addend);
11347 for (a = buf; *a == '0'; ++a)
11349 len = strlen (a);
11350 memcpy (names, a, len);
11351 names += len;
11353 memcpy (names, "@plt", sizeof ("@plt"));
11354 names += sizeof ("@plt");
11355 ++s, ++n;
11358 return n;
11361 /* It is only used by x86-64 so far.
11362 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
11363 but current usage would allow all of _bfd_std_section to be zero. */
11364 static const asymbol lcomm_sym
11365 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
11366 asection _bfd_elf_large_com_section
11367 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
11368 "LARGE_COMMON", 0, SEC_IS_COMMON);
11370 void
11371 _bfd_elf_post_process_headers (bfd * abfd,
11372 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
11374 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
11376 i_ehdrp = elf_elfheader (abfd);
11378 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
11380 /* To make things simpler for the loader on Linux systems we set the
11381 osabi field to ELFOSABI_GNU if the binary contains symbols of
11382 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
11383 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
11384 && elf_tdata (abfd)->has_gnu_symbols)
11385 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
11389 /* Return TRUE for ELF symbol types that represent functions.
11390 This is the default version of this function, which is sufficient for
11391 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
11393 bfd_boolean
11394 _bfd_elf_is_function_type (unsigned int type)
11396 return (type == STT_FUNC
11397 || type == STT_GNU_IFUNC);
11400 /* If the ELF symbol SYM might be a function in SEC, return the
11401 function size and set *CODE_OFF to the function's entry point,
11402 otherwise return zero. */
11404 bfd_size_type
11405 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
11406 bfd_vma *code_off)
11408 bfd_size_type size;
11410 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
11411 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
11412 || sym->section != sec)
11413 return 0;
11415 *code_off = sym->value;
11416 size = 0;
11417 if (!(sym->flags & BSF_SYNTHETIC))
11418 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
11419 if (size == 0)
11420 size = 1;
11421 return size;