Include compressed DWARF debug sections in ELF linker scripts.
[binutils.git] / bfd / elf.c
blob4f326a7ae538d1354f4629f0bd9d6279c0998463
1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
26 SECTION
27 ELF backends
29 BFD support for ELF formats is being worked on.
30 Currently, the best supported back ends are for sparc and i386
31 (running svr4 or Solaris 2).
33 Documentation of the internals of the support code still needs
34 to be written. The code is changing quickly enough that we
35 haven't bothered yet. */
37 /* For sparc64-cross-sparc32. */
38 #define _SYSCALL32
39 #include "sysdep.h"
40 #include "bfd.h"
41 #include "bfdlink.h"
42 #include "libbfd.h"
43 #define ARCH_SIZE 0
44 #include "elf-bfd.h"
45 #include "libiberty.h"
46 #include "safe-ctype.h"
48 #ifdef CORE_HEADER
49 #include CORE_HEADER
50 #endif
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bfd_boolean prep_headers (bfd *);
55 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
56 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
57 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
58 file_ptr offset);
60 /* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
64 /* Swap in a Verdef structure. */
66 void
67 _bfd_elf_swap_verdef_in (bfd *abfd,
68 const Elf_External_Verdef *src,
69 Elf_Internal_Verdef *dst)
71 dst->vd_version = H_GET_16 (abfd, src->vd_version);
72 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
73 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
74 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
75 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
76 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
77 dst->vd_next = H_GET_32 (abfd, src->vd_next);
80 /* Swap out a Verdef structure. */
82 void
83 _bfd_elf_swap_verdef_out (bfd *abfd,
84 const Elf_Internal_Verdef *src,
85 Elf_External_Verdef *dst)
87 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
88 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
89 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
90 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
91 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
92 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
93 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
96 /* Swap in a Verdaux structure. */
98 void
99 _bfd_elf_swap_verdaux_in (bfd *abfd,
100 const Elf_External_Verdaux *src,
101 Elf_Internal_Verdaux *dst)
103 dst->vda_name = H_GET_32 (abfd, src->vda_name);
104 dst->vda_next = H_GET_32 (abfd, src->vda_next);
107 /* Swap out a Verdaux structure. */
109 void
110 _bfd_elf_swap_verdaux_out (bfd *abfd,
111 const Elf_Internal_Verdaux *src,
112 Elf_External_Verdaux *dst)
114 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
115 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
118 /* Swap in a Verneed structure. */
120 void
121 _bfd_elf_swap_verneed_in (bfd *abfd,
122 const Elf_External_Verneed *src,
123 Elf_Internal_Verneed *dst)
125 dst->vn_version = H_GET_16 (abfd, src->vn_version);
126 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
127 dst->vn_file = H_GET_32 (abfd, src->vn_file);
128 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
129 dst->vn_next = H_GET_32 (abfd, src->vn_next);
132 /* Swap out a Verneed structure. */
134 void
135 _bfd_elf_swap_verneed_out (bfd *abfd,
136 const Elf_Internal_Verneed *src,
137 Elf_External_Verneed *dst)
139 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
140 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
141 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
142 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
143 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
146 /* Swap in a Vernaux structure. */
148 void
149 _bfd_elf_swap_vernaux_in (bfd *abfd,
150 const Elf_External_Vernaux *src,
151 Elf_Internal_Vernaux *dst)
153 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
154 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
155 dst->vna_other = H_GET_16 (abfd, src->vna_other);
156 dst->vna_name = H_GET_32 (abfd, src->vna_name);
157 dst->vna_next = H_GET_32 (abfd, src->vna_next);
160 /* Swap out a Vernaux structure. */
162 void
163 _bfd_elf_swap_vernaux_out (bfd *abfd,
164 const Elf_Internal_Vernaux *src,
165 Elf_External_Vernaux *dst)
167 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
168 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
169 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
170 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
171 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
174 /* Swap in a Versym structure. */
176 void
177 _bfd_elf_swap_versym_in (bfd *abfd,
178 const Elf_External_Versym *src,
179 Elf_Internal_Versym *dst)
181 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
184 /* Swap out a Versym structure. */
186 void
187 _bfd_elf_swap_versym_out (bfd *abfd,
188 const Elf_Internal_Versym *src,
189 Elf_External_Versym *dst)
191 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
194 /* Standard ELF hash function. Do not change this function; you will
195 cause invalid hash tables to be generated. */
197 unsigned long
198 bfd_elf_hash (const char *namearg)
200 const unsigned char *name = (const unsigned char *) namearg;
201 unsigned long h = 0;
202 unsigned long g;
203 int ch;
205 while ((ch = *name++) != '\0')
207 h = (h << 4) + ch;
208 if ((g = (h & 0xf0000000)) != 0)
210 h ^= g >> 24;
211 /* The ELF ABI says `h &= ~g', but this is equivalent in
212 this case and on some machines one insn instead of two. */
213 h ^= g;
216 return h & 0xffffffff;
219 /* DT_GNU_HASH hash function. Do not change this function; you will
220 cause invalid hash tables to be generated. */
222 unsigned long
223 bfd_elf_gnu_hash (const char *namearg)
225 const unsigned char *name = (const unsigned char *) namearg;
226 unsigned long h = 5381;
227 unsigned char ch;
229 while ((ch = *name++) != '\0')
230 h = (h << 5) + h + ch;
231 return h & 0xffffffff;
234 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
235 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_boolean
237 bfd_elf_allocate_object (bfd *abfd,
238 size_t object_size,
239 enum elf_target_id object_id)
241 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
242 abfd->tdata.any = bfd_zalloc (abfd, object_size);
243 if (abfd->tdata.any == NULL)
244 return FALSE;
246 elf_object_id (abfd) = object_id;
247 elf_program_header_size (abfd) = (bfd_size_type) -1;
248 return TRUE;
252 bfd_boolean
253 bfd_elf_make_object (bfd *abfd)
255 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
256 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
257 bed->target_id);
260 bfd_boolean
261 bfd_elf_mkcorefile (bfd *abfd)
263 /* I think this can be done just like an object file. */
264 return abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd);
267 static char *
268 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
270 Elf_Internal_Shdr **i_shdrp;
271 bfd_byte *shstrtab = NULL;
272 file_ptr offset;
273 bfd_size_type shstrtabsize;
275 i_shdrp = elf_elfsections (abfd);
276 if (i_shdrp == 0
277 || shindex >= elf_numsections (abfd)
278 || i_shdrp[shindex] == 0)
279 return NULL;
281 shstrtab = i_shdrp[shindex]->contents;
282 if (shstrtab == NULL)
284 /* No cached one, attempt to read, and cache what we read. */
285 offset = i_shdrp[shindex]->sh_offset;
286 shstrtabsize = i_shdrp[shindex]->sh_size;
288 /* Allocate and clear an extra byte at the end, to prevent crashes
289 in case the string table is not terminated. */
290 if (shstrtabsize + 1 <= 1
291 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL
292 || bfd_seek (abfd, offset, SEEK_SET) != 0)
293 shstrtab = NULL;
294 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
296 if (bfd_get_error () != bfd_error_system_call)
297 bfd_set_error (bfd_error_file_truncated);
298 shstrtab = NULL;
299 /* Once we've failed to read it, make sure we don't keep
300 trying. Otherwise, we'll keep allocating space for
301 the string table over and over. */
302 i_shdrp[shindex]->sh_size = 0;
304 else
305 shstrtab[shstrtabsize] = '\0';
306 i_shdrp[shindex]->contents = shstrtab;
308 return (char *) shstrtab;
311 char *
312 bfd_elf_string_from_elf_section (bfd *abfd,
313 unsigned int shindex,
314 unsigned int strindex)
316 Elf_Internal_Shdr *hdr;
318 if (strindex == 0)
319 return "";
321 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
322 return NULL;
324 hdr = elf_elfsections (abfd)[shindex];
326 if (hdr->contents == NULL
327 && bfd_elf_get_str_section (abfd, shindex) == NULL)
328 return NULL;
330 if (strindex >= hdr->sh_size)
332 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
333 (*_bfd_error_handler)
334 (_("%B: invalid string offset %u >= %lu for section `%s'"),
335 abfd, strindex, (unsigned long) hdr->sh_size,
336 (shindex == shstrndx && strindex == hdr->sh_name
337 ? ".shstrtab"
338 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
339 return NULL;
342 return ((char *) hdr->contents) + strindex;
345 /* Read and convert symbols to internal format.
346 SYMCOUNT specifies the number of symbols to read, starting from
347 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
348 are non-NULL, they are used to store the internal symbols, external
349 symbols, and symbol section index extensions, respectively.
350 Returns a pointer to the internal symbol buffer (malloced if necessary)
351 or NULL if there were no symbols or some kind of problem. */
353 Elf_Internal_Sym *
354 bfd_elf_get_elf_syms (bfd *ibfd,
355 Elf_Internal_Shdr *symtab_hdr,
356 size_t symcount,
357 size_t symoffset,
358 Elf_Internal_Sym *intsym_buf,
359 void *extsym_buf,
360 Elf_External_Sym_Shndx *extshndx_buf)
362 Elf_Internal_Shdr *shndx_hdr;
363 void *alloc_ext;
364 const bfd_byte *esym;
365 Elf_External_Sym_Shndx *alloc_extshndx;
366 Elf_External_Sym_Shndx *shndx;
367 Elf_Internal_Sym *alloc_intsym;
368 Elf_Internal_Sym *isym;
369 Elf_Internal_Sym *isymend;
370 const struct elf_backend_data *bed;
371 size_t extsym_size;
372 bfd_size_type amt;
373 file_ptr pos;
375 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
376 abort ();
378 if (symcount == 0)
379 return intsym_buf;
381 /* Normal syms might have section extension entries. */
382 shndx_hdr = NULL;
383 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
384 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
386 /* Read the symbols. */
387 alloc_ext = NULL;
388 alloc_extshndx = NULL;
389 alloc_intsym = NULL;
390 bed = get_elf_backend_data (ibfd);
391 extsym_size = bed->s->sizeof_sym;
392 amt = symcount * extsym_size;
393 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
394 if (extsym_buf == NULL)
396 alloc_ext = bfd_malloc2 (symcount, extsym_size);
397 extsym_buf = alloc_ext;
399 if (extsym_buf == NULL
400 || bfd_seek (ibfd, pos, SEEK_SET) != 0
401 || bfd_bread (extsym_buf, amt, ibfd) != amt)
403 intsym_buf = NULL;
404 goto out;
407 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
408 extshndx_buf = NULL;
409 else
411 amt = symcount * sizeof (Elf_External_Sym_Shndx);
412 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
413 if (extshndx_buf == NULL)
415 alloc_extshndx = (Elf_External_Sym_Shndx *)
416 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
417 extshndx_buf = alloc_extshndx;
419 if (extshndx_buf == NULL
420 || bfd_seek (ibfd, pos, SEEK_SET) != 0
421 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
423 intsym_buf = NULL;
424 goto out;
428 if (intsym_buf == NULL)
430 alloc_intsym = (Elf_Internal_Sym *)
431 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
432 intsym_buf = alloc_intsym;
433 if (intsym_buf == NULL)
434 goto out;
437 /* Convert the symbols to internal form. */
438 isymend = intsym_buf + symcount;
439 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
440 shndx = extshndx_buf;
441 isym < isymend;
442 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
443 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
445 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
446 (*_bfd_error_handler) (_("%B symbol number %lu references "
447 "nonexistent SHT_SYMTAB_SHNDX section"),
448 ibfd, (unsigned long) symoffset);
449 if (alloc_intsym != NULL)
450 free (alloc_intsym);
451 intsym_buf = NULL;
452 goto out;
455 out:
456 if (alloc_ext != NULL)
457 free (alloc_ext);
458 if (alloc_extshndx != NULL)
459 free (alloc_extshndx);
461 return intsym_buf;
464 /* Look up a symbol name. */
465 const char *
466 bfd_elf_sym_name (bfd *abfd,
467 Elf_Internal_Shdr *symtab_hdr,
468 Elf_Internal_Sym *isym,
469 asection *sym_sec)
471 const char *name;
472 unsigned int iname = isym->st_name;
473 unsigned int shindex = symtab_hdr->sh_link;
475 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
476 /* Check for a bogus st_shndx to avoid crashing. */
477 && isym->st_shndx < elf_numsections (abfd))
479 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
480 shindex = elf_elfheader (abfd)->e_shstrndx;
483 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
484 if (name == NULL)
485 name = "(null)";
486 else if (sym_sec && *name == '\0')
487 name = bfd_section_name (abfd, sym_sec);
489 return name;
492 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
493 sections. The first element is the flags, the rest are section
494 pointers. */
496 typedef union elf_internal_group {
497 Elf_Internal_Shdr *shdr;
498 unsigned int flags;
499 } Elf_Internal_Group;
501 /* Return the name of the group signature symbol. Why isn't the
502 signature just a string? */
504 static const char *
505 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
507 Elf_Internal_Shdr *hdr;
508 unsigned char esym[sizeof (Elf64_External_Sym)];
509 Elf_External_Sym_Shndx eshndx;
510 Elf_Internal_Sym isym;
512 /* First we need to ensure the symbol table is available. Make sure
513 that it is a symbol table section. */
514 if (ghdr->sh_link >= elf_numsections (abfd))
515 return NULL;
516 hdr = elf_elfsections (abfd) [ghdr->sh_link];
517 if (hdr->sh_type != SHT_SYMTAB
518 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
519 return NULL;
521 /* Go read the symbol. */
522 hdr = &elf_tdata (abfd)->symtab_hdr;
523 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
524 &isym, esym, &eshndx) == NULL)
525 return NULL;
527 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
530 /* Set next_in_group list pointer, and group name for NEWSECT. */
532 static bfd_boolean
533 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
535 unsigned int num_group = elf_tdata (abfd)->num_group;
537 /* If num_group is zero, read in all SHT_GROUP sections. The count
538 is set to -1 if there are no SHT_GROUP sections. */
539 if (num_group == 0)
541 unsigned int i, shnum;
543 /* First count the number of groups. If we have a SHT_GROUP
544 section with just a flag word (ie. sh_size is 4), ignore it. */
545 shnum = elf_numsections (abfd);
546 num_group = 0;
548 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
549 ( (shdr)->sh_type == SHT_GROUP \
550 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
551 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
552 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
554 for (i = 0; i < shnum; i++)
556 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
558 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
559 num_group += 1;
562 if (num_group == 0)
564 num_group = (unsigned) -1;
565 elf_tdata (abfd)->num_group = num_group;
567 else
569 /* We keep a list of elf section headers for group sections,
570 so we can find them quickly. */
571 bfd_size_type amt;
573 elf_tdata (abfd)->num_group = num_group;
574 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
575 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
576 if (elf_tdata (abfd)->group_sect_ptr == NULL)
577 return FALSE;
579 num_group = 0;
580 for (i = 0; i < shnum; i++)
582 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
584 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
586 unsigned char *src;
587 Elf_Internal_Group *dest;
589 /* Add to list of sections. */
590 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
591 num_group += 1;
593 /* Read the raw contents. */
594 BFD_ASSERT (sizeof (*dest) >= 4);
595 amt = shdr->sh_size * sizeof (*dest) / 4;
596 shdr->contents = (unsigned char *)
597 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
598 /* PR binutils/4110: Handle corrupt group headers. */
599 if (shdr->contents == NULL)
601 _bfd_error_handler
602 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
603 bfd_set_error (bfd_error_bad_value);
604 return FALSE;
607 memset (shdr->contents, 0, amt);
609 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
610 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
611 != shdr->sh_size))
612 return FALSE;
614 /* Translate raw contents, a flag word followed by an
615 array of elf section indices all in target byte order,
616 to the flag word followed by an array of elf section
617 pointers. */
618 src = shdr->contents + shdr->sh_size;
619 dest = (Elf_Internal_Group *) (shdr->contents + amt);
620 while (1)
622 unsigned int idx;
624 src -= 4;
625 --dest;
626 idx = H_GET_32 (abfd, src);
627 if (src == shdr->contents)
629 dest->flags = idx;
630 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
631 shdr->bfd_section->flags
632 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
633 break;
635 if (idx >= shnum)
637 ((*_bfd_error_handler)
638 (_("%B: invalid SHT_GROUP entry"), abfd));
639 idx = 0;
641 dest->shdr = elf_elfsections (abfd)[idx];
648 if (num_group != (unsigned) -1)
650 unsigned int i;
652 for (i = 0; i < num_group; i++)
654 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
655 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
656 unsigned int n_elt = shdr->sh_size / 4;
658 /* Look through this group's sections to see if current
659 section is a member. */
660 while (--n_elt != 0)
661 if ((++idx)->shdr == hdr)
663 asection *s = NULL;
665 /* We are a member of this group. Go looking through
666 other members to see if any others are linked via
667 next_in_group. */
668 idx = (Elf_Internal_Group *) shdr->contents;
669 n_elt = shdr->sh_size / 4;
670 while (--n_elt != 0)
671 if ((s = (++idx)->shdr->bfd_section) != NULL
672 && elf_next_in_group (s) != NULL)
673 break;
674 if (n_elt != 0)
676 /* Snarf the group name from other member, and
677 insert current section in circular list. */
678 elf_group_name (newsect) = elf_group_name (s);
679 elf_next_in_group (newsect) = elf_next_in_group (s);
680 elf_next_in_group (s) = newsect;
682 else
684 const char *gname;
686 gname = group_signature (abfd, shdr);
687 if (gname == NULL)
688 return FALSE;
689 elf_group_name (newsect) = gname;
691 /* Start a circular list with one element. */
692 elf_next_in_group (newsect) = newsect;
695 /* If the group section has been created, point to the
696 new member. */
697 if (shdr->bfd_section != NULL)
698 elf_next_in_group (shdr->bfd_section) = newsect;
700 i = num_group - 1;
701 break;
706 if (elf_group_name (newsect) == NULL)
708 (*_bfd_error_handler) (_("%B: no group info for section %A"),
709 abfd, newsect);
711 return TRUE;
714 bfd_boolean
715 _bfd_elf_setup_sections (bfd *abfd)
717 unsigned int i;
718 unsigned int num_group = elf_tdata (abfd)->num_group;
719 bfd_boolean result = TRUE;
720 asection *s;
722 /* Process SHF_LINK_ORDER. */
723 for (s = abfd->sections; s != NULL; s = s->next)
725 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
726 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
728 unsigned int elfsec = this_hdr->sh_link;
729 /* FIXME: The old Intel compiler and old strip/objcopy may
730 not set the sh_link or sh_info fields. Hence we could
731 get the situation where elfsec is 0. */
732 if (elfsec == 0)
734 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
735 if (bed->link_order_error_handler)
736 bed->link_order_error_handler
737 (_("%B: warning: sh_link not set for section `%A'"),
738 abfd, s);
740 else
742 asection *linksec = NULL;
744 if (elfsec < elf_numsections (abfd))
746 this_hdr = elf_elfsections (abfd)[elfsec];
747 linksec = this_hdr->bfd_section;
750 /* PR 1991, 2008:
751 Some strip/objcopy may leave an incorrect value in
752 sh_link. We don't want to proceed. */
753 if (linksec == NULL)
755 (*_bfd_error_handler)
756 (_("%B: sh_link [%d] in section `%A' is incorrect"),
757 s->owner, s, elfsec);
758 result = FALSE;
761 elf_linked_to_section (s) = linksec;
766 /* Process section groups. */
767 if (num_group == (unsigned) -1)
768 return result;
770 for (i = 0; i < num_group; i++)
772 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
773 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
774 unsigned int n_elt = shdr->sh_size / 4;
776 while (--n_elt != 0)
777 if ((++idx)->shdr->bfd_section)
778 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
779 else if (idx->shdr->sh_type == SHT_RELA
780 || idx->shdr->sh_type == SHT_REL)
781 /* We won't include relocation sections in section groups in
782 output object files. We adjust the group section size here
783 so that relocatable link will work correctly when
784 relocation sections are in section group in input object
785 files. */
786 shdr->bfd_section->size -= 4;
787 else
789 /* There are some unknown sections in the group. */
790 (*_bfd_error_handler)
791 (_("%B: unknown [%d] section `%s' in group [%s]"),
792 abfd,
793 (unsigned int) idx->shdr->sh_type,
794 bfd_elf_string_from_elf_section (abfd,
795 (elf_elfheader (abfd)
796 ->e_shstrndx),
797 idx->shdr->sh_name),
798 shdr->bfd_section->name);
799 result = FALSE;
802 return result;
805 bfd_boolean
806 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
808 return elf_next_in_group (sec) != NULL;
811 /* Make a BFD section from an ELF section. We store a pointer to the
812 BFD section in the bfd_section field of the header. */
814 bfd_boolean
815 _bfd_elf_make_section_from_shdr (bfd *abfd,
816 Elf_Internal_Shdr *hdr,
817 const char *name,
818 int shindex)
820 asection *newsect;
821 flagword flags;
822 const struct elf_backend_data *bed;
824 if (hdr->bfd_section != NULL)
826 BFD_ASSERT (strcmp (name,
827 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
828 return TRUE;
831 newsect = bfd_make_section_anyway (abfd, name);
832 if (newsect == NULL)
833 return FALSE;
835 hdr->bfd_section = newsect;
836 elf_section_data (newsect)->this_hdr = *hdr;
837 elf_section_data (newsect)->this_idx = shindex;
839 /* Always use the real type/flags. */
840 elf_section_type (newsect) = hdr->sh_type;
841 elf_section_flags (newsect) = hdr->sh_flags;
843 newsect->filepos = hdr->sh_offset;
845 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
846 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
847 || ! bfd_set_section_alignment (abfd, newsect,
848 bfd_log2 (hdr->sh_addralign)))
849 return FALSE;
851 flags = SEC_NO_FLAGS;
852 if (hdr->sh_type != SHT_NOBITS)
853 flags |= SEC_HAS_CONTENTS;
854 if (hdr->sh_type == SHT_GROUP)
855 flags |= SEC_GROUP | SEC_EXCLUDE;
856 if ((hdr->sh_flags & SHF_ALLOC) != 0)
858 flags |= SEC_ALLOC;
859 if (hdr->sh_type != SHT_NOBITS)
860 flags |= SEC_LOAD;
862 if ((hdr->sh_flags & SHF_WRITE) == 0)
863 flags |= SEC_READONLY;
864 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
865 flags |= SEC_CODE;
866 else if ((flags & SEC_LOAD) != 0)
867 flags |= SEC_DATA;
868 if ((hdr->sh_flags & SHF_MERGE) != 0)
870 flags |= SEC_MERGE;
871 newsect->entsize = hdr->sh_entsize;
872 if ((hdr->sh_flags & SHF_STRINGS) != 0)
873 flags |= SEC_STRINGS;
875 if (hdr->sh_flags & SHF_GROUP)
876 if (!setup_group (abfd, hdr, newsect))
877 return FALSE;
878 if ((hdr->sh_flags & SHF_TLS) != 0)
879 flags |= SEC_THREAD_LOCAL;
880 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
881 flags |= SEC_EXCLUDE;
883 if ((flags & SEC_ALLOC) == 0)
885 /* The debugging sections appear to be recognized only by name,
886 not any sort of flag. Their SEC_ALLOC bits are cleared. */
887 static const struct
889 const char *name;
890 int len;
891 } debug_sections [] =
893 { STRING_COMMA_LEN ("debug") }, /* 'd' */
894 { NULL, 0 }, /* 'e' */
895 { NULL, 0 }, /* 'f' */
896 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
897 { NULL, 0 }, /* 'h' */
898 { NULL, 0 }, /* 'i' */
899 { NULL, 0 }, /* 'j' */
900 { NULL, 0 }, /* 'k' */
901 { STRING_COMMA_LEN ("line") }, /* 'l' */
902 { NULL, 0 }, /* 'm' */
903 { NULL, 0 }, /* 'n' */
904 { NULL, 0 }, /* 'o' */
905 { NULL, 0 }, /* 'p' */
906 { NULL, 0 }, /* 'q' */
907 { NULL, 0 }, /* 'r' */
908 { STRING_COMMA_LEN ("stab") }, /* 's' */
909 { NULL, 0 }, /* 't' */
910 { NULL, 0 }, /* 'u' */
911 { NULL, 0 }, /* 'v' */
912 { NULL, 0 }, /* 'w' */
913 { NULL, 0 }, /* 'x' */
914 { NULL, 0 }, /* 'y' */
915 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
918 if (name [0] == '.')
920 int i = name [1] - 'd';
921 if (i >= 0
922 && i < (int) ARRAY_SIZE (debug_sections)
923 && debug_sections [i].name != NULL
924 && strncmp (&name [1], debug_sections [i].name,
925 debug_sections [i].len) == 0)
926 flags |= SEC_DEBUGGING;
930 /* As a GNU extension, if the name begins with .gnu.linkonce, we
931 only link a single copy of the section. This is used to support
932 g++. g++ will emit each template expansion in its own section.
933 The symbols will be defined as weak, so that multiple definitions
934 are permitted. The GNU linker extension is to actually discard
935 all but one of the sections. */
936 if (CONST_STRNEQ (name, ".gnu.linkonce")
937 && elf_next_in_group (newsect) == NULL)
938 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
940 bed = get_elf_backend_data (abfd);
941 if (bed->elf_backend_section_flags)
942 if (! bed->elf_backend_section_flags (&flags, hdr))
943 return FALSE;
945 if (! bfd_set_section_flags (abfd, newsect, flags))
946 return FALSE;
948 /* We do not parse the PT_NOTE segments as we are interested even in the
949 separate debug info files which may have the segments offsets corrupted.
950 PT_NOTEs from the core files are currently not parsed using BFD. */
951 if (hdr->sh_type == SHT_NOTE)
953 bfd_byte *contents;
955 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
956 return FALSE;
958 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
959 free (contents);
962 if ((flags & SEC_ALLOC) != 0)
964 Elf_Internal_Phdr *phdr;
965 unsigned int i, nload;
967 /* Some ELF linkers produce binaries with all the program header
968 p_paddr fields zero. If we have such a binary with more than
969 one PT_LOAD header, then leave the section lma equal to vma
970 so that we don't create sections with overlapping lma. */
971 phdr = elf_tdata (abfd)->phdr;
972 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
973 if (phdr->p_paddr != 0)
974 break;
975 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
976 ++nload;
977 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
978 return TRUE;
980 phdr = elf_tdata (abfd)->phdr;
981 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
983 if (phdr->p_type == PT_LOAD
984 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
986 if ((flags & SEC_LOAD) == 0)
987 newsect->lma = (phdr->p_paddr
988 + hdr->sh_addr - phdr->p_vaddr);
989 else
990 /* We used to use the same adjustment for SEC_LOAD
991 sections, but that doesn't work if the segment
992 is packed with code from multiple VMAs.
993 Instead we calculate the section LMA based on
994 the segment LMA. It is assumed that the
995 segment will contain sections with contiguous
996 LMAs, even if the VMAs are not. */
997 newsect->lma = (phdr->p_paddr
998 + hdr->sh_offset - phdr->p_offset);
1000 /* With contiguous segments, we can't tell from file
1001 offsets whether a section with zero size should
1002 be placed at the end of one segment or the
1003 beginning of the next. Decide based on vaddr. */
1004 if (hdr->sh_addr >= phdr->p_vaddr
1005 && (hdr->sh_addr + hdr->sh_size
1006 <= phdr->p_vaddr + phdr->p_memsz))
1007 break;
1012 /* Compress/decompress DWARF debug sections with names: .debug_* and
1013 .zdebug_*, after the section flags is set. */
1014 if ((flags & SEC_DEBUGGING)
1015 && ((name[1] == 'd' && name[6] == '_')
1016 || (name[1] == 'z' && name[7] == '_')))
1018 enum { nothing, compress, decompress } action = nothing;
1020 if (bfd_is_section_compressed (abfd, newsect))
1022 /* Compressed section. Check if we should decompress. */
1023 if ((abfd->flags & BFD_DECOMPRESS))
1024 action = decompress;
1026 else
1028 /* Normal section. Check if we should compress. */
1029 if ((abfd->flags & BFD_COMPRESS))
1030 action = compress;
1033 switch (action)
1035 case nothing:
1036 break;
1037 case compress:
1038 if (!bfd_init_section_compress_status (abfd, newsect))
1040 (*_bfd_error_handler)
1041 (_("%B: unable to initialize commpress status for section %s"),
1042 abfd, name);
1043 return FALSE;
1045 break;
1046 case decompress:
1047 if (!bfd_init_section_decompress_status (abfd, newsect))
1049 (*_bfd_error_handler)
1050 (_("%B: unable to initialize decommpress status for section %s"),
1051 abfd, name);
1052 return FALSE;
1054 break;
1058 return TRUE;
1061 const char *const bfd_elf_section_type_names[] = {
1062 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1063 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1064 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1067 /* ELF relocs are against symbols. If we are producing relocatable
1068 output, and the reloc is against an external symbol, and nothing
1069 has given us any additional addend, the resulting reloc will also
1070 be against the same symbol. In such a case, we don't want to
1071 change anything about the way the reloc is handled, since it will
1072 all be done at final link time. Rather than put special case code
1073 into bfd_perform_relocation, all the reloc types use this howto
1074 function. It just short circuits the reloc if producing
1075 relocatable output against an external symbol. */
1077 bfd_reloc_status_type
1078 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1079 arelent *reloc_entry,
1080 asymbol *symbol,
1081 void *data ATTRIBUTE_UNUSED,
1082 asection *input_section,
1083 bfd *output_bfd,
1084 char **error_message ATTRIBUTE_UNUSED)
1086 if (output_bfd != NULL
1087 && (symbol->flags & BSF_SECTION_SYM) == 0
1088 && (! reloc_entry->howto->partial_inplace
1089 || reloc_entry->addend == 0))
1091 reloc_entry->address += input_section->output_offset;
1092 return bfd_reloc_ok;
1095 return bfd_reloc_continue;
1098 /* Copy the program header and other data from one object module to
1099 another. */
1101 bfd_boolean
1102 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1104 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1105 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1106 return TRUE;
1108 BFD_ASSERT (!elf_flags_init (obfd)
1109 || (elf_elfheader (obfd)->e_flags
1110 == elf_elfheader (ibfd)->e_flags));
1112 elf_gp (obfd) = elf_gp (ibfd);
1113 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1114 elf_flags_init (obfd) = TRUE;
1116 /* Copy object attributes. */
1117 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1118 return TRUE;
1121 static const char *
1122 get_segment_type (unsigned int p_type)
1124 const char *pt;
1125 switch (p_type)
1127 case PT_NULL: pt = "NULL"; break;
1128 case PT_LOAD: pt = "LOAD"; break;
1129 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1130 case PT_INTERP: pt = "INTERP"; break;
1131 case PT_NOTE: pt = "NOTE"; break;
1132 case PT_SHLIB: pt = "SHLIB"; break;
1133 case PT_PHDR: pt = "PHDR"; break;
1134 case PT_TLS: pt = "TLS"; break;
1135 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1136 case PT_GNU_STACK: pt = "STACK"; break;
1137 case PT_GNU_RELRO: pt = "RELRO"; break;
1138 default: pt = NULL; break;
1140 return pt;
1143 /* Print out the program headers. */
1145 bfd_boolean
1146 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1148 FILE *f = (FILE *) farg;
1149 Elf_Internal_Phdr *p;
1150 asection *s;
1151 bfd_byte *dynbuf = NULL;
1153 p = elf_tdata (abfd)->phdr;
1154 if (p != NULL)
1156 unsigned int i, c;
1158 fprintf (f, _("\nProgram Header:\n"));
1159 c = elf_elfheader (abfd)->e_phnum;
1160 for (i = 0; i < c; i++, p++)
1162 const char *pt = get_segment_type (p->p_type);
1163 char buf[20];
1165 if (pt == NULL)
1167 sprintf (buf, "0x%lx", p->p_type);
1168 pt = buf;
1170 fprintf (f, "%8s off 0x", pt);
1171 bfd_fprintf_vma (abfd, f, p->p_offset);
1172 fprintf (f, " vaddr 0x");
1173 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1174 fprintf (f, " paddr 0x");
1175 bfd_fprintf_vma (abfd, f, p->p_paddr);
1176 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1177 fprintf (f, " filesz 0x");
1178 bfd_fprintf_vma (abfd, f, p->p_filesz);
1179 fprintf (f, " memsz 0x");
1180 bfd_fprintf_vma (abfd, f, p->p_memsz);
1181 fprintf (f, " flags %c%c%c",
1182 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1183 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1184 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1185 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1186 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1187 fprintf (f, "\n");
1191 s = bfd_get_section_by_name (abfd, ".dynamic");
1192 if (s != NULL)
1194 unsigned int elfsec;
1195 unsigned long shlink;
1196 bfd_byte *extdyn, *extdynend;
1197 size_t extdynsize;
1198 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1200 fprintf (f, _("\nDynamic Section:\n"));
1202 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1203 goto error_return;
1205 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1206 if (elfsec == SHN_BAD)
1207 goto error_return;
1208 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1210 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1211 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1213 extdyn = dynbuf;
1214 extdynend = extdyn + s->size;
1215 for (; extdyn < extdynend; extdyn += extdynsize)
1217 Elf_Internal_Dyn dyn;
1218 const char *name = "";
1219 char ab[20];
1220 bfd_boolean stringp;
1221 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1223 (*swap_dyn_in) (abfd, extdyn, &dyn);
1225 if (dyn.d_tag == DT_NULL)
1226 break;
1228 stringp = FALSE;
1229 switch (dyn.d_tag)
1231 default:
1232 if (bed->elf_backend_get_target_dtag)
1233 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1235 if (!strcmp (name, ""))
1237 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1238 name = ab;
1240 break;
1242 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1243 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1244 case DT_PLTGOT: name = "PLTGOT"; break;
1245 case DT_HASH: name = "HASH"; break;
1246 case DT_STRTAB: name = "STRTAB"; break;
1247 case DT_SYMTAB: name = "SYMTAB"; break;
1248 case DT_RELA: name = "RELA"; break;
1249 case DT_RELASZ: name = "RELASZ"; break;
1250 case DT_RELAENT: name = "RELAENT"; break;
1251 case DT_STRSZ: name = "STRSZ"; break;
1252 case DT_SYMENT: name = "SYMENT"; break;
1253 case DT_INIT: name = "INIT"; break;
1254 case DT_FINI: name = "FINI"; break;
1255 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1256 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1257 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1258 case DT_REL: name = "REL"; break;
1259 case DT_RELSZ: name = "RELSZ"; break;
1260 case DT_RELENT: name = "RELENT"; break;
1261 case DT_PLTREL: name = "PLTREL"; break;
1262 case DT_DEBUG: name = "DEBUG"; break;
1263 case DT_TEXTREL: name = "TEXTREL"; break;
1264 case DT_JMPREL: name = "JMPREL"; break;
1265 case DT_BIND_NOW: name = "BIND_NOW"; break;
1266 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1267 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1268 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1269 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1270 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1271 case DT_FLAGS: name = "FLAGS"; break;
1272 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1273 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1274 case DT_CHECKSUM: name = "CHECKSUM"; break;
1275 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1276 case DT_MOVEENT: name = "MOVEENT"; break;
1277 case DT_MOVESZ: name = "MOVESZ"; break;
1278 case DT_FEATURE: name = "FEATURE"; break;
1279 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1280 case DT_SYMINSZ: name = "SYMINSZ"; break;
1281 case DT_SYMINENT: name = "SYMINENT"; break;
1282 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1283 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1284 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1285 case DT_PLTPAD: name = "PLTPAD"; break;
1286 case DT_MOVETAB: name = "MOVETAB"; break;
1287 case DT_SYMINFO: name = "SYMINFO"; break;
1288 case DT_RELACOUNT: name = "RELACOUNT"; break;
1289 case DT_RELCOUNT: name = "RELCOUNT"; break;
1290 case DT_FLAGS_1: name = "FLAGS_1"; break;
1291 case DT_VERSYM: name = "VERSYM"; break;
1292 case DT_VERDEF: name = "VERDEF"; break;
1293 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1294 case DT_VERNEED: name = "VERNEED"; break;
1295 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1296 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1297 case DT_USED: name = "USED"; break;
1298 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1299 case DT_GNU_HASH: name = "GNU_HASH"; break;
1302 fprintf (f, " %-20s ", name);
1303 if (! stringp)
1305 fprintf (f, "0x");
1306 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1308 else
1310 const char *string;
1311 unsigned int tagv = dyn.d_un.d_val;
1313 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1314 if (string == NULL)
1315 goto error_return;
1316 fprintf (f, "%s", string);
1318 fprintf (f, "\n");
1321 free (dynbuf);
1322 dynbuf = NULL;
1325 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1326 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1328 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1329 return FALSE;
1332 if (elf_dynverdef (abfd) != 0)
1334 Elf_Internal_Verdef *t;
1336 fprintf (f, _("\nVersion definitions:\n"));
1337 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1339 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1340 t->vd_flags, t->vd_hash,
1341 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1342 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1344 Elf_Internal_Verdaux *a;
1346 fprintf (f, "\t");
1347 for (a = t->vd_auxptr->vda_nextptr;
1348 a != NULL;
1349 a = a->vda_nextptr)
1350 fprintf (f, "%s ",
1351 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1352 fprintf (f, "\n");
1357 if (elf_dynverref (abfd) != 0)
1359 Elf_Internal_Verneed *t;
1361 fprintf (f, _("\nVersion References:\n"));
1362 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1364 Elf_Internal_Vernaux *a;
1366 fprintf (f, _(" required from %s:\n"),
1367 t->vn_filename ? t->vn_filename : "<corrupt>");
1368 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1369 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1370 a->vna_flags, a->vna_other,
1371 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1375 return TRUE;
1377 error_return:
1378 if (dynbuf != NULL)
1379 free (dynbuf);
1380 return FALSE;
1383 /* Display ELF-specific fields of a symbol. */
1385 void
1386 bfd_elf_print_symbol (bfd *abfd,
1387 void *filep,
1388 asymbol *symbol,
1389 bfd_print_symbol_type how)
1391 FILE *file = (FILE *) filep;
1392 switch (how)
1394 case bfd_print_symbol_name:
1395 fprintf (file, "%s", symbol->name);
1396 break;
1397 case bfd_print_symbol_more:
1398 fprintf (file, "elf ");
1399 bfd_fprintf_vma (abfd, file, symbol->value);
1400 fprintf (file, " %lx", (unsigned long) symbol->flags);
1401 break;
1402 case bfd_print_symbol_all:
1404 const char *section_name;
1405 const char *name = NULL;
1406 const struct elf_backend_data *bed;
1407 unsigned char st_other;
1408 bfd_vma val;
1410 section_name = symbol->section ? symbol->section->name : "(*none*)";
1412 bed = get_elf_backend_data (abfd);
1413 if (bed->elf_backend_print_symbol_all)
1414 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1416 if (name == NULL)
1418 name = symbol->name;
1419 bfd_print_symbol_vandf (abfd, file, symbol);
1422 fprintf (file, " %s\t", section_name);
1423 /* Print the "other" value for a symbol. For common symbols,
1424 we've already printed the size; now print the alignment.
1425 For other symbols, we have no specified alignment, and
1426 we've printed the address; now print the size. */
1427 if (symbol->section && bfd_is_com_section (symbol->section))
1428 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1429 else
1430 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1431 bfd_fprintf_vma (abfd, file, val);
1433 /* If we have version information, print it. */
1434 if (elf_tdata (abfd)->dynversym_section != 0
1435 && (elf_tdata (abfd)->dynverdef_section != 0
1436 || elf_tdata (abfd)->dynverref_section != 0))
1438 unsigned int vernum;
1439 const char *version_string;
1441 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1443 if (vernum == 0)
1444 version_string = "";
1445 else if (vernum == 1)
1446 version_string = "Base";
1447 else if (vernum <= elf_tdata (abfd)->cverdefs)
1448 version_string =
1449 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1450 else
1452 Elf_Internal_Verneed *t;
1454 version_string = "";
1455 for (t = elf_tdata (abfd)->verref;
1456 t != NULL;
1457 t = t->vn_nextref)
1459 Elf_Internal_Vernaux *a;
1461 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1463 if (a->vna_other == vernum)
1465 version_string = a->vna_nodename;
1466 break;
1472 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1473 fprintf (file, " %-11s", version_string);
1474 else
1476 int i;
1478 fprintf (file, " (%s)", version_string);
1479 for (i = 10 - strlen (version_string); i > 0; --i)
1480 putc (' ', file);
1484 /* If the st_other field is not zero, print it. */
1485 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1487 switch (st_other)
1489 case 0: break;
1490 case STV_INTERNAL: fprintf (file, " .internal"); break;
1491 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1492 case STV_PROTECTED: fprintf (file, " .protected"); break;
1493 default:
1494 /* Some other non-defined flags are also present, so print
1495 everything hex. */
1496 fprintf (file, " 0x%02x", (unsigned int) st_other);
1499 fprintf (file, " %s", name);
1501 break;
1505 /* Allocate an ELF string table--force the first byte to be zero. */
1507 struct bfd_strtab_hash *
1508 _bfd_elf_stringtab_init (void)
1510 struct bfd_strtab_hash *ret;
1512 ret = _bfd_stringtab_init ();
1513 if (ret != NULL)
1515 bfd_size_type loc;
1517 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1518 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1519 if (loc == (bfd_size_type) -1)
1521 _bfd_stringtab_free (ret);
1522 ret = NULL;
1525 return ret;
1528 /* ELF .o/exec file reading */
1530 /* Create a new bfd section from an ELF section header. */
1532 bfd_boolean
1533 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1535 Elf_Internal_Shdr *hdr;
1536 Elf_Internal_Ehdr *ehdr;
1537 const struct elf_backend_data *bed;
1538 const char *name;
1540 if (shindex >= elf_numsections (abfd))
1541 return FALSE;
1543 hdr = elf_elfsections (abfd)[shindex];
1544 ehdr = elf_elfheader (abfd);
1545 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1546 hdr->sh_name);
1547 if (name == NULL)
1548 return FALSE;
1550 bed = get_elf_backend_data (abfd);
1551 switch (hdr->sh_type)
1553 case SHT_NULL:
1554 /* Inactive section. Throw it away. */
1555 return TRUE;
1557 case SHT_PROGBITS: /* Normal section with contents. */
1558 case SHT_NOBITS: /* .bss section. */
1559 case SHT_HASH: /* .hash section. */
1560 case SHT_NOTE: /* .note section. */
1561 case SHT_INIT_ARRAY: /* .init_array section. */
1562 case SHT_FINI_ARRAY: /* .fini_array section. */
1563 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1564 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1565 case SHT_GNU_HASH: /* .gnu.hash section. */
1566 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1568 case SHT_DYNAMIC: /* Dynamic linking information. */
1569 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1570 return FALSE;
1571 if (hdr->sh_link > elf_numsections (abfd))
1573 /* PR 10478: Accept Solaris binaries with a sh_link
1574 field set to SHN_BEFORE or SHN_AFTER. */
1575 switch (bfd_get_arch (abfd))
1577 case bfd_arch_i386:
1578 case bfd_arch_sparc:
1579 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1580 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1581 break;
1582 /* Otherwise fall through. */
1583 default:
1584 return FALSE;
1587 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1588 return FALSE;
1589 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1591 Elf_Internal_Shdr *dynsymhdr;
1593 /* The shared libraries distributed with hpux11 have a bogus
1594 sh_link field for the ".dynamic" section. Find the
1595 string table for the ".dynsym" section instead. */
1596 if (elf_dynsymtab (abfd) != 0)
1598 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1599 hdr->sh_link = dynsymhdr->sh_link;
1601 else
1603 unsigned int i, num_sec;
1605 num_sec = elf_numsections (abfd);
1606 for (i = 1; i < num_sec; i++)
1608 dynsymhdr = elf_elfsections (abfd)[i];
1609 if (dynsymhdr->sh_type == SHT_DYNSYM)
1611 hdr->sh_link = dynsymhdr->sh_link;
1612 break;
1617 break;
1619 case SHT_SYMTAB: /* A symbol table */
1620 if (elf_onesymtab (abfd) == shindex)
1621 return TRUE;
1623 if (hdr->sh_entsize != bed->s->sizeof_sym)
1624 return FALSE;
1625 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1626 return FALSE;
1627 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1628 elf_onesymtab (abfd) = shindex;
1629 elf_tdata (abfd)->symtab_hdr = *hdr;
1630 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1631 abfd->flags |= HAS_SYMS;
1633 /* Sometimes a shared object will map in the symbol table. If
1634 SHF_ALLOC is set, and this is a shared object, then we also
1635 treat this section as a BFD section. We can not base the
1636 decision purely on SHF_ALLOC, because that flag is sometimes
1637 set in a relocatable object file, which would confuse the
1638 linker. */
1639 if ((hdr->sh_flags & SHF_ALLOC) != 0
1640 && (abfd->flags & DYNAMIC) != 0
1641 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1642 shindex))
1643 return FALSE;
1645 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1646 can't read symbols without that section loaded as well. It
1647 is most likely specified by the next section header. */
1648 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1650 unsigned int i, num_sec;
1652 num_sec = elf_numsections (abfd);
1653 for (i = shindex + 1; i < num_sec; i++)
1655 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1656 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1657 && hdr2->sh_link == shindex)
1658 break;
1660 if (i == num_sec)
1661 for (i = 1; i < shindex; i++)
1663 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1664 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1665 && hdr2->sh_link == shindex)
1666 break;
1668 if (i != shindex)
1669 return bfd_section_from_shdr (abfd, i);
1671 return TRUE;
1673 case SHT_DYNSYM: /* A dynamic symbol table */
1674 if (elf_dynsymtab (abfd) == shindex)
1675 return TRUE;
1677 if (hdr->sh_entsize != bed->s->sizeof_sym)
1678 return FALSE;
1679 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1680 elf_dynsymtab (abfd) = shindex;
1681 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1682 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1683 abfd->flags |= HAS_SYMS;
1685 /* Besides being a symbol table, we also treat this as a regular
1686 section, so that objcopy can handle it. */
1687 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1689 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1690 if (elf_symtab_shndx (abfd) == shindex)
1691 return TRUE;
1693 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1694 elf_symtab_shndx (abfd) = shindex;
1695 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1696 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1697 return TRUE;
1699 case SHT_STRTAB: /* A string table */
1700 if (hdr->bfd_section != NULL)
1701 return TRUE;
1702 if (ehdr->e_shstrndx == shindex)
1704 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1705 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1706 return TRUE;
1708 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1710 symtab_strtab:
1711 elf_tdata (abfd)->strtab_hdr = *hdr;
1712 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1713 return TRUE;
1715 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1717 dynsymtab_strtab:
1718 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1719 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1720 elf_elfsections (abfd)[shindex] = hdr;
1721 /* We also treat this as a regular section, so that objcopy
1722 can handle it. */
1723 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1724 shindex);
1727 /* If the string table isn't one of the above, then treat it as a
1728 regular section. We need to scan all the headers to be sure,
1729 just in case this strtab section appeared before the above. */
1730 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1732 unsigned int i, num_sec;
1734 num_sec = elf_numsections (abfd);
1735 for (i = 1; i < num_sec; i++)
1737 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1738 if (hdr2->sh_link == shindex)
1740 /* Prevent endless recursion on broken objects. */
1741 if (i == shindex)
1742 return FALSE;
1743 if (! bfd_section_from_shdr (abfd, i))
1744 return FALSE;
1745 if (elf_onesymtab (abfd) == i)
1746 goto symtab_strtab;
1747 if (elf_dynsymtab (abfd) == i)
1748 goto dynsymtab_strtab;
1752 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1754 case SHT_REL:
1755 case SHT_RELA:
1756 /* *These* do a lot of work -- but build no sections! */
1758 asection *target_sect;
1759 Elf_Internal_Shdr *hdr2, **p_hdr;
1760 unsigned int num_sec = elf_numsections (abfd);
1761 struct bfd_elf_section_data *esdt;
1762 bfd_size_type amt;
1764 if (hdr->sh_entsize
1765 != (bfd_size_type) (hdr->sh_type == SHT_REL
1766 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1767 return FALSE;
1769 /* Check for a bogus link to avoid crashing. */
1770 if (hdr->sh_link >= num_sec)
1772 ((*_bfd_error_handler)
1773 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1774 abfd, hdr->sh_link, name, shindex));
1775 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1776 shindex);
1779 /* For some incomprehensible reason Oracle distributes
1780 libraries for Solaris in which some of the objects have
1781 bogus sh_link fields. It would be nice if we could just
1782 reject them, but, unfortunately, some people need to use
1783 them. We scan through the section headers; if we find only
1784 one suitable symbol table, we clobber the sh_link to point
1785 to it. I hope this doesn't break anything.
1787 Don't do it on executable nor shared library. */
1788 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1789 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1790 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1792 unsigned int scan;
1793 int found;
1795 found = 0;
1796 for (scan = 1; scan < num_sec; scan++)
1798 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1799 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1801 if (found != 0)
1803 found = 0;
1804 break;
1806 found = scan;
1809 if (found != 0)
1810 hdr->sh_link = found;
1813 /* Get the symbol table. */
1814 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1815 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1816 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1817 return FALSE;
1819 /* If this reloc section does not use the main symbol table we
1820 don't treat it as a reloc section. BFD can't adequately
1821 represent such a section, so at least for now, we don't
1822 try. We just present it as a normal section. We also
1823 can't use it as a reloc section if it points to the null
1824 section, an invalid section, another reloc section, or its
1825 sh_link points to the null section. */
1826 if (hdr->sh_link != elf_onesymtab (abfd)
1827 || hdr->sh_link == SHN_UNDEF
1828 || hdr->sh_info == SHN_UNDEF
1829 || hdr->sh_info >= num_sec
1830 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1831 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1832 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1833 shindex);
1835 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1836 return FALSE;
1837 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1838 if (target_sect == NULL)
1839 return FALSE;
1841 esdt = elf_section_data (target_sect);
1842 if (hdr->sh_type == SHT_RELA)
1843 p_hdr = &esdt->rela.hdr;
1844 else
1845 p_hdr = &esdt->rel.hdr;
1847 BFD_ASSERT (*p_hdr == NULL);
1848 amt = sizeof (*hdr2);
1849 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1850 if (hdr2 == NULL)
1851 return FALSE;
1852 *hdr2 = *hdr;
1853 *p_hdr = hdr2;
1854 elf_elfsections (abfd)[shindex] = hdr2;
1855 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1856 target_sect->flags |= SEC_RELOC;
1857 target_sect->relocation = NULL;
1858 target_sect->rel_filepos = hdr->sh_offset;
1859 /* In the section to which the relocations apply, mark whether
1860 its relocations are of the REL or RELA variety. */
1861 if (hdr->sh_size != 0)
1863 if (hdr->sh_type == SHT_RELA)
1864 target_sect->use_rela_p = 1;
1866 abfd->flags |= HAS_RELOC;
1867 return TRUE;
1870 case SHT_GNU_verdef:
1871 elf_dynverdef (abfd) = shindex;
1872 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1873 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1875 case SHT_GNU_versym:
1876 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1877 return FALSE;
1878 elf_dynversym (abfd) = shindex;
1879 elf_tdata (abfd)->dynversym_hdr = *hdr;
1880 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1882 case SHT_GNU_verneed:
1883 elf_dynverref (abfd) = shindex;
1884 elf_tdata (abfd)->dynverref_hdr = *hdr;
1885 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1887 case SHT_SHLIB:
1888 return TRUE;
1890 case SHT_GROUP:
1891 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1892 return FALSE;
1893 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1894 return FALSE;
1895 if (hdr->contents != NULL)
1897 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1898 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1899 asection *s;
1901 if (idx->flags & GRP_COMDAT)
1902 hdr->bfd_section->flags
1903 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1905 /* We try to keep the same section order as it comes in. */
1906 idx += n_elt;
1907 while (--n_elt != 0)
1909 --idx;
1911 if (idx->shdr != NULL
1912 && (s = idx->shdr->bfd_section) != NULL
1913 && elf_next_in_group (s) != NULL)
1915 elf_next_in_group (hdr->bfd_section) = s;
1916 break;
1920 break;
1922 default:
1923 /* Possibly an attributes section. */
1924 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1925 || hdr->sh_type == bed->obj_attrs_section_type)
1927 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1928 return FALSE;
1929 _bfd_elf_parse_attributes (abfd, hdr);
1930 return TRUE;
1933 /* Check for any processor-specific section types. */
1934 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1935 return TRUE;
1937 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1939 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1940 /* FIXME: How to properly handle allocated section reserved
1941 for applications? */
1942 (*_bfd_error_handler)
1943 (_("%B: don't know how to handle allocated, application "
1944 "specific section `%s' [0x%8x]"),
1945 abfd, name, hdr->sh_type);
1946 else
1947 /* Allow sections reserved for applications. */
1948 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1949 shindex);
1951 else if (hdr->sh_type >= SHT_LOPROC
1952 && hdr->sh_type <= SHT_HIPROC)
1953 /* FIXME: We should handle this section. */
1954 (*_bfd_error_handler)
1955 (_("%B: don't know how to handle processor specific section "
1956 "`%s' [0x%8x]"),
1957 abfd, name, hdr->sh_type);
1958 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1960 /* Unrecognised OS-specific sections. */
1961 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1962 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1963 required to correctly process the section and the file should
1964 be rejected with an error message. */
1965 (*_bfd_error_handler)
1966 (_("%B: don't know how to handle OS specific section "
1967 "`%s' [0x%8x]"),
1968 abfd, name, hdr->sh_type);
1969 else
1970 /* Otherwise it should be processed. */
1971 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1973 else
1974 /* FIXME: We should handle this section. */
1975 (*_bfd_error_handler)
1976 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1977 abfd, name, hdr->sh_type);
1979 return FALSE;
1982 return TRUE;
1985 /* Return the local symbol specified by ABFD, R_SYMNDX. */
1987 Elf_Internal_Sym *
1988 bfd_sym_from_r_symndx (struct sym_cache *cache,
1989 bfd *abfd,
1990 unsigned long r_symndx)
1992 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1994 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1996 Elf_Internal_Shdr *symtab_hdr;
1997 unsigned char esym[sizeof (Elf64_External_Sym)];
1998 Elf_External_Sym_Shndx eshndx;
2000 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2001 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2002 &cache->sym[ent], esym, &eshndx) == NULL)
2003 return NULL;
2005 if (cache->abfd != abfd)
2007 memset (cache->indx, -1, sizeof (cache->indx));
2008 cache->abfd = abfd;
2010 cache->indx[ent] = r_symndx;
2013 return &cache->sym[ent];
2016 /* Given an ELF section number, retrieve the corresponding BFD
2017 section. */
2019 asection *
2020 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2022 if (sec_index >= elf_numsections (abfd))
2023 return NULL;
2024 return elf_elfsections (abfd)[sec_index]->bfd_section;
2027 static const struct bfd_elf_special_section special_sections_b[] =
2029 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2030 { NULL, 0, 0, 0, 0 }
2033 static const struct bfd_elf_special_section special_sections_c[] =
2035 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2036 { NULL, 0, 0, 0, 0 }
2039 static const struct bfd_elf_special_section special_sections_d[] =
2041 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2042 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2043 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2044 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2045 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2046 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2047 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2048 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2049 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2050 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2051 { NULL, 0, 0, 0, 0 }
2054 static const struct bfd_elf_special_section special_sections_f[] =
2056 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2057 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2058 { NULL, 0, 0, 0, 0 }
2061 static const struct bfd_elf_special_section special_sections_g[] =
2063 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2064 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2065 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2066 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2067 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2068 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2069 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2070 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2071 { NULL, 0, 0, 0, 0 }
2074 static const struct bfd_elf_special_section special_sections_h[] =
2076 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2077 { NULL, 0, 0, 0, 0 }
2080 static const struct bfd_elf_special_section special_sections_i[] =
2082 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2083 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2084 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2085 { NULL, 0, 0, 0, 0 }
2088 static const struct bfd_elf_special_section special_sections_l[] =
2090 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2091 { NULL, 0, 0, 0, 0 }
2094 static const struct bfd_elf_special_section special_sections_n[] =
2096 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2097 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2098 { NULL, 0, 0, 0, 0 }
2101 static const struct bfd_elf_special_section special_sections_p[] =
2103 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2104 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2105 { NULL, 0, 0, 0, 0 }
2108 static const struct bfd_elf_special_section special_sections_r[] =
2110 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2111 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2112 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2113 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2114 { NULL, 0, 0, 0, 0 }
2117 static const struct bfd_elf_special_section special_sections_s[] =
2119 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2120 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2121 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2122 /* See struct bfd_elf_special_section declaration for the semantics of
2123 this special case where .prefix_length != strlen (.prefix). */
2124 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2125 { NULL, 0, 0, 0, 0 }
2128 static const struct bfd_elf_special_section special_sections_t[] =
2130 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2131 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2132 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2133 { NULL, 0, 0, 0, 0 }
2136 static const struct bfd_elf_special_section special_sections_z[] =
2138 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2139 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2140 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2141 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2142 { NULL, 0, 0, 0, 0 }
2145 static const struct bfd_elf_special_section *special_sections[] =
2147 special_sections_b, /* 'b' */
2148 special_sections_c, /* 'c' */
2149 special_sections_d, /* 'd' */
2150 NULL, /* 'e' */
2151 special_sections_f, /* 'f' */
2152 special_sections_g, /* 'g' */
2153 special_sections_h, /* 'h' */
2154 special_sections_i, /* 'i' */
2155 NULL, /* 'j' */
2156 NULL, /* 'k' */
2157 special_sections_l, /* 'l' */
2158 NULL, /* 'm' */
2159 special_sections_n, /* 'n' */
2160 NULL, /* 'o' */
2161 special_sections_p, /* 'p' */
2162 NULL, /* 'q' */
2163 special_sections_r, /* 'r' */
2164 special_sections_s, /* 's' */
2165 special_sections_t, /* 't' */
2166 NULL, /* 'u' */
2167 NULL, /* 'v' */
2168 NULL, /* 'w' */
2169 NULL, /* 'x' */
2170 NULL, /* 'y' */
2171 special_sections_z /* 'z' */
2174 const struct bfd_elf_special_section *
2175 _bfd_elf_get_special_section (const char *name,
2176 const struct bfd_elf_special_section *spec,
2177 unsigned int rela)
2179 int i;
2180 int len;
2182 len = strlen (name);
2184 for (i = 0; spec[i].prefix != NULL; i++)
2186 int suffix_len;
2187 int prefix_len = spec[i].prefix_length;
2189 if (len < prefix_len)
2190 continue;
2191 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2192 continue;
2194 suffix_len = spec[i].suffix_length;
2195 if (suffix_len <= 0)
2197 if (name[prefix_len] != 0)
2199 if (suffix_len == 0)
2200 continue;
2201 if (name[prefix_len] != '.'
2202 && (suffix_len == -2
2203 || (rela && spec[i].type == SHT_REL)))
2204 continue;
2207 else
2209 if (len < prefix_len + suffix_len)
2210 continue;
2211 if (memcmp (name + len - suffix_len,
2212 spec[i].prefix + prefix_len,
2213 suffix_len) != 0)
2214 continue;
2216 return &spec[i];
2219 return NULL;
2222 const struct bfd_elf_special_section *
2223 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2225 int i;
2226 const struct bfd_elf_special_section *spec;
2227 const struct elf_backend_data *bed;
2229 /* See if this is one of the special sections. */
2230 if (sec->name == NULL)
2231 return NULL;
2233 bed = get_elf_backend_data (abfd);
2234 spec = bed->special_sections;
2235 if (spec)
2237 spec = _bfd_elf_get_special_section (sec->name,
2238 bed->special_sections,
2239 sec->use_rela_p);
2240 if (spec != NULL)
2241 return spec;
2244 if (sec->name[0] != '.')
2245 return NULL;
2247 i = sec->name[1] - 'b';
2248 if (i < 0 || i > 'z' - 'b')
2249 return NULL;
2251 spec = special_sections[i];
2253 if (spec == NULL)
2254 return NULL;
2256 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2259 bfd_boolean
2260 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2262 struct bfd_elf_section_data *sdata;
2263 const struct elf_backend_data *bed;
2264 const struct bfd_elf_special_section *ssect;
2266 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2267 if (sdata == NULL)
2269 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2270 sizeof (*sdata));
2271 if (sdata == NULL)
2272 return FALSE;
2273 sec->used_by_bfd = sdata;
2276 /* Indicate whether or not this section should use RELA relocations. */
2277 bed = get_elf_backend_data (abfd);
2278 sec->use_rela_p = bed->default_use_rela_p;
2280 /* When we read a file, we don't need to set ELF section type and
2281 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2282 anyway. We will set ELF section type and flags for all linker
2283 created sections. If user specifies BFD section flags, we will
2284 set ELF section type and flags based on BFD section flags in
2285 elf_fake_sections. */
2286 if ((!sec->flags && abfd->direction != read_direction)
2287 || (sec->flags & SEC_LINKER_CREATED) != 0)
2289 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2290 if (ssect != NULL)
2292 elf_section_type (sec) = ssect->type;
2293 elf_section_flags (sec) = ssect->attr;
2297 return _bfd_generic_new_section_hook (abfd, sec);
2300 /* Create a new bfd section from an ELF program header.
2302 Since program segments have no names, we generate a synthetic name
2303 of the form segment<NUM>, where NUM is generally the index in the
2304 program header table. For segments that are split (see below) we
2305 generate the names segment<NUM>a and segment<NUM>b.
2307 Note that some program segments may have a file size that is different than
2308 (less than) the memory size. All this means is that at execution the
2309 system must allocate the amount of memory specified by the memory size,
2310 but only initialize it with the first "file size" bytes read from the
2311 file. This would occur for example, with program segments consisting
2312 of combined data+bss.
2314 To handle the above situation, this routine generates TWO bfd sections
2315 for the single program segment. The first has the length specified by
2316 the file size of the segment, and the second has the length specified
2317 by the difference between the two sizes. In effect, the segment is split
2318 into its initialized and uninitialized parts.
2322 bfd_boolean
2323 _bfd_elf_make_section_from_phdr (bfd *abfd,
2324 Elf_Internal_Phdr *hdr,
2325 int hdr_index,
2326 const char *type_name)
2328 asection *newsect;
2329 char *name;
2330 char namebuf[64];
2331 size_t len;
2332 int split;
2334 split = ((hdr->p_memsz > 0)
2335 && (hdr->p_filesz > 0)
2336 && (hdr->p_memsz > hdr->p_filesz));
2338 if (hdr->p_filesz > 0)
2340 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2341 len = strlen (namebuf) + 1;
2342 name = (char *) bfd_alloc (abfd, len);
2343 if (!name)
2344 return FALSE;
2345 memcpy (name, namebuf, len);
2346 newsect = bfd_make_section (abfd, name);
2347 if (newsect == NULL)
2348 return FALSE;
2349 newsect->vma = hdr->p_vaddr;
2350 newsect->lma = hdr->p_paddr;
2351 newsect->size = hdr->p_filesz;
2352 newsect->filepos = hdr->p_offset;
2353 newsect->flags |= SEC_HAS_CONTENTS;
2354 newsect->alignment_power = bfd_log2 (hdr->p_align);
2355 if (hdr->p_type == PT_LOAD)
2357 newsect->flags |= SEC_ALLOC;
2358 newsect->flags |= SEC_LOAD;
2359 if (hdr->p_flags & PF_X)
2361 /* FIXME: all we known is that it has execute PERMISSION,
2362 may be data. */
2363 newsect->flags |= SEC_CODE;
2366 if (!(hdr->p_flags & PF_W))
2368 newsect->flags |= SEC_READONLY;
2372 if (hdr->p_memsz > hdr->p_filesz)
2374 bfd_vma align;
2376 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2377 len = strlen (namebuf) + 1;
2378 name = (char *) bfd_alloc (abfd, len);
2379 if (!name)
2380 return FALSE;
2381 memcpy (name, namebuf, len);
2382 newsect = bfd_make_section (abfd, name);
2383 if (newsect == NULL)
2384 return FALSE;
2385 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2386 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2387 newsect->size = hdr->p_memsz - hdr->p_filesz;
2388 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2389 align = newsect->vma & -newsect->vma;
2390 if (align == 0 || align > hdr->p_align)
2391 align = hdr->p_align;
2392 newsect->alignment_power = bfd_log2 (align);
2393 if (hdr->p_type == PT_LOAD)
2395 /* Hack for gdb. Segments that have not been modified do
2396 not have their contents written to a core file, on the
2397 assumption that a debugger can find the contents in the
2398 executable. We flag this case by setting the fake
2399 section size to zero. Note that "real" bss sections will
2400 always have their contents dumped to the core file. */
2401 if (bfd_get_format (abfd) == bfd_core)
2402 newsect->size = 0;
2403 newsect->flags |= SEC_ALLOC;
2404 if (hdr->p_flags & PF_X)
2405 newsect->flags |= SEC_CODE;
2407 if (!(hdr->p_flags & PF_W))
2408 newsect->flags |= SEC_READONLY;
2411 return TRUE;
2414 bfd_boolean
2415 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2417 const struct elf_backend_data *bed;
2419 switch (hdr->p_type)
2421 case PT_NULL:
2422 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2424 case PT_LOAD:
2425 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2427 case PT_DYNAMIC:
2428 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2430 case PT_INTERP:
2431 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2433 case PT_NOTE:
2434 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2435 return FALSE;
2436 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2437 return FALSE;
2438 return TRUE;
2440 case PT_SHLIB:
2441 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2443 case PT_PHDR:
2444 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2446 case PT_GNU_EH_FRAME:
2447 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2448 "eh_frame_hdr");
2450 case PT_GNU_STACK:
2451 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2453 case PT_GNU_RELRO:
2454 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2456 default:
2457 /* Check for any processor-specific program segment types. */
2458 bed = get_elf_backend_data (abfd);
2459 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2463 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2464 REL or RELA. */
2466 Elf_Internal_Shdr *
2467 _bfd_elf_single_rel_hdr (asection *sec)
2469 if (elf_section_data (sec)->rel.hdr)
2471 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2472 return elf_section_data (sec)->rel.hdr;
2474 else
2475 return elf_section_data (sec)->rela.hdr;
2478 /* Allocate and initialize a section-header for a new reloc section,
2479 containing relocations against ASECT. It is stored in RELDATA. If
2480 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2481 relocations. */
2483 bfd_boolean
2484 _bfd_elf_init_reloc_shdr (bfd *abfd,
2485 struct bfd_elf_section_reloc_data *reldata,
2486 asection *asect,
2487 bfd_boolean use_rela_p)
2489 Elf_Internal_Shdr *rel_hdr;
2490 char *name;
2491 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2492 bfd_size_type amt;
2494 amt = sizeof (Elf_Internal_Shdr);
2495 BFD_ASSERT (reldata->hdr == NULL);
2496 rel_hdr = bfd_zalloc (abfd, amt);
2497 reldata->hdr = rel_hdr;
2499 amt = sizeof ".rela" + strlen (asect->name);
2500 name = (char *) bfd_alloc (abfd, amt);
2501 if (name == NULL)
2502 return FALSE;
2503 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2504 rel_hdr->sh_name =
2505 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2506 FALSE);
2507 if (rel_hdr->sh_name == (unsigned int) -1)
2508 return FALSE;
2509 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2510 rel_hdr->sh_entsize = (use_rela_p
2511 ? bed->s->sizeof_rela
2512 : bed->s->sizeof_rel);
2513 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2514 rel_hdr->sh_flags = 0;
2515 rel_hdr->sh_addr = 0;
2516 rel_hdr->sh_size = 0;
2517 rel_hdr->sh_offset = 0;
2519 return TRUE;
2522 /* Return the default section type based on the passed in section flags. */
2525 bfd_elf_get_default_section_type (flagword flags)
2527 if ((flags & SEC_ALLOC) != 0
2528 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2529 return SHT_NOBITS;
2530 return SHT_PROGBITS;
2533 struct fake_section_arg
2535 struct bfd_link_info *link_info;
2536 bfd_boolean failed;
2539 /* Set up an ELF internal section header for a section. */
2541 static void
2542 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2544 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2545 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2546 struct bfd_elf_section_data *esd = elf_section_data (asect);
2547 Elf_Internal_Shdr *this_hdr;
2548 unsigned int sh_type;
2550 if (arg->failed)
2552 /* We already failed; just get out of the bfd_map_over_sections
2553 loop. */
2554 return;
2557 this_hdr = &esd->this_hdr;
2559 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2560 asect->name, FALSE);
2561 if (this_hdr->sh_name == (unsigned int) -1)
2563 arg->failed = TRUE;
2564 return;
2567 /* Don't clear sh_flags. Assembler may set additional bits. */
2569 if ((asect->flags & SEC_ALLOC) != 0
2570 || asect->user_set_vma)
2571 this_hdr->sh_addr = asect->vma;
2572 else
2573 this_hdr->sh_addr = 0;
2575 this_hdr->sh_offset = 0;
2576 this_hdr->sh_size = asect->size;
2577 this_hdr->sh_link = 0;
2578 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2579 /* The sh_entsize and sh_info fields may have been set already by
2580 copy_private_section_data. */
2582 this_hdr->bfd_section = asect;
2583 this_hdr->contents = NULL;
2585 /* If the section type is unspecified, we set it based on
2586 asect->flags. */
2587 if ((asect->flags & SEC_GROUP) != 0)
2588 sh_type = SHT_GROUP;
2589 else
2590 sh_type = bfd_elf_get_default_section_type (asect->flags);
2592 if (this_hdr->sh_type == SHT_NULL)
2593 this_hdr->sh_type = sh_type;
2594 else if (this_hdr->sh_type == SHT_NOBITS
2595 && sh_type == SHT_PROGBITS
2596 && (asect->flags & SEC_ALLOC) != 0)
2598 /* Warn if we are changing a NOBITS section to PROGBITS, but
2599 allow the link to proceed. This can happen when users link
2600 non-bss input sections to bss output sections, or emit data
2601 to a bss output section via a linker script. */
2602 (*_bfd_error_handler)
2603 (_("warning: section `%A' type changed to PROGBITS"), asect);
2604 this_hdr->sh_type = sh_type;
2607 switch (this_hdr->sh_type)
2609 default:
2610 break;
2612 case SHT_STRTAB:
2613 case SHT_INIT_ARRAY:
2614 case SHT_FINI_ARRAY:
2615 case SHT_PREINIT_ARRAY:
2616 case SHT_NOTE:
2617 case SHT_NOBITS:
2618 case SHT_PROGBITS:
2619 break;
2621 case SHT_HASH:
2622 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2623 break;
2625 case SHT_DYNSYM:
2626 this_hdr->sh_entsize = bed->s->sizeof_sym;
2627 break;
2629 case SHT_DYNAMIC:
2630 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2631 break;
2633 case SHT_RELA:
2634 if (get_elf_backend_data (abfd)->may_use_rela_p)
2635 this_hdr->sh_entsize = bed->s->sizeof_rela;
2636 break;
2638 case SHT_REL:
2639 if (get_elf_backend_data (abfd)->may_use_rel_p)
2640 this_hdr->sh_entsize = bed->s->sizeof_rel;
2641 break;
2643 case SHT_GNU_versym:
2644 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2645 break;
2647 case SHT_GNU_verdef:
2648 this_hdr->sh_entsize = 0;
2649 /* objcopy or strip will copy over sh_info, but may not set
2650 cverdefs. The linker will set cverdefs, but sh_info will be
2651 zero. */
2652 if (this_hdr->sh_info == 0)
2653 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2654 else
2655 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2656 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2657 break;
2659 case SHT_GNU_verneed:
2660 this_hdr->sh_entsize = 0;
2661 /* objcopy or strip will copy over sh_info, but may not set
2662 cverrefs. The linker will set cverrefs, but sh_info will be
2663 zero. */
2664 if (this_hdr->sh_info == 0)
2665 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2666 else
2667 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2668 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2669 break;
2671 case SHT_GROUP:
2672 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2673 break;
2675 case SHT_GNU_HASH:
2676 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2677 break;
2680 if ((asect->flags & SEC_ALLOC) != 0)
2681 this_hdr->sh_flags |= SHF_ALLOC;
2682 if ((asect->flags & SEC_READONLY) == 0)
2683 this_hdr->sh_flags |= SHF_WRITE;
2684 if ((asect->flags & SEC_CODE) != 0)
2685 this_hdr->sh_flags |= SHF_EXECINSTR;
2686 if ((asect->flags & SEC_MERGE) != 0)
2688 this_hdr->sh_flags |= SHF_MERGE;
2689 this_hdr->sh_entsize = asect->entsize;
2690 if ((asect->flags & SEC_STRINGS) != 0)
2691 this_hdr->sh_flags |= SHF_STRINGS;
2693 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2694 this_hdr->sh_flags |= SHF_GROUP;
2695 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2697 this_hdr->sh_flags |= SHF_TLS;
2698 if (asect->size == 0
2699 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2701 struct bfd_link_order *o = asect->map_tail.link_order;
2703 this_hdr->sh_size = 0;
2704 if (o != NULL)
2706 this_hdr->sh_size = o->offset + o->size;
2707 if (this_hdr->sh_size != 0)
2708 this_hdr->sh_type = SHT_NOBITS;
2712 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2713 this_hdr->sh_flags |= SHF_EXCLUDE;
2715 /* If the section has relocs, set up a section header for the
2716 SHT_REL[A] section. If two relocation sections are required for
2717 this section, it is up to the processor-specific back-end to
2718 create the other. */
2719 if ((asect->flags & SEC_RELOC) != 0)
2721 /* When doing a relocatable link, create both REL and RELA sections if
2722 needed. */
2723 if (arg->link_info
2724 /* Do the normal setup if we wouldn't create any sections here. */
2725 && esd->rel.count + esd->rela.count > 0
2726 && (arg->link_info->relocatable || arg->link_info->emitrelocations))
2728 if (esd->rel.count && esd->rel.hdr == NULL
2729 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE))
2731 arg->failed = TRUE;
2732 return;
2734 if (esd->rela.count && esd->rela.hdr == NULL
2735 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE))
2737 arg->failed = TRUE;
2738 return;
2741 else if (!_bfd_elf_init_reloc_shdr (abfd,
2742 (asect->use_rela_p
2743 ? &esd->rela : &esd->rel),
2744 asect,
2745 asect->use_rela_p))
2746 arg->failed = TRUE;
2749 /* Check for processor-specific section types. */
2750 sh_type = this_hdr->sh_type;
2751 if (bed->elf_backend_fake_sections
2752 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2753 arg->failed = TRUE;
2755 if (sh_type == SHT_NOBITS && asect->size != 0)
2757 /* Don't change the header type from NOBITS if we are being
2758 called for objcopy --only-keep-debug. */
2759 this_hdr->sh_type = sh_type;
2763 /* Fill in the contents of a SHT_GROUP section. Called from
2764 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2765 when ELF targets use the generic linker, ld. Called for ld -r
2766 from bfd_elf_final_link. */
2768 void
2769 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2771 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2772 asection *elt, *first;
2773 unsigned char *loc;
2774 bfd_boolean gas;
2776 /* Ignore linker created group section. See elfNN_ia64_object_p in
2777 elfxx-ia64.c. */
2778 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2779 || *failedptr)
2780 return;
2782 if (elf_section_data (sec)->this_hdr.sh_info == 0)
2784 unsigned long symindx = 0;
2786 /* elf_group_id will have been set up by objcopy and the
2787 generic linker. */
2788 if (elf_group_id (sec) != NULL)
2789 symindx = elf_group_id (sec)->udata.i;
2791 if (symindx == 0)
2793 /* If called from the assembler, swap_out_syms will have set up
2794 elf_section_syms. */
2795 BFD_ASSERT (elf_section_syms (abfd) != NULL);
2796 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2798 elf_section_data (sec)->this_hdr.sh_info = symindx;
2800 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2802 /* The ELF backend linker sets sh_info to -2 when the group
2803 signature symbol is global, and thus the index can't be
2804 set until all local symbols are output. */
2805 asection *igroup = elf_sec_group (elf_next_in_group (sec));
2806 struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2807 unsigned long symndx = sec_data->this_hdr.sh_info;
2808 unsigned long extsymoff = 0;
2809 struct elf_link_hash_entry *h;
2811 if (!elf_bad_symtab (igroup->owner))
2813 Elf_Internal_Shdr *symtab_hdr;
2815 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2816 extsymoff = symtab_hdr->sh_info;
2818 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2819 while (h->root.type == bfd_link_hash_indirect
2820 || h->root.type == bfd_link_hash_warning)
2821 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2823 elf_section_data (sec)->this_hdr.sh_info = h->indx;
2826 /* The contents won't be allocated for "ld -r" or objcopy. */
2827 gas = TRUE;
2828 if (sec->contents == NULL)
2830 gas = FALSE;
2831 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2833 /* Arrange for the section to be written out. */
2834 elf_section_data (sec)->this_hdr.contents = sec->contents;
2835 if (sec->contents == NULL)
2837 *failedptr = TRUE;
2838 return;
2842 loc = sec->contents + sec->size;
2844 /* Get the pointer to the first section in the group that gas
2845 squirreled away here. objcopy arranges for this to be set to the
2846 start of the input section group. */
2847 first = elt = elf_next_in_group (sec);
2849 /* First element is a flag word. Rest of section is elf section
2850 indices for all the sections of the group. Write them backwards
2851 just to keep the group in the same order as given in .section
2852 directives, not that it matters. */
2853 while (elt != NULL)
2855 asection *s;
2857 s = elt;
2858 if (!gas)
2859 s = s->output_section;
2860 if (s != NULL
2861 && !bfd_is_abs_section (s))
2863 unsigned int idx = elf_section_data (s)->this_idx;
2865 loc -= 4;
2866 H_PUT_32 (abfd, idx, loc);
2868 elt = elf_next_in_group (elt);
2869 if (elt == first)
2870 break;
2873 if ((loc -= 4) != sec->contents)
2874 abort ();
2876 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2879 /* Assign all ELF section numbers. The dummy first section is handled here
2880 too. The link/info pointers for the standard section types are filled
2881 in here too, while we're at it. */
2883 static bfd_boolean
2884 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2886 struct elf_obj_tdata *t = elf_tdata (abfd);
2887 asection *sec;
2888 unsigned int section_number, secn;
2889 Elf_Internal_Shdr **i_shdrp;
2890 struct bfd_elf_section_data *d;
2891 bfd_boolean need_symtab;
2893 section_number = 1;
2895 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2897 /* SHT_GROUP sections are in relocatable files only. */
2898 if (link_info == NULL || link_info->relocatable)
2900 /* Put SHT_GROUP sections first. */
2901 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2903 d = elf_section_data (sec);
2905 if (d->this_hdr.sh_type == SHT_GROUP)
2907 if (sec->flags & SEC_LINKER_CREATED)
2909 /* Remove the linker created SHT_GROUP sections. */
2910 bfd_section_list_remove (abfd, sec);
2911 abfd->section_count--;
2913 else
2914 d->this_idx = section_number++;
2919 for (sec = abfd->sections; sec; sec = sec->next)
2921 d = elf_section_data (sec);
2923 if (d->this_hdr.sh_type != SHT_GROUP)
2924 d->this_idx = section_number++;
2925 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2926 if (d->rel.hdr)
2928 d->rel.idx = section_number++;
2929 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2931 else
2932 d->rel.idx = 0;
2934 if (d->rela.hdr)
2936 d->rela.idx = section_number++;
2937 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2939 else
2940 d->rela.idx = 0;
2943 t->shstrtab_section = section_number++;
2944 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2945 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2947 need_symtab = (bfd_get_symcount (abfd) > 0
2948 || (link_info == NULL
2949 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
2950 == HAS_RELOC)));
2951 if (need_symtab)
2953 t->symtab_section = section_number++;
2954 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2955 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2957 t->symtab_shndx_section = section_number++;
2958 t->symtab_shndx_hdr.sh_name
2959 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2960 ".symtab_shndx", FALSE);
2961 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2962 return FALSE;
2964 t->strtab_section = section_number++;
2965 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2968 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2969 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2971 elf_numsections (abfd) = section_number;
2972 elf_elfheader (abfd)->e_shnum = section_number;
2974 /* Set up the list of section header pointers, in agreement with the
2975 indices. */
2976 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
2977 sizeof (Elf_Internal_Shdr *));
2978 if (i_shdrp == NULL)
2979 return FALSE;
2981 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
2982 sizeof (Elf_Internal_Shdr));
2983 if (i_shdrp[0] == NULL)
2985 bfd_release (abfd, i_shdrp);
2986 return FALSE;
2989 elf_elfsections (abfd) = i_shdrp;
2991 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2992 if (need_symtab)
2994 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2995 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2997 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2998 t->symtab_shndx_hdr.sh_link = t->symtab_section;
3000 i_shdrp[t->strtab_section] = &t->strtab_hdr;
3001 t->symtab_hdr.sh_link = t->strtab_section;
3004 for (sec = abfd->sections; sec; sec = sec->next)
3006 asection *s;
3007 const char *name;
3009 d = elf_section_data (sec);
3011 i_shdrp[d->this_idx] = &d->this_hdr;
3012 if (d->rel.idx != 0)
3013 i_shdrp[d->rel.idx] = d->rel.hdr;
3014 if (d->rela.idx != 0)
3015 i_shdrp[d->rela.idx] = d->rela.hdr;
3017 /* Fill in the sh_link and sh_info fields while we're at it. */
3019 /* sh_link of a reloc section is the section index of the symbol
3020 table. sh_info is the section index of the section to which
3021 the relocation entries apply. */
3022 if (d->rel.idx != 0)
3024 d->rel.hdr->sh_link = t->symtab_section;
3025 d->rel.hdr->sh_info = d->this_idx;
3027 if (d->rela.idx != 0)
3029 d->rela.hdr->sh_link = t->symtab_section;
3030 d->rela.hdr->sh_info = d->this_idx;
3033 /* We need to set up sh_link for SHF_LINK_ORDER. */
3034 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3036 s = elf_linked_to_section (sec);
3037 if (s)
3039 /* elf_linked_to_section points to the input section. */
3040 if (link_info != NULL)
3042 /* Check discarded linkonce section. */
3043 if (elf_discarded_section (s))
3045 asection *kept;
3046 (*_bfd_error_handler)
3047 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3048 abfd, d->this_hdr.bfd_section,
3049 s, s->owner);
3050 /* Point to the kept section if it has the same
3051 size as the discarded one. */
3052 kept = _bfd_elf_check_kept_section (s, link_info);
3053 if (kept == NULL)
3055 bfd_set_error (bfd_error_bad_value);
3056 return FALSE;
3058 s = kept;
3061 s = s->output_section;
3062 BFD_ASSERT (s != NULL);
3064 else
3066 /* Handle objcopy. */
3067 if (s->output_section == NULL)
3069 (*_bfd_error_handler)
3070 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3071 abfd, d->this_hdr.bfd_section, s, s->owner);
3072 bfd_set_error (bfd_error_bad_value);
3073 return FALSE;
3075 s = s->output_section;
3077 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3079 else
3081 /* PR 290:
3082 The Intel C compiler generates SHT_IA_64_UNWIND with
3083 SHF_LINK_ORDER. But it doesn't set the sh_link or
3084 sh_info fields. Hence we could get the situation
3085 where s is NULL. */
3086 const struct elf_backend_data *bed
3087 = get_elf_backend_data (abfd);
3088 if (bed->link_order_error_handler)
3089 bed->link_order_error_handler
3090 (_("%B: warning: sh_link not set for section `%A'"),
3091 abfd, sec);
3095 switch (d->this_hdr.sh_type)
3097 case SHT_REL:
3098 case SHT_RELA:
3099 /* A reloc section which we are treating as a normal BFD
3100 section. sh_link is the section index of the symbol
3101 table. sh_info is the section index of the section to
3102 which the relocation entries apply. We assume that an
3103 allocated reloc section uses the dynamic symbol table.
3104 FIXME: How can we be sure? */
3105 s = bfd_get_section_by_name (abfd, ".dynsym");
3106 if (s != NULL)
3107 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3109 /* We look up the section the relocs apply to by name. */
3110 name = sec->name;
3111 if (d->this_hdr.sh_type == SHT_REL)
3112 name += 4;
3113 else
3114 name += 5;
3115 s = bfd_get_section_by_name (abfd, name);
3116 if (s != NULL)
3117 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3118 break;
3120 case SHT_STRTAB:
3121 /* We assume that a section named .stab*str is a stabs
3122 string section. We look for a section with the same name
3123 but without the trailing ``str'', and set its sh_link
3124 field to point to this section. */
3125 if (CONST_STRNEQ (sec->name, ".stab")
3126 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3128 size_t len;
3129 char *alc;
3131 len = strlen (sec->name);
3132 alc = (char *) bfd_malloc (len - 2);
3133 if (alc == NULL)
3134 return FALSE;
3135 memcpy (alc, sec->name, len - 3);
3136 alc[len - 3] = '\0';
3137 s = bfd_get_section_by_name (abfd, alc);
3138 free (alc);
3139 if (s != NULL)
3141 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3143 /* This is a .stab section. */
3144 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3145 elf_section_data (s)->this_hdr.sh_entsize
3146 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3149 break;
3151 case SHT_DYNAMIC:
3152 case SHT_DYNSYM:
3153 case SHT_GNU_verneed:
3154 case SHT_GNU_verdef:
3155 /* sh_link is the section header index of the string table
3156 used for the dynamic entries, or the symbol table, or the
3157 version strings. */
3158 s = bfd_get_section_by_name (abfd, ".dynstr");
3159 if (s != NULL)
3160 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3161 break;
3163 case SHT_GNU_LIBLIST:
3164 /* sh_link is the section header index of the prelink library
3165 list used for the dynamic entries, or the symbol table, or
3166 the version strings. */
3167 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3168 ? ".dynstr" : ".gnu.libstr");
3169 if (s != NULL)
3170 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3171 break;
3173 case SHT_HASH:
3174 case SHT_GNU_HASH:
3175 case SHT_GNU_versym:
3176 /* sh_link is the section header index of the symbol table
3177 this hash table or version table is for. */
3178 s = bfd_get_section_by_name (abfd, ".dynsym");
3179 if (s != NULL)
3180 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3181 break;
3183 case SHT_GROUP:
3184 d->this_hdr.sh_link = t->symtab_section;
3188 for (secn = 1; secn < section_number; ++secn)
3189 if (i_shdrp[secn] == NULL)
3190 i_shdrp[secn] = i_shdrp[0];
3191 else
3192 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3193 i_shdrp[secn]->sh_name);
3194 return TRUE;
3197 /* Map symbol from it's internal number to the external number, moving
3198 all local symbols to be at the head of the list. */
3200 static bfd_boolean
3201 sym_is_global (bfd *abfd, asymbol *sym)
3203 /* If the backend has a special mapping, use it. */
3204 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3205 if (bed->elf_backend_sym_is_global)
3206 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3208 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3209 || bfd_is_und_section (bfd_get_section (sym))
3210 || bfd_is_com_section (bfd_get_section (sym)));
3213 /* Don't output section symbols for sections that are not going to be
3214 output. */
3216 static bfd_boolean
3217 ignore_section_sym (bfd *abfd, asymbol *sym)
3219 return ((sym->flags & BSF_SECTION_SYM) != 0
3220 && !(sym->section->owner == abfd
3221 || (sym->section->output_section->owner == abfd
3222 && sym->section->output_offset == 0)));
3225 static bfd_boolean
3226 elf_map_symbols (bfd *abfd)
3228 unsigned int symcount = bfd_get_symcount (abfd);
3229 asymbol **syms = bfd_get_outsymbols (abfd);
3230 asymbol **sect_syms;
3231 unsigned int num_locals = 0;
3232 unsigned int num_globals = 0;
3233 unsigned int num_locals2 = 0;
3234 unsigned int num_globals2 = 0;
3235 int max_index = 0;
3236 unsigned int idx;
3237 asection *asect;
3238 asymbol **new_syms;
3240 #ifdef DEBUG
3241 fprintf (stderr, "elf_map_symbols\n");
3242 fflush (stderr);
3243 #endif
3245 for (asect = abfd->sections; asect; asect = asect->next)
3247 if (max_index < asect->index)
3248 max_index = asect->index;
3251 max_index++;
3252 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3253 if (sect_syms == NULL)
3254 return FALSE;
3255 elf_section_syms (abfd) = sect_syms;
3256 elf_num_section_syms (abfd) = max_index;
3258 /* Init sect_syms entries for any section symbols we have already
3259 decided to output. */
3260 for (idx = 0; idx < symcount; idx++)
3262 asymbol *sym = syms[idx];
3264 if ((sym->flags & BSF_SECTION_SYM) != 0
3265 && sym->value == 0
3266 && !ignore_section_sym (abfd, sym))
3268 asection *sec = sym->section;
3270 if (sec->owner != abfd)
3271 sec = sec->output_section;
3273 sect_syms[sec->index] = syms[idx];
3277 /* Classify all of the symbols. */
3278 for (idx = 0; idx < symcount; idx++)
3280 if (ignore_section_sym (abfd, syms[idx]))
3281 continue;
3282 if (!sym_is_global (abfd, syms[idx]))
3283 num_locals++;
3284 else
3285 num_globals++;
3288 /* We will be adding a section symbol for each normal BFD section. Most
3289 sections will already have a section symbol in outsymbols, but
3290 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3291 at least in that case. */
3292 for (asect = abfd->sections; asect; asect = asect->next)
3294 if (sect_syms[asect->index] == NULL)
3296 if (!sym_is_global (abfd, asect->symbol))
3297 num_locals++;
3298 else
3299 num_globals++;
3303 /* Now sort the symbols so the local symbols are first. */
3304 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3305 sizeof (asymbol *));
3307 if (new_syms == NULL)
3308 return FALSE;
3310 for (idx = 0; idx < symcount; idx++)
3312 asymbol *sym = syms[idx];
3313 unsigned int i;
3315 if (ignore_section_sym (abfd, sym))
3316 continue;
3317 if (!sym_is_global (abfd, sym))
3318 i = num_locals2++;
3319 else
3320 i = num_locals + num_globals2++;
3321 new_syms[i] = sym;
3322 sym->udata.i = i + 1;
3324 for (asect = abfd->sections; asect; asect = asect->next)
3326 if (sect_syms[asect->index] == NULL)
3328 asymbol *sym = asect->symbol;
3329 unsigned int i;
3331 sect_syms[asect->index] = sym;
3332 if (!sym_is_global (abfd, sym))
3333 i = num_locals2++;
3334 else
3335 i = num_locals + num_globals2++;
3336 new_syms[i] = sym;
3337 sym->udata.i = i + 1;
3341 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3343 elf_num_locals (abfd) = num_locals;
3344 elf_num_globals (abfd) = num_globals;
3345 return TRUE;
3348 /* Align to the maximum file alignment that could be required for any
3349 ELF data structure. */
3351 static inline file_ptr
3352 align_file_position (file_ptr off, int align)
3354 return (off + align - 1) & ~(align - 1);
3357 /* Assign a file position to a section, optionally aligning to the
3358 required section alignment. */
3360 file_ptr
3361 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3362 file_ptr offset,
3363 bfd_boolean align)
3365 if (align && i_shdrp->sh_addralign > 1)
3366 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3367 i_shdrp->sh_offset = offset;
3368 if (i_shdrp->bfd_section != NULL)
3369 i_shdrp->bfd_section->filepos = offset;
3370 if (i_shdrp->sh_type != SHT_NOBITS)
3371 offset += i_shdrp->sh_size;
3372 return offset;
3375 /* Compute the file positions we are going to put the sections at, and
3376 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3377 is not NULL, this is being called by the ELF backend linker. */
3379 bfd_boolean
3380 _bfd_elf_compute_section_file_positions (bfd *abfd,
3381 struct bfd_link_info *link_info)
3383 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3384 struct fake_section_arg fsargs;
3385 bfd_boolean failed;
3386 struct bfd_strtab_hash *strtab = NULL;
3387 Elf_Internal_Shdr *shstrtab_hdr;
3388 bfd_boolean need_symtab;
3390 if (abfd->output_has_begun)
3391 return TRUE;
3393 /* Do any elf backend specific processing first. */
3394 if (bed->elf_backend_begin_write_processing)
3395 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3397 if (! prep_headers (abfd))
3398 return FALSE;
3400 /* Post process the headers if necessary. */
3401 if (bed->elf_backend_post_process_headers)
3402 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3404 fsargs.failed = FALSE;
3405 fsargs.link_info = link_info;
3406 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3407 if (fsargs.failed)
3408 return FALSE;
3410 if (!assign_section_numbers (abfd, link_info))
3411 return FALSE;
3413 /* The backend linker builds symbol table information itself. */
3414 need_symtab = (link_info == NULL
3415 && (bfd_get_symcount (abfd) > 0
3416 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3417 == HAS_RELOC)));
3418 if (need_symtab)
3420 /* Non-zero if doing a relocatable link. */
3421 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3423 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3424 return FALSE;
3427 failed = FALSE;
3428 if (link_info == NULL)
3430 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3431 if (failed)
3432 return FALSE;
3435 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3436 /* sh_name was set in prep_headers. */
3437 shstrtab_hdr->sh_type = SHT_STRTAB;
3438 shstrtab_hdr->sh_flags = 0;
3439 shstrtab_hdr->sh_addr = 0;
3440 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3441 shstrtab_hdr->sh_entsize = 0;
3442 shstrtab_hdr->sh_link = 0;
3443 shstrtab_hdr->sh_info = 0;
3444 /* sh_offset is set in assign_file_positions_except_relocs. */
3445 shstrtab_hdr->sh_addralign = 1;
3447 if (!assign_file_positions_except_relocs (abfd, link_info))
3448 return FALSE;
3450 if (need_symtab)
3452 file_ptr off;
3453 Elf_Internal_Shdr *hdr;
3455 off = elf_tdata (abfd)->next_file_pos;
3457 hdr = &elf_tdata (abfd)->symtab_hdr;
3458 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3460 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3461 if (hdr->sh_size != 0)
3462 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3464 hdr = &elf_tdata (abfd)->strtab_hdr;
3465 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3467 elf_tdata (abfd)->next_file_pos = off;
3469 /* Now that we know where the .strtab section goes, write it
3470 out. */
3471 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3472 || ! _bfd_stringtab_emit (abfd, strtab))
3473 return FALSE;
3474 _bfd_stringtab_free (strtab);
3477 abfd->output_has_begun = TRUE;
3479 return TRUE;
3482 /* Make an initial estimate of the size of the program header. If we
3483 get the number wrong here, we'll redo section placement. */
3485 static bfd_size_type
3486 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3488 size_t segs;
3489 asection *s;
3490 const struct elf_backend_data *bed;
3492 /* Assume we will need exactly two PT_LOAD segments: one for text
3493 and one for data. */
3494 segs = 2;
3496 s = bfd_get_section_by_name (abfd, ".interp");
3497 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3499 /* If we have a loadable interpreter section, we need a
3500 PT_INTERP segment. In this case, assume we also need a
3501 PT_PHDR segment, although that may not be true for all
3502 targets. */
3503 segs += 2;
3506 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3508 /* We need a PT_DYNAMIC segment. */
3509 ++segs;
3512 if (info != NULL && info->relro)
3514 /* We need a PT_GNU_RELRO segment. */
3515 ++segs;
3518 if (elf_tdata (abfd)->eh_frame_hdr)
3520 /* We need a PT_GNU_EH_FRAME segment. */
3521 ++segs;
3524 if (elf_tdata (abfd)->stack_flags)
3526 /* We need a PT_GNU_STACK segment. */
3527 ++segs;
3530 for (s = abfd->sections; s != NULL; s = s->next)
3532 if ((s->flags & SEC_LOAD) != 0
3533 && CONST_STRNEQ (s->name, ".note"))
3535 /* We need a PT_NOTE segment. */
3536 ++segs;
3537 /* Try to create just one PT_NOTE segment
3538 for all adjacent loadable .note* sections.
3539 gABI requires that within a PT_NOTE segment
3540 (and also inside of each SHT_NOTE section)
3541 each note is padded to a multiple of 4 size,
3542 so we check whether the sections are correctly
3543 aligned. */
3544 if (s->alignment_power == 2)
3545 while (s->next != NULL
3546 && s->next->alignment_power == 2
3547 && (s->next->flags & SEC_LOAD) != 0
3548 && CONST_STRNEQ (s->next->name, ".note"))
3549 s = s->next;
3553 for (s = abfd->sections; s != NULL; s = s->next)
3555 if (s->flags & SEC_THREAD_LOCAL)
3557 /* We need a PT_TLS segment. */
3558 ++segs;
3559 break;
3563 /* Let the backend count up any program headers it might need. */
3564 bed = get_elf_backend_data (abfd);
3565 if (bed->elf_backend_additional_program_headers)
3567 int a;
3569 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3570 if (a == -1)
3571 abort ();
3572 segs += a;
3575 return segs * bed->s->sizeof_phdr;
3578 /* Find the segment that contains the output_section of section. */
3580 Elf_Internal_Phdr *
3581 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3583 struct elf_segment_map *m;
3584 Elf_Internal_Phdr *p;
3586 for (m = elf_tdata (abfd)->segment_map,
3587 p = elf_tdata (abfd)->phdr;
3588 m != NULL;
3589 m = m->next, p++)
3591 int i;
3593 for (i = m->count - 1; i >= 0; i--)
3594 if (m->sections[i] == section)
3595 return p;
3598 return NULL;
3601 /* Create a mapping from a set of sections to a program segment. */
3603 static struct elf_segment_map *
3604 make_mapping (bfd *abfd,
3605 asection **sections,
3606 unsigned int from,
3607 unsigned int to,
3608 bfd_boolean phdr)
3610 struct elf_segment_map *m;
3611 unsigned int i;
3612 asection **hdrpp;
3613 bfd_size_type amt;
3615 amt = sizeof (struct elf_segment_map);
3616 amt += (to - from - 1) * sizeof (asection *);
3617 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3618 if (m == NULL)
3619 return NULL;
3620 m->next = NULL;
3621 m->p_type = PT_LOAD;
3622 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3623 m->sections[i - from] = *hdrpp;
3624 m->count = to - from;
3626 if (from == 0 && phdr)
3628 /* Include the headers in the first PT_LOAD segment. */
3629 m->includes_filehdr = 1;
3630 m->includes_phdrs = 1;
3633 return m;
3636 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3637 on failure. */
3639 struct elf_segment_map *
3640 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3642 struct elf_segment_map *m;
3644 m = (struct elf_segment_map *) bfd_zalloc (abfd,
3645 sizeof (struct elf_segment_map));
3646 if (m == NULL)
3647 return NULL;
3648 m->next = NULL;
3649 m->p_type = PT_DYNAMIC;
3650 m->count = 1;
3651 m->sections[0] = dynsec;
3653 return m;
3656 /* Possibly add or remove segments from the segment map. */
3658 static bfd_boolean
3659 elf_modify_segment_map (bfd *abfd,
3660 struct bfd_link_info *info,
3661 bfd_boolean remove_empty_load)
3663 struct elf_segment_map **m;
3664 const struct elf_backend_data *bed;
3666 /* The placement algorithm assumes that non allocated sections are
3667 not in PT_LOAD segments. We ensure this here by removing such
3668 sections from the segment map. We also remove excluded
3669 sections. Finally, any PT_LOAD segment without sections is
3670 removed. */
3671 m = &elf_tdata (abfd)->segment_map;
3672 while (*m)
3674 unsigned int i, new_count;
3676 for (new_count = 0, i = 0; i < (*m)->count; i++)
3678 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3679 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3680 || (*m)->p_type != PT_LOAD))
3682 (*m)->sections[new_count] = (*m)->sections[i];
3683 new_count++;
3686 (*m)->count = new_count;
3688 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3689 *m = (*m)->next;
3690 else
3691 m = &(*m)->next;
3694 bed = get_elf_backend_data (abfd);
3695 if (bed->elf_backend_modify_segment_map != NULL)
3697 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3698 return FALSE;
3701 return TRUE;
3704 /* Set up a mapping from BFD sections to program segments. */
3706 bfd_boolean
3707 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3709 unsigned int count;
3710 struct elf_segment_map *m;
3711 asection **sections = NULL;
3712 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3713 bfd_boolean no_user_phdrs;
3715 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3716 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3718 asection *s;
3719 unsigned int i;
3720 struct elf_segment_map *mfirst;
3721 struct elf_segment_map **pm;
3722 asection *last_hdr;
3723 bfd_vma last_size;
3724 unsigned int phdr_index;
3725 bfd_vma maxpagesize;
3726 asection **hdrpp;
3727 bfd_boolean phdr_in_segment = TRUE;
3728 bfd_boolean writable;
3729 int tls_count = 0;
3730 asection *first_tls = NULL;
3731 asection *dynsec, *eh_frame_hdr;
3732 bfd_size_type amt;
3733 bfd_vma addr_mask, wrap_to = 0;
3735 /* Select the allocated sections, and sort them. */
3737 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3738 sizeof (asection *));
3739 if (sections == NULL)
3740 goto error_return;
3742 /* Calculate top address, avoiding undefined behaviour of shift
3743 left operator when shift count is equal to size of type
3744 being shifted. */
3745 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
3746 addr_mask = (addr_mask << 1) + 1;
3748 i = 0;
3749 for (s = abfd->sections; s != NULL; s = s->next)
3751 if ((s->flags & SEC_ALLOC) != 0)
3753 sections[i] = s;
3754 ++i;
3755 /* A wrapping section potentially clashes with header. */
3756 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
3757 wrap_to = (s->lma + s->size) & addr_mask;
3760 BFD_ASSERT (i <= bfd_count_sections (abfd));
3761 count = i;
3763 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3765 /* Build the mapping. */
3767 mfirst = NULL;
3768 pm = &mfirst;
3770 /* If we have a .interp section, then create a PT_PHDR segment for
3771 the program headers and a PT_INTERP segment for the .interp
3772 section. */
3773 s = bfd_get_section_by_name (abfd, ".interp");
3774 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3776 amt = sizeof (struct elf_segment_map);
3777 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3778 if (m == NULL)
3779 goto error_return;
3780 m->next = NULL;
3781 m->p_type = PT_PHDR;
3782 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3783 m->p_flags = PF_R | PF_X;
3784 m->p_flags_valid = 1;
3785 m->includes_phdrs = 1;
3787 *pm = m;
3788 pm = &m->next;
3790 amt = sizeof (struct elf_segment_map);
3791 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3792 if (m == NULL)
3793 goto error_return;
3794 m->next = NULL;
3795 m->p_type = PT_INTERP;
3796 m->count = 1;
3797 m->sections[0] = s;
3799 *pm = m;
3800 pm = &m->next;
3803 /* Look through the sections. We put sections in the same program
3804 segment when the start of the second section can be placed within
3805 a few bytes of the end of the first section. */
3806 last_hdr = NULL;
3807 last_size = 0;
3808 phdr_index = 0;
3809 maxpagesize = bed->maxpagesize;
3810 writable = FALSE;
3811 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3812 if (dynsec != NULL
3813 && (dynsec->flags & SEC_LOAD) == 0)
3814 dynsec = NULL;
3816 /* Deal with -Ttext or something similar such that the first section
3817 is not adjacent to the program headers. This is an
3818 approximation, since at this point we don't know exactly how many
3819 program headers we will need. */
3820 if (count > 0)
3822 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3824 if (phdr_size == (bfd_size_type) -1)
3825 phdr_size = get_program_header_size (abfd, info);
3826 if ((abfd->flags & D_PAGED) == 0
3827 || (sections[0]->lma & addr_mask) < phdr_size
3828 || ((sections[0]->lma & addr_mask) % maxpagesize
3829 < phdr_size % maxpagesize)
3830 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
3831 phdr_in_segment = FALSE;
3834 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3836 asection *hdr;
3837 bfd_boolean new_segment;
3839 hdr = *hdrpp;
3841 /* See if this section and the last one will fit in the same
3842 segment. */
3844 if (last_hdr == NULL)
3846 /* If we don't have a segment yet, then we don't need a new
3847 one (we build the last one after this loop). */
3848 new_segment = FALSE;
3850 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3852 /* If this section has a different relation between the
3853 virtual address and the load address, then we need a new
3854 segment. */
3855 new_segment = TRUE;
3857 else if (hdr->lma < last_hdr->lma + last_size
3858 || last_hdr->lma + last_size < last_hdr->lma)
3860 /* If this section has a load address that makes it overlap
3861 the previous section, then we need a new segment. */
3862 new_segment = TRUE;
3864 /* In the next test we have to be careful when last_hdr->lma is close
3865 to the end of the address space. If the aligned address wraps
3866 around to the start of the address space, then there are no more
3867 pages left in memory and it is OK to assume that the current
3868 section can be included in the current segment. */
3869 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3870 > last_hdr->lma)
3871 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3872 <= hdr->lma))
3874 /* If putting this section in this segment would force us to
3875 skip a page in the segment, then we need a new segment. */
3876 new_segment = TRUE;
3878 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3879 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3881 /* We don't want to put a loadable section after a
3882 nonloadable section in the same segment.
3883 Consider .tbss sections as loadable for this purpose. */
3884 new_segment = TRUE;
3886 else if ((abfd->flags & D_PAGED) == 0)
3888 /* If the file is not demand paged, which means that we
3889 don't require the sections to be correctly aligned in the
3890 file, then there is no other reason for a new segment. */
3891 new_segment = FALSE;
3893 else if (! writable
3894 && (hdr->flags & SEC_READONLY) == 0
3895 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
3896 != (hdr->lma & -maxpagesize)))
3898 /* We don't want to put a writable section in a read only
3899 segment, unless they are on the same page in memory
3900 anyhow. We already know that the last section does not
3901 bring us past the current section on the page, so the
3902 only case in which the new section is not on the same
3903 page as the previous section is when the previous section
3904 ends precisely on a page boundary. */
3905 new_segment = TRUE;
3907 else
3909 /* Otherwise, we can use the same segment. */
3910 new_segment = FALSE;
3913 /* Allow interested parties a chance to override our decision. */
3914 if (last_hdr != NULL
3915 && info != NULL
3916 && info->callbacks->override_segment_assignment != NULL)
3917 new_segment
3918 = info->callbacks->override_segment_assignment (info, abfd, hdr,
3919 last_hdr,
3920 new_segment);
3922 if (! new_segment)
3924 if ((hdr->flags & SEC_READONLY) == 0)
3925 writable = TRUE;
3926 last_hdr = hdr;
3927 /* .tbss sections effectively have zero size. */
3928 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3929 != SEC_THREAD_LOCAL)
3930 last_size = hdr->size;
3931 else
3932 last_size = 0;
3933 continue;
3936 /* We need a new program segment. We must create a new program
3937 header holding all the sections from phdr_index until hdr. */
3939 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3940 if (m == NULL)
3941 goto error_return;
3943 *pm = m;
3944 pm = &m->next;
3946 if ((hdr->flags & SEC_READONLY) == 0)
3947 writable = TRUE;
3948 else
3949 writable = FALSE;
3951 last_hdr = hdr;
3952 /* .tbss sections effectively have zero size. */
3953 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3954 last_size = hdr->size;
3955 else
3956 last_size = 0;
3957 phdr_index = i;
3958 phdr_in_segment = FALSE;
3961 /* Create a final PT_LOAD program segment. */
3962 if (last_hdr != NULL)
3964 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3965 if (m == NULL)
3966 goto error_return;
3968 *pm = m;
3969 pm = &m->next;
3972 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3973 if (dynsec != NULL)
3975 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3976 if (m == NULL)
3977 goto error_return;
3978 *pm = m;
3979 pm = &m->next;
3982 /* For each batch of consecutive loadable .note sections,
3983 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3984 because if we link together nonloadable .note sections and
3985 loadable .note sections, we will generate two .note sections
3986 in the output file. FIXME: Using names for section types is
3987 bogus anyhow. */
3988 for (s = abfd->sections; s != NULL; s = s->next)
3990 if ((s->flags & SEC_LOAD) != 0
3991 && CONST_STRNEQ (s->name, ".note"))
3993 asection *s2;
3995 count = 1;
3996 amt = sizeof (struct elf_segment_map);
3997 if (s->alignment_power == 2)
3998 for (s2 = s; s2->next != NULL; s2 = s2->next)
4000 if (s2->next->alignment_power == 2
4001 && (s2->next->flags & SEC_LOAD) != 0
4002 && CONST_STRNEQ (s2->next->name, ".note")
4003 && align_power (s2->lma + s2->size, 2)
4004 == s2->next->lma)
4005 count++;
4006 else
4007 break;
4009 amt += (count - 1) * sizeof (asection *);
4010 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4011 if (m == NULL)
4012 goto error_return;
4013 m->next = NULL;
4014 m->p_type = PT_NOTE;
4015 m->count = count;
4016 while (count > 1)
4018 m->sections[m->count - count--] = s;
4019 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4020 s = s->next;
4022 m->sections[m->count - 1] = s;
4023 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4024 *pm = m;
4025 pm = &m->next;
4027 if (s->flags & SEC_THREAD_LOCAL)
4029 if (! tls_count)
4030 first_tls = s;
4031 tls_count++;
4035 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4036 if (tls_count > 0)
4038 amt = sizeof (struct elf_segment_map);
4039 amt += (tls_count - 1) * sizeof (asection *);
4040 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4041 if (m == NULL)
4042 goto error_return;
4043 m->next = NULL;
4044 m->p_type = PT_TLS;
4045 m->count = tls_count;
4046 /* Mandated PF_R. */
4047 m->p_flags = PF_R;
4048 m->p_flags_valid = 1;
4049 for (i = 0; i < (unsigned int) tls_count; ++i)
4051 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
4052 m->sections[i] = first_tls;
4053 first_tls = first_tls->next;
4056 *pm = m;
4057 pm = &m->next;
4060 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4061 segment. */
4062 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
4063 if (eh_frame_hdr != NULL
4064 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4066 amt = sizeof (struct elf_segment_map);
4067 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4068 if (m == NULL)
4069 goto error_return;
4070 m->next = NULL;
4071 m->p_type = PT_GNU_EH_FRAME;
4072 m->count = 1;
4073 m->sections[0] = eh_frame_hdr->output_section;
4075 *pm = m;
4076 pm = &m->next;
4079 if (elf_tdata (abfd)->stack_flags)
4081 amt = sizeof (struct elf_segment_map);
4082 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4083 if (m == NULL)
4084 goto error_return;
4085 m->next = NULL;
4086 m->p_type = PT_GNU_STACK;
4087 m->p_flags = elf_tdata (abfd)->stack_flags;
4088 m->p_flags_valid = 1;
4090 *pm = m;
4091 pm = &m->next;
4094 if (info != NULL && info->relro)
4096 for (m = mfirst; m != NULL; m = m->next)
4098 if (m->p_type == PT_LOAD)
4100 asection *last = m->sections[m->count - 1];
4101 bfd_vma vaddr = m->sections[0]->vma;
4102 bfd_vma filesz = last->vma - vaddr + last->size;
4104 if (vaddr < info->relro_end
4105 && vaddr >= info->relro_start
4106 && (vaddr + filesz) >= info->relro_end)
4107 break;
4111 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4112 if (m != NULL)
4114 amt = sizeof (struct elf_segment_map);
4115 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4116 if (m == NULL)
4117 goto error_return;
4118 m->next = NULL;
4119 m->p_type = PT_GNU_RELRO;
4120 m->p_flags = PF_R;
4121 m->p_flags_valid = 1;
4123 *pm = m;
4124 pm = &m->next;
4128 free (sections);
4129 elf_tdata (abfd)->segment_map = mfirst;
4132 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4133 return FALSE;
4135 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4136 ++count;
4137 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
4139 return TRUE;
4141 error_return:
4142 if (sections != NULL)
4143 free (sections);
4144 return FALSE;
4147 /* Sort sections by address. */
4149 static int
4150 elf_sort_sections (const void *arg1, const void *arg2)
4152 const asection *sec1 = *(const asection **) arg1;
4153 const asection *sec2 = *(const asection **) arg2;
4154 bfd_size_type size1, size2;
4156 /* Sort by LMA first, since this is the address used to
4157 place the section into a segment. */
4158 if (sec1->lma < sec2->lma)
4159 return -1;
4160 else if (sec1->lma > sec2->lma)
4161 return 1;
4163 /* Then sort by VMA. Normally the LMA and the VMA will be
4164 the same, and this will do nothing. */
4165 if (sec1->vma < sec2->vma)
4166 return -1;
4167 else if (sec1->vma > sec2->vma)
4168 return 1;
4170 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4172 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4174 if (TOEND (sec1))
4176 if (TOEND (sec2))
4178 /* If the indicies are the same, do not return 0
4179 here, but continue to try the next comparison. */
4180 if (sec1->target_index - sec2->target_index != 0)
4181 return sec1->target_index - sec2->target_index;
4183 else
4184 return 1;
4186 else if (TOEND (sec2))
4187 return -1;
4189 #undef TOEND
4191 /* Sort by size, to put zero sized sections
4192 before others at the same address. */
4194 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4195 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4197 if (size1 < size2)
4198 return -1;
4199 if (size1 > size2)
4200 return 1;
4202 return sec1->target_index - sec2->target_index;
4205 /* Ian Lance Taylor writes:
4207 We shouldn't be using % with a negative signed number. That's just
4208 not good. We have to make sure either that the number is not
4209 negative, or that the number has an unsigned type. When the types
4210 are all the same size they wind up as unsigned. When file_ptr is a
4211 larger signed type, the arithmetic winds up as signed long long,
4212 which is wrong.
4214 What we're trying to say here is something like ``increase OFF by
4215 the least amount that will cause it to be equal to the VMA modulo
4216 the page size.'' */
4217 /* In other words, something like:
4219 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4220 off_offset = off % bed->maxpagesize;
4221 if (vma_offset < off_offset)
4222 adjustment = vma_offset + bed->maxpagesize - off_offset;
4223 else
4224 adjustment = vma_offset - off_offset;
4226 which can can be collapsed into the expression below. */
4228 static file_ptr
4229 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4231 return ((vma - off) % maxpagesize);
4234 static void
4235 print_segment_map (const struct elf_segment_map *m)
4237 unsigned int j;
4238 const char *pt = get_segment_type (m->p_type);
4239 char buf[32];
4241 if (pt == NULL)
4243 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4244 sprintf (buf, "LOPROC+%7.7x",
4245 (unsigned int) (m->p_type - PT_LOPROC));
4246 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4247 sprintf (buf, "LOOS+%7.7x",
4248 (unsigned int) (m->p_type - PT_LOOS));
4249 else
4250 snprintf (buf, sizeof (buf), "%8.8x",
4251 (unsigned int) m->p_type);
4252 pt = buf;
4254 fprintf (stderr, "%s:", pt);
4255 for (j = 0; j < m->count; j++)
4256 fprintf (stderr, " %s", m->sections [j]->name);
4257 putc ('\n',stderr);
4260 static bfd_boolean
4261 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4263 void *buf;
4264 bfd_boolean ret;
4266 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4267 return FALSE;
4268 buf = bfd_zmalloc (len);
4269 if (buf == NULL)
4270 return FALSE;
4271 ret = bfd_bwrite (buf, len, abfd) == len;
4272 free (buf);
4273 return ret;
4276 /* Assign file positions to the sections based on the mapping from
4277 sections to segments. This function also sets up some fields in
4278 the file header. */
4280 static bfd_boolean
4281 assign_file_positions_for_load_sections (bfd *abfd,
4282 struct bfd_link_info *link_info)
4284 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4285 struct elf_segment_map *m;
4286 Elf_Internal_Phdr *phdrs;
4287 Elf_Internal_Phdr *p;
4288 file_ptr off;
4289 bfd_size_type maxpagesize;
4290 unsigned int alloc;
4291 unsigned int i, j;
4292 bfd_vma header_pad = 0;
4294 if (link_info == NULL
4295 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4296 return FALSE;
4298 alloc = 0;
4299 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4301 ++alloc;
4302 if (m->header_size)
4303 header_pad = m->header_size;
4306 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4307 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4308 elf_elfheader (abfd)->e_phnum = alloc;
4310 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4311 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4312 else
4313 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4314 >= alloc * bed->s->sizeof_phdr);
4316 if (alloc == 0)
4318 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4319 return TRUE;
4322 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4323 see assign_file_positions_except_relocs, so make sure we have
4324 that amount allocated, with trailing space cleared.
4325 The variable alloc contains the computed need, while elf_tdata
4326 (abfd)->program_header_size contains the size used for the
4327 layout.
4328 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4329 where the layout is forced to according to a larger size in the
4330 last iterations for the testcase ld-elf/header. */
4331 BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr
4332 == 0);
4333 phdrs = (Elf_Internal_Phdr *)
4334 bfd_zalloc2 (abfd,
4335 (elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr),
4336 sizeof (Elf_Internal_Phdr));
4337 elf_tdata (abfd)->phdr = phdrs;
4338 if (phdrs == NULL)
4339 return FALSE;
4341 maxpagesize = 1;
4342 if ((abfd->flags & D_PAGED) != 0)
4343 maxpagesize = bed->maxpagesize;
4345 off = bed->s->sizeof_ehdr;
4346 off += alloc * bed->s->sizeof_phdr;
4347 if (header_pad < (bfd_vma) off)
4348 header_pad = 0;
4349 else
4350 header_pad -= off;
4351 off += header_pad;
4353 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4354 m != NULL;
4355 m = m->next, p++, j++)
4357 asection **secpp;
4358 bfd_vma off_adjust;
4359 bfd_boolean no_contents;
4361 /* If elf_segment_map is not from map_sections_to_segments, the
4362 sections may not be correctly ordered. NOTE: sorting should
4363 not be done to the PT_NOTE section of a corefile, which may
4364 contain several pseudo-sections artificially created by bfd.
4365 Sorting these pseudo-sections breaks things badly. */
4366 if (m->count > 1
4367 && !(elf_elfheader (abfd)->e_type == ET_CORE
4368 && m->p_type == PT_NOTE))
4369 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4370 elf_sort_sections);
4372 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4373 number of sections with contents contributing to both p_filesz
4374 and p_memsz, followed by a number of sections with no contents
4375 that just contribute to p_memsz. In this loop, OFF tracks next
4376 available file offset for PT_LOAD and PT_NOTE segments. */
4377 p->p_type = m->p_type;
4378 p->p_flags = m->p_flags;
4380 if (m->count == 0)
4381 p->p_vaddr = 0;
4382 else
4383 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4385 if (m->p_paddr_valid)
4386 p->p_paddr = m->p_paddr;
4387 else if (m->count == 0)
4388 p->p_paddr = 0;
4389 else
4390 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4392 if (p->p_type == PT_LOAD
4393 && (abfd->flags & D_PAGED) != 0)
4395 /* p_align in demand paged PT_LOAD segments effectively stores
4396 the maximum page size. When copying an executable with
4397 objcopy, we set m->p_align from the input file. Use this
4398 value for maxpagesize rather than bed->maxpagesize, which
4399 may be different. Note that we use maxpagesize for PT_TLS
4400 segment alignment later in this function, so we are relying
4401 on at least one PT_LOAD segment appearing before a PT_TLS
4402 segment. */
4403 if (m->p_align_valid)
4404 maxpagesize = m->p_align;
4406 p->p_align = maxpagesize;
4408 else if (m->p_align_valid)
4409 p->p_align = m->p_align;
4410 else if (m->count == 0)
4411 p->p_align = 1 << bed->s->log_file_align;
4412 else
4413 p->p_align = 0;
4415 no_contents = FALSE;
4416 off_adjust = 0;
4417 if (p->p_type == PT_LOAD
4418 && m->count > 0)
4420 bfd_size_type align;
4421 unsigned int align_power = 0;
4423 if (m->p_align_valid)
4424 align = p->p_align;
4425 else
4427 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4429 unsigned int secalign;
4431 secalign = bfd_get_section_alignment (abfd, *secpp);
4432 if (secalign > align_power)
4433 align_power = secalign;
4435 align = (bfd_size_type) 1 << align_power;
4436 if (align < maxpagesize)
4437 align = maxpagesize;
4440 for (i = 0; i < m->count; i++)
4441 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4442 /* If we aren't making room for this section, then
4443 it must be SHT_NOBITS regardless of what we've
4444 set via struct bfd_elf_special_section. */
4445 elf_section_type (m->sections[i]) = SHT_NOBITS;
4447 /* Find out whether this segment contains any loadable
4448 sections. */
4449 no_contents = TRUE;
4450 for (i = 0; i < m->count; i++)
4451 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4453 no_contents = FALSE;
4454 break;
4457 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4458 off += off_adjust;
4459 if (no_contents)
4461 /* We shouldn't need to align the segment on disk since
4462 the segment doesn't need file space, but the gABI
4463 arguably requires the alignment and glibc ld.so
4464 checks it. So to comply with the alignment
4465 requirement but not waste file space, we adjust
4466 p_offset for just this segment. (OFF_ADJUST is
4467 subtracted from OFF later.) This may put p_offset
4468 past the end of file, but that shouldn't matter. */
4470 else
4471 off_adjust = 0;
4473 /* Make sure the .dynamic section is the first section in the
4474 PT_DYNAMIC segment. */
4475 else if (p->p_type == PT_DYNAMIC
4476 && m->count > 1
4477 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4479 _bfd_error_handler
4480 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4481 abfd);
4482 bfd_set_error (bfd_error_bad_value);
4483 return FALSE;
4485 /* Set the note section type to SHT_NOTE. */
4486 else if (p->p_type == PT_NOTE)
4487 for (i = 0; i < m->count; i++)
4488 elf_section_type (m->sections[i]) = SHT_NOTE;
4490 p->p_offset = 0;
4491 p->p_filesz = 0;
4492 p->p_memsz = 0;
4494 if (m->includes_filehdr)
4496 if (!m->p_flags_valid)
4497 p->p_flags |= PF_R;
4498 p->p_filesz = bed->s->sizeof_ehdr;
4499 p->p_memsz = bed->s->sizeof_ehdr;
4500 if (m->count > 0)
4502 BFD_ASSERT (p->p_type == PT_LOAD);
4504 if (p->p_vaddr < (bfd_vma) off)
4506 (*_bfd_error_handler)
4507 (_("%B: Not enough room for program headers, try linking with -N"),
4508 abfd);
4509 bfd_set_error (bfd_error_bad_value);
4510 return FALSE;
4513 p->p_vaddr -= off;
4514 if (!m->p_paddr_valid)
4515 p->p_paddr -= off;
4519 if (m->includes_phdrs)
4521 if (!m->p_flags_valid)
4522 p->p_flags |= PF_R;
4524 if (!m->includes_filehdr)
4526 p->p_offset = bed->s->sizeof_ehdr;
4528 if (m->count > 0)
4530 BFD_ASSERT (p->p_type == PT_LOAD);
4531 p->p_vaddr -= off - p->p_offset;
4532 if (!m->p_paddr_valid)
4533 p->p_paddr -= off - p->p_offset;
4537 p->p_filesz += alloc * bed->s->sizeof_phdr;
4538 p->p_memsz += alloc * bed->s->sizeof_phdr;
4539 if (m->count)
4541 p->p_filesz += header_pad;
4542 p->p_memsz += header_pad;
4546 if (p->p_type == PT_LOAD
4547 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4549 if (!m->includes_filehdr && !m->includes_phdrs)
4550 p->p_offset = off;
4551 else
4553 file_ptr adjust;
4555 adjust = off - (p->p_offset + p->p_filesz);
4556 if (!no_contents)
4557 p->p_filesz += adjust;
4558 p->p_memsz += adjust;
4562 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4563 maps. Set filepos for sections in PT_LOAD segments, and in
4564 core files, for sections in PT_NOTE segments.
4565 assign_file_positions_for_non_load_sections will set filepos
4566 for other sections and update p_filesz for other segments. */
4567 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4569 asection *sec;
4570 bfd_size_type align;
4571 Elf_Internal_Shdr *this_hdr;
4573 sec = *secpp;
4574 this_hdr = &elf_section_data (sec)->this_hdr;
4575 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4577 if ((p->p_type == PT_LOAD
4578 || p->p_type == PT_TLS)
4579 && (this_hdr->sh_type != SHT_NOBITS
4580 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4581 && ((this_hdr->sh_flags & SHF_TLS) == 0
4582 || p->p_type == PT_TLS))))
4584 bfd_vma p_start = p->p_paddr;
4585 bfd_vma p_end = p_start + p->p_memsz;
4586 bfd_vma s_start = sec->lma;
4587 bfd_vma adjust = s_start - p_end;
4589 if (adjust != 0
4590 && (s_start < p_end
4591 || p_end < p_start))
4593 (*_bfd_error_handler)
4594 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4595 (unsigned long) s_start, (unsigned long) p_end);
4596 adjust = 0;
4597 sec->lma = p_end;
4599 p->p_memsz += adjust;
4601 if (this_hdr->sh_type != SHT_NOBITS)
4603 if (p->p_filesz + adjust < p->p_memsz)
4605 /* We have a PROGBITS section following NOBITS ones.
4606 Allocate file space for the NOBITS section(s) and
4607 zero it. */
4608 adjust = p->p_memsz - p->p_filesz;
4609 if (!write_zeros (abfd, off, adjust))
4610 return FALSE;
4612 off += adjust;
4613 p->p_filesz += adjust;
4617 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4619 /* The section at i == 0 is the one that actually contains
4620 everything. */
4621 if (i == 0)
4623 this_hdr->sh_offset = sec->filepos = off;
4624 off += this_hdr->sh_size;
4625 p->p_filesz = this_hdr->sh_size;
4626 p->p_memsz = 0;
4627 p->p_align = 1;
4629 else
4631 /* The rest are fake sections that shouldn't be written. */
4632 sec->filepos = 0;
4633 sec->size = 0;
4634 sec->flags = 0;
4635 continue;
4638 else
4640 if (p->p_type == PT_LOAD)
4642 this_hdr->sh_offset = sec->filepos = off;
4643 if (this_hdr->sh_type != SHT_NOBITS)
4644 off += this_hdr->sh_size;
4647 if (this_hdr->sh_type != SHT_NOBITS)
4649 p->p_filesz += this_hdr->sh_size;
4650 /* A load section without SHF_ALLOC is something like
4651 a note section in a PT_NOTE segment. These take
4652 file space but are not loaded into memory. */
4653 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4654 p->p_memsz += this_hdr->sh_size;
4656 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4658 if (p->p_type == PT_TLS)
4659 p->p_memsz += this_hdr->sh_size;
4661 /* .tbss is special. It doesn't contribute to p_memsz of
4662 normal segments. */
4663 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4664 p->p_memsz += this_hdr->sh_size;
4667 if (align > p->p_align
4668 && !m->p_align_valid
4669 && (p->p_type != PT_LOAD
4670 || (abfd->flags & D_PAGED) == 0))
4671 p->p_align = align;
4674 if (!m->p_flags_valid)
4676 p->p_flags |= PF_R;
4677 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4678 p->p_flags |= PF_X;
4679 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4680 p->p_flags |= PF_W;
4683 off -= off_adjust;
4685 /* Check that all sections are in a PT_LOAD segment.
4686 Don't check funky gdb generated core files. */
4687 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4689 bfd_boolean check_vma = TRUE;
4691 for (i = 1; i < m->count; i++)
4692 if (m->sections[i]->vma == m->sections[i - 1]->vma
4693 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4694 ->this_hdr), p) != 0
4695 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4696 ->this_hdr), p) != 0)
4698 /* Looks like we have overlays packed into the segment. */
4699 check_vma = FALSE;
4700 break;
4703 for (i = 0; i < m->count; i++)
4705 Elf_Internal_Shdr *this_hdr;
4706 asection *sec;
4708 sec = m->sections[i];
4709 this_hdr = &(elf_section_data(sec)->this_hdr);
4710 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0))
4712 (*_bfd_error_handler)
4713 (_("%B: section `%A' can't be allocated in segment %d"),
4714 abfd, sec, j);
4715 print_segment_map (m);
4721 elf_tdata (abfd)->next_file_pos = off;
4722 return TRUE;
4725 /* Assign file positions for the other sections. */
4727 static bfd_boolean
4728 assign_file_positions_for_non_load_sections (bfd *abfd,
4729 struct bfd_link_info *link_info)
4731 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4732 Elf_Internal_Shdr **i_shdrpp;
4733 Elf_Internal_Shdr **hdrpp;
4734 Elf_Internal_Phdr *phdrs;
4735 Elf_Internal_Phdr *p;
4736 struct elf_segment_map *m;
4737 bfd_vma filehdr_vaddr, filehdr_paddr;
4738 bfd_vma phdrs_vaddr, phdrs_paddr;
4739 file_ptr off;
4740 unsigned int num_sec;
4741 unsigned int i;
4742 unsigned int count;
4744 i_shdrpp = elf_elfsections (abfd);
4745 num_sec = elf_numsections (abfd);
4746 off = elf_tdata (abfd)->next_file_pos;
4747 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4749 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4750 Elf_Internal_Shdr *hdr;
4752 hdr = *hdrpp;
4753 if (hdr->bfd_section != NULL
4754 && (hdr->bfd_section->filepos != 0
4755 || (hdr->sh_type == SHT_NOBITS
4756 && hdr->contents == NULL)))
4757 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4758 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4760 (*_bfd_error_handler)
4761 (_("%B: warning: allocated section `%s' not in segment"),
4762 abfd,
4763 (hdr->bfd_section == NULL
4764 ? "*unknown*"
4765 : hdr->bfd_section->name));
4766 /* We don't need to page align empty sections. */
4767 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4768 off += vma_page_aligned_bias (hdr->sh_addr, off,
4769 bed->maxpagesize);
4770 else
4771 off += vma_page_aligned_bias (hdr->sh_addr, off,
4772 hdr->sh_addralign);
4773 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4774 FALSE);
4776 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4777 && hdr->bfd_section == NULL)
4778 || hdr == i_shdrpp[tdata->symtab_section]
4779 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4780 || hdr == i_shdrpp[tdata->strtab_section])
4781 hdr->sh_offset = -1;
4782 else
4783 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4786 /* Now that we have set the section file positions, we can set up
4787 the file positions for the non PT_LOAD segments. */
4788 count = 0;
4789 filehdr_vaddr = 0;
4790 filehdr_paddr = 0;
4791 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4792 phdrs_paddr = 0;
4793 phdrs = elf_tdata (abfd)->phdr;
4794 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4795 m != NULL;
4796 m = m->next, p++)
4798 ++count;
4799 if (p->p_type != PT_LOAD)
4800 continue;
4802 if (m->includes_filehdr)
4804 filehdr_vaddr = p->p_vaddr;
4805 filehdr_paddr = p->p_paddr;
4807 if (m->includes_phdrs)
4809 phdrs_vaddr = p->p_vaddr;
4810 phdrs_paddr = p->p_paddr;
4811 if (m->includes_filehdr)
4813 phdrs_vaddr += bed->s->sizeof_ehdr;
4814 phdrs_paddr += bed->s->sizeof_ehdr;
4819 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4820 m != NULL;
4821 m = m->next, p++)
4823 if (p->p_type == PT_GNU_RELRO)
4825 const Elf_Internal_Phdr *lp;
4827 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4829 if (link_info != NULL)
4831 /* During linking the range of the RELRO segment is passed
4832 in link_info. */
4833 for (lp = phdrs; lp < phdrs + count; ++lp)
4835 if (lp->p_type == PT_LOAD
4836 && lp->p_vaddr >= link_info->relro_start
4837 && lp->p_vaddr < link_info->relro_end
4838 && lp->p_vaddr + lp->p_filesz >= link_info->relro_end)
4839 break;
4842 else
4844 /* Otherwise we are copying an executable or shared
4845 library, but we need to use the same linker logic. */
4846 for (lp = phdrs; lp < phdrs + count; ++lp)
4848 if (lp->p_type == PT_LOAD
4849 && lp->p_paddr == p->p_paddr)
4850 break;
4854 if (lp < phdrs + count)
4856 p->p_vaddr = lp->p_vaddr;
4857 p->p_paddr = lp->p_paddr;
4858 p->p_offset = lp->p_offset;
4859 if (link_info != NULL)
4860 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4861 else if (m->p_size_valid)
4862 p->p_filesz = m->p_size;
4863 else
4864 abort ();
4865 p->p_memsz = p->p_filesz;
4866 p->p_align = 1;
4867 p->p_flags = (lp->p_flags & ~PF_W);
4869 else
4871 memset (p, 0, sizeof *p);
4872 p->p_type = PT_NULL;
4875 else if (m->count != 0)
4877 if (p->p_type != PT_LOAD
4878 && (p->p_type != PT_NOTE
4879 || bfd_get_format (abfd) != bfd_core))
4881 Elf_Internal_Shdr *hdr;
4882 asection *sect;
4884 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4886 sect = m->sections[m->count - 1];
4887 hdr = &elf_section_data (sect)->this_hdr;
4888 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4889 if (hdr->sh_type != SHT_NOBITS)
4890 p->p_filesz += hdr->sh_size;
4891 p->p_offset = m->sections[0]->filepos;
4894 else if (m->includes_filehdr)
4896 p->p_vaddr = filehdr_vaddr;
4897 if (! m->p_paddr_valid)
4898 p->p_paddr = filehdr_paddr;
4900 else if (m->includes_phdrs)
4902 p->p_vaddr = phdrs_vaddr;
4903 if (! m->p_paddr_valid)
4904 p->p_paddr = phdrs_paddr;
4908 elf_tdata (abfd)->next_file_pos = off;
4910 return TRUE;
4913 /* Work out the file positions of all the sections. This is called by
4914 _bfd_elf_compute_section_file_positions. All the section sizes and
4915 VMAs must be known before this is called.
4917 Reloc sections come in two flavours: Those processed specially as
4918 "side-channel" data attached to a section to which they apply, and
4919 those that bfd doesn't process as relocations. The latter sort are
4920 stored in a normal bfd section by bfd_section_from_shdr. We don't
4921 consider the former sort here, unless they form part of the loadable
4922 image. Reloc sections not assigned here will be handled later by
4923 assign_file_positions_for_relocs.
4925 We also don't set the positions of the .symtab and .strtab here. */
4927 static bfd_boolean
4928 assign_file_positions_except_relocs (bfd *abfd,
4929 struct bfd_link_info *link_info)
4931 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4932 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4933 file_ptr off;
4934 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4936 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4937 && bfd_get_format (abfd) != bfd_core)
4939 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4940 unsigned int num_sec = elf_numsections (abfd);
4941 Elf_Internal_Shdr **hdrpp;
4942 unsigned int i;
4944 /* Start after the ELF header. */
4945 off = i_ehdrp->e_ehsize;
4947 /* We are not creating an executable, which means that we are
4948 not creating a program header, and that the actual order of
4949 the sections in the file is unimportant. */
4950 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4952 Elf_Internal_Shdr *hdr;
4954 hdr = *hdrpp;
4955 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4956 && hdr->bfd_section == NULL)
4957 || i == tdata->symtab_section
4958 || i == tdata->symtab_shndx_section
4959 || i == tdata->strtab_section)
4961 hdr->sh_offset = -1;
4963 else
4964 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4967 else
4969 unsigned int alloc;
4971 /* Assign file positions for the loaded sections based on the
4972 assignment of sections to segments. */
4973 if (!assign_file_positions_for_load_sections (abfd, link_info))
4974 return FALSE;
4976 /* And for non-load sections. */
4977 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4978 return FALSE;
4980 if (bed->elf_backend_modify_program_headers != NULL)
4982 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4983 return FALSE;
4986 /* Write out the program headers. */
4987 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4988 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4989 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4990 return FALSE;
4992 off = tdata->next_file_pos;
4995 /* Place the section headers. */
4996 off = align_file_position (off, 1 << bed->s->log_file_align);
4997 i_ehdrp->e_shoff = off;
4998 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
5000 tdata->next_file_pos = off;
5002 return TRUE;
5005 static bfd_boolean
5006 prep_headers (bfd *abfd)
5008 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
5009 struct elf_strtab_hash *shstrtab;
5010 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5012 i_ehdrp = elf_elfheader (abfd);
5014 shstrtab = _bfd_elf_strtab_init ();
5015 if (shstrtab == NULL)
5016 return FALSE;
5018 elf_shstrtab (abfd) = shstrtab;
5020 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5021 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5022 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5023 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5025 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5026 i_ehdrp->e_ident[EI_DATA] =
5027 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5028 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5030 if ((abfd->flags & DYNAMIC) != 0)
5031 i_ehdrp->e_type = ET_DYN;
5032 else if ((abfd->flags & EXEC_P) != 0)
5033 i_ehdrp->e_type = ET_EXEC;
5034 else if (bfd_get_format (abfd) == bfd_core)
5035 i_ehdrp->e_type = ET_CORE;
5036 else
5037 i_ehdrp->e_type = ET_REL;
5039 switch (bfd_get_arch (abfd))
5041 case bfd_arch_unknown:
5042 i_ehdrp->e_machine = EM_NONE;
5043 break;
5045 /* There used to be a long list of cases here, each one setting
5046 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5047 in the corresponding bfd definition. To avoid duplication,
5048 the switch was removed. Machines that need special handling
5049 can generally do it in elf_backend_final_write_processing(),
5050 unless they need the information earlier than the final write.
5051 Such need can generally be supplied by replacing the tests for
5052 e_machine with the conditions used to determine it. */
5053 default:
5054 i_ehdrp->e_machine = bed->elf_machine_code;
5057 i_ehdrp->e_version = bed->s->ev_current;
5058 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5060 /* No program header, for now. */
5061 i_ehdrp->e_phoff = 0;
5062 i_ehdrp->e_phentsize = 0;
5063 i_ehdrp->e_phnum = 0;
5065 /* Each bfd section is section header entry. */
5066 i_ehdrp->e_entry = bfd_get_start_address (abfd);
5067 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5069 /* If we're building an executable, we'll need a program header table. */
5070 if (abfd->flags & EXEC_P)
5071 /* It all happens later. */
5073 else
5075 i_ehdrp->e_phentsize = 0;
5076 i_ehdrp->e_phoff = 0;
5079 elf_tdata (abfd)->symtab_hdr.sh_name =
5080 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5081 elf_tdata (abfd)->strtab_hdr.sh_name =
5082 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5083 elf_tdata (abfd)->shstrtab_hdr.sh_name =
5084 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5085 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5086 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5087 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5088 return FALSE;
5090 return TRUE;
5093 /* Assign file positions for all the reloc sections which are not part
5094 of the loadable file image. */
5096 void
5097 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
5099 file_ptr off;
5100 unsigned int i, num_sec;
5101 Elf_Internal_Shdr **shdrpp;
5103 off = elf_tdata (abfd)->next_file_pos;
5105 num_sec = elf_numsections (abfd);
5106 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5108 Elf_Internal_Shdr *shdrp;
5110 shdrp = *shdrpp;
5111 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5112 && shdrp->sh_offset == -1)
5113 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5116 elf_tdata (abfd)->next_file_pos = off;
5119 bfd_boolean
5120 _bfd_elf_write_object_contents (bfd *abfd)
5122 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5123 Elf_Internal_Shdr **i_shdrp;
5124 bfd_boolean failed;
5125 unsigned int count, num_sec;
5127 if (! abfd->output_has_begun
5128 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5129 return FALSE;
5131 i_shdrp = elf_elfsections (abfd);
5133 failed = FALSE;
5134 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5135 if (failed)
5136 return FALSE;
5138 _bfd_elf_assign_file_positions_for_relocs (abfd);
5140 /* After writing the headers, we need to write the sections too... */
5141 num_sec = elf_numsections (abfd);
5142 for (count = 1; count < num_sec; count++)
5144 if (bed->elf_backend_section_processing)
5145 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5146 if (i_shdrp[count]->contents)
5148 bfd_size_type amt = i_shdrp[count]->sh_size;
5150 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5151 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5152 return FALSE;
5156 /* Write out the section header names. */
5157 if (elf_shstrtab (abfd) != NULL
5158 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5159 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5160 return FALSE;
5162 if (bed->elf_backend_final_write_processing)
5163 (*bed->elf_backend_final_write_processing) (abfd,
5164 elf_tdata (abfd)->linker);
5166 if (!bed->s->write_shdrs_and_ehdr (abfd))
5167 return FALSE;
5169 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5170 if (elf_tdata (abfd)->after_write_object_contents)
5171 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
5173 return TRUE;
5176 bfd_boolean
5177 _bfd_elf_write_corefile_contents (bfd *abfd)
5179 /* Hopefully this can be done just like an object file. */
5180 return _bfd_elf_write_object_contents (abfd);
5183 /* Given a section, search the header to find them. */
5185 unsigned int
5186 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5188 const struct elf_backend_data *bed;
5189 unsigned int sec_index;
5191 if (elf_section_data (asect) != NULL
5192 && elf_section_data (asect)->this_idx != 0)
5193 return elf_section_data (asect)->this_idx;
5195 if (bfd_is_abs_section (asect))
5196 sec_index = SHN_ABS;
5197 else if (bfd_is_com_section (asect))
5198 sec_index = SHN_COMMON;
5199 else if (bfd_is_und_section (asect))
5200 sec_index = SHN_UNDEF;
5201 else
5202 sec_index = SHN_BAD;
5204 bed = get_elf_backend_data (abfd);
5205 if (bed->elf_backend_section_from_bfd_section)
5207 int retval = sec_index;
5209 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5210 return retval;
5213 if (sec_index == SHN_BAD)
5214 bfd_set_error (bfd_error_nonrepresentable_section);
5216 return sec_index;
5219 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5220 on error. */
5223 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5225 asymbol *asym_ptr = *asym_ptr_ptr;
5226 int idx;
5227 flagword flags = asym_ptr->flags;
5229 /* When gas creates relocations against local labels, it creates its
5230 own symbol for the section, but does put the symbol into the
5231 symbol chain, so udata is 0. When the linker is generating
5232 relocatable output, this section symbol may be for one of the
5233 input sections rather than the output section. */
5234 if (asym_ptr->udata.i == 0
5235 && (flags & BSF_SECTION_SYM)
5236 && asym_ptr->section)
5238 asection *sec;
5239 int indx;
5241 sec = asym_ptr->section;
5242 if (sec->owner != abfd && sec->output_section != NULL)
5243 sec = sec->output_section;
5244 if (sec->owner == abfd
5245 && (indx = sec->index) < elf_num_section_syms (abfd)
5246 && elf_section_syms (abfd)[indx] != NULL)
5247 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5250 idx = asym_ptr->udata.i;
5252 if (idx == 0)
5254 /* This case can occur when using --strip-symbol on a symbol
5255 which is used in a relocation entry. */
5256 (*_bfd_error_handler)
5257 (_("%B: symbol `%s' required but not present"),
5258 abfd, bfd_asymbol_name (asym_ptr));
5259 bfd_set_error (bfd_error_no_symbols);
5260 return -1;
5263 #if DEBUG & 4
5265 fprintf (stderr,
5266 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5267 (long) asym_ptr, asym_ptr->name, idx, (long) flags);
5268 fflush (stderr);
5270 #endif
5272 return idx;
5275 /* Rewrite program header information. */
5277 static bfd_boolean
5278 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5280 Elf_Internal_Ehdr *iehdr;
5281 struct elf_segment_map *map;
5282 struct elf_segment_map *map_first;
5283 struct elf_segment_map **pointer_to_map;
5284 Elf_Internal_Phdr *segment;
5285 asection *section;
5286 unsigned int i;
5287 unsigned int num_segments;
5288 bfd_boolean phdr_included = FALSE;
5289 bfd_boolean p_paddr_valid;
5290 bfd_vma maxpagesize;
5291 struct elf_segment_map *phdr_adjust_seg = NULL;
5292 unsigned int phdr_adjust_num = 0;
5293 const struct elf_backend_data *bed;
5295 bed = get_elf_backend_data (ibfd);
5296 iehdr = elf_elfheader (ibfd);
5298 map_first = NULL;
5299 pointer_to_map = &map_first;
5301 num_segments = elf_elfheader (ibfd)->e_phnum;
5302 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5304 /* Returns the end address of the segment + 1. */
5305 #define SEGMENT_END(segment, start) \
5306 (start + (segment->p_memsz > segment->p_filesz \
5307 ? segment->p_memsz : segment->p_filesz))
5309 #define SECTION_SIZE(section, segment) \
5310 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5311 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5312 ? section->size : 0)
5314 /* Returns TRUE if the given section is contained within
5315 the given segment. VMA addresses are compared. */
5316 #define IS_CONTAINED_BY_VMA(section, segment) \
5317 (section->vma >= segment->p_vaddr \
5318 && (section->vma + SECTION_SIZE (section, segment) \
5319 <= (SEGMENT_END (segment, segment->p_vaddr))))
5321 /* Returns TRUE if the given section is contained within
5322 the given segment. LMA addresses are compared. */
5323 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5324 (section->lma >= base \
5325 && (section->lma + SECTION_SIZE (section, segment) \
5326 <= SEGMENT_END (segment, base)))
5328 /* Handle PT_NOTE segment. */
5329 #define IS_NOTE(p, s) \
5330 (p->p_type == PT_NOTE \
5331 && elf_section_type (s) == SHT_NOTE \
5332 && (bfd_vma) s->filepos >= p->p_offset \
5333 && ((bfd_vma) s->filepos + s->size \
5334 <= p->p_offset + p->p_filesz))
5336 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5337 etc. */
5338 #define IS_COREFILE_NOTE(p, s) \
5339 (IS_NOTE (p, s) \
5340 && bfd_get_format (ibfd) == bfd_core \
5341 && s->vma == 0 \
5342 && s->lma == 0)
5344 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5345 linker, which generates a PT_INTERP section with p_vaddr and
5346 p_memsz set to 0. */
5347 #define IS_SOLARIS_PT_INTERP(p, s) \
5348 (p->p_vaddr == 0 \
5349 && p->p_paddr == 0 \
5350 && p->p_memsz == 0 \
5351 && p->p_filesz > 0 \
5352 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5353 && s->size > 0 \
5354 && (bfd_vma) s->filepos >= p->p_offset \
5355 && ((bfd_vma) s->filepos + s->size \
5356 <= p->p_offset + p->p_filesz))
5358 /* Decide if the given section should be included in the given segment.
5359 A section will be included if:
5360 1. It is within the address space of the segment -- we use the LMA
5361 if that is set for the segment and the VMA otherwise,
5362 2. It is an allocated section or a NOTE section in a PT_NOTE
5363 segment.
5364 3. There is an output section associated with it,
5365 4. The section has not already been allocated to a previous segment.
5366 5. PT_GNU_STACK segments do not include any sections.
5367 6. PT_TLS segment includes only SHF_TLS sections.
5368 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5369 8. PT_DYNAMIC should not contain empty sections at the beginning
5370 (with the possible exception of .dynamic). */
5371 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5372 ((((segment->p_paddr \
5373 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5374 : IS_CONTAINED_BY_VMA (section, segment)) \
5375 && (section->flags & SEC_ALLOC) != 0) \
5376 || IS_NOTE (segment, section)) \
5377 && segment->p_type != PT_GNU_STACK \
5378 && (segment->p_type != PT_TLS \
5379 || (section->flags & SEC_THREAD_LOCAL)) \
5380 && (segment->p_type == PT_LOAD \
5381 || segment->p_type == PT_TLS \
5382 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5383 && (segment->p_type != PT_DYNAMIC \
5384 || SECTION_SIZE (section, segment) > 0 \
5385 || (segment->p_paddr \
5386 ? segment->p_paddr != section->lma \
5387 : segment->p_vaddr != section->vma) \
5388 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5389 == 0)) \
5390 && !section->segment_mark)
5392 /* If the output section of a section in the input segment is NULL,
5393 it is removed from the corresponding output segment. */
5394 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5395 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5396 && section->output_section != NULL)
5398 /* Returns TRUE iff seg1 starts after the end of seg2. */
5399 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5400 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5402 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5403 their VMA address ranges and their LMA address ranges overlap.
5404 It is possible to have overlapping VMA ranges without overlapping LMA
5405 ranges. RedBoot images for example can have both .data and .bss mapped
5406 to the same VMA range, but with the .data section mapped to a different
5407 LMA. */
5408 #define SEGMENT_OVERLAPS(seg1, seg2) \
5409 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5410 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5411 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5412 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5414 /* Initialise the segment mark field. */
5415 for (section = ibfd->sections; section != NULL; section = section->next)
5416 section->segment_mark = FALSE;
5418 /* The Solaris linker creates program headers in which all the
5419 p_paddr fields are zero. When we try to objcopy or strip such a
5420 file, we get confused. Check for this case, and if we find it
5421 don't set the p_paddr_valid fields. */
5422 p_paddr_valid = FALSE;
5423 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5424 i < num_segments;
5425 i++, segment++)
5426 if (segment->p_paddr != 0)
5428 p_paddr_valid = TRUE;
5429 break;
5432 /* Scan through the segments specified in the program header
5433 of the input BFD. For this first scan we look for overlaps
5434 in the loadable segments. These can be created by weird
5435 parameters to objcopy. Also, fix some solaris weirdness. */
5436 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5437 i < num_segments;
5438 i++, segment++)
5440 unsigned int j;
5441 Elf_Internal_Phdr *segment2;
5443 if (segment->p_type == PT_INTERP)
5444 for (section = ibfd->sections; section; section = section->next)
5445 if (IS_SOLARIS_PT_INTERP (segment, section))
5447 /* Mininal change so that the normal section to segment
5448 assignment code will work. */
5449 segment->p_vaddr = section->vma;
5450 break;
5453 if (segment->p_type != PT_LOAD)
5455 /* Remove PT_GNU_RELRO segment. */
5456 if (segment->p_type == PT_GNU_RELRO)
5457 segment->p_type = PT_NULL;
5458 continue;
5461 /* Determine if this segment overlaps any previous segments. */
5462 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5464 bfd_signed_vma extra_length;
5466 if (segment2->p_type != PT_LOAD
5467 || !SEGMENT_OVERLAPS (segment, segment2))
5468 continue;
5470 /* Merge the two segments together. */
5471 if (segment2->p_vaddr < segment->p_vaddr)
5473 /* Extend SEGMENT2 to include SEGMENT and then delete
5474 SEGMENT. */
5475 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5476 - SEGMENT_END (segment2, segment2->p_vaddr));
5478 if (extra_length > 0)
5480 segment2->p_memsz += extra_length;
5481 segment2->p_filesz += extra_length;
5484 segment->p_type = PT_NULL;
5486 /* Since we have deleted P we must restart the outer loop. */
5487 i = 0;
5488 segment = elf_tdata (ibfd)->phdr;
5489 break;
5491 else
5493 /* Extend SEGMENT to include SEGMENT2 and then delete
5494 SEGMENT2. */
5495 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5496 - SEGMENT_END (segment, segment->p_vaddr));
5498 if (extra_length > 0)
5500 segment->p_memsz += extra_length;
5501 segment->p_filesz += extra_length;
5504 segment2->p_type = PT_NULL;
5509 /* The second scan attempts to assign sections to segments. */
5510 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5511 i < num_segments;
5512 i++, segment++)
5514 unsigned int section_count;
5515 asection **sections;
5516 asection *output_section;
5517 unsigned int isec;
5518 bfd_vma matching_lma;
5519 bfd_vma suggested_lma;
5520 unsigned int j;
5521 bfd_size_type amt;
5522 asection *first_section;
5523 bfd_boolean first_matching_lma;
5524 bfd_boolean first_suggested_lma;
5526 if (segment->p_type == PT_NULL)
5527 continue;
5529 first_section = NULL;
5530 /* Compute how many sections might be placed into this segment. */
5531 for (section = ibfd->sections, section_count = 0;
5532 section != NULL;
5533 section = section->next)
5535 /* Find the first section in the input segment, which may be
5536 removed from the corresponding output segment. */
5537 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5539 if (first_section == NULL)
5540 first_section = section;
5541 if (section->output_section != NULL)
5542 ++section_count;
5546 /* Allocate a segment map big enough to contain
5547 all of the sections we have selected. */
5548 amt = sizeof (struct elf_segment_map);
5549 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5550 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5551 if (map == NULL)
5552 return FALSE;
5554 /* Initialise the fields of the segment map. Default to
5555 using the physical address of the segment in the input BFD. */
5556 map->next = NULL;
5557 map->p_type = segment->p_type;
5558 map->p_flags = segment->p_flags;
5559 map->p_flags_valid = 1;
5561 /* If the first section in the input segment is removed, there is
5562 no need to preserve segment physical address in the corresponding
5563 output segment. */
5564 if (!first_section || first_section->output_section != NULL)
5566 map->p_paddr = segment->p_paddr;
5567 map->p_paddr_valid = p_paddr_valid;
5570 /* Determine if this segment contains the ELF file header
5571 and if it contains the program headers themselves. */
5572 map->includes_filehdr = (segment->p_offset == 0
5573 && segment->p_filesz >= iehdr->e_ehsize);
5574 map->includes_phdrs = 0;
5576 if (!phdr_included || segment->p_type != PT_LOAD)
5578 map->includes_phdrs =
5579 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5580 && (segment->p_offset + segment->p_filesz
5581 >= ((bfd_vma) iehdr->e_phoff
5582 + iehdr->e_phnum * iehdr->e_phentsize)));
5584 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5585 phdr_included = TRUE;
5588 if (section_count == 0)
5590 /* Special segments, such as the PT_PHDR segment, may contain
5591 no sections, but ordinary, loadable segments should contain
5592 something. They are allowed by the ELF spec however, so only
5593 a warning is produced. */
5594 if (segment->p_type == PT_LOAD)
5595 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5596 " detected, is this intentional ?\n"),
5597 ibfd);
5599 map->count = 0;
5600 *pointer_to_map = map;
5601 pointer_to_map = &map->next;
5603 continue;
5606 /* Now scan the sections in the input BFD again and attempt
5607 to add their corresponding output sections to the segment map.
5608 The problem here is how to handle an output section which has
5609 been moved (ie had its LMA changed). There are four possibilities:
5611 1. None of the sections have been moved.
5612 In this case we can continue to use the segment LMA from the
5613 input BFD.
5615 2. All of the sections have been moved by the same amount.
5616 In this case we can change the segment's LMA to match the LMA
5617 of the first section.
5619 3. Some of the sections have been moved, others have not.
5620 In this case those sections which have not been moved can be
5621 placed in the current segment which will have to have its size,
5622 and possibly its LMA changed, and a new segment or segments will
5623 have to be created to contain the other sections.
5625 4. The sections have been moved, but not by the same amount.
5626 In this case we can change the segment's LMA to match the LMA
5627 of the first section and we will have to create a new segment
5628 or segments to contain the other sections.
5630 In order to save time, we allocate an array to hold the section
5631 pointers that we are interested in. As these sections get assigned
5632 to a segment, they are removed from this array. */
5634 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5635 if (sections == NULL)
5636 return FALSE;
5638 /* Step One: Scan for segment vs section LMA conflicts.
5639 Also add the sections to the section array allocated above.
5640 Also add the sections to the current segment. In the common
5641 case, where the sections have not been moved, this means that
5642 we have completely filled the segment, and there is nothing
5643 more to do. */
5644 isec = 0;
5645 matching_lma = 0;
5646 suggested_lma = 0;
5647 first_matching_lma = TRUE;
5648 first_suggested_lma = TRUE;
5650 for (section = ibfd->sections;
5651 section != NULL;
5652 section = section->next)
5653 if (section == first_section)
5654 break;
5656 for (j = 0; section != NULL; section = section->next)
5658 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5660 output_section = section->output_section;
5662 sections[j++] = section;
5664 /* The Solaris native linker always sets p_paddr to 0.
5665 We try to catch that case here, and set it to the
5666 correct value. Note - some backends require that
5667 p_paddr be left as zero. */
5668 if (!p_paddr_valid
5669 && segment->p_vaddr != 0
5670 && !bed->want_p_paddr_set_to_zero
5671 && isec == 0
5672 && output_section->lma != 0
5673 && output_section->vma == (segment->p_vaddr
5674 + (map->includes_filehdr
5675 ? iehdr->e_ehsize
5676 : 0)
5677 + (map->includes_phdrs
5678 ? (iehdr->e_phnum
5679 * iehdr->e_phentsize)
5680 : 0)))
5681 map->p_paddr = segment->p_vaddr;
5683 /* Match up the physical address of the segment with the
5684 LMA address of the output section. */
5685 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5686 || IS_COREFILE_NOTE (segment, section)
5687 || (bed->want_p_paddr_set_to_zero
5688 && IS_CONTAINED_BY_VMA (output_section, segment)))
5690 if (first_matching_lma || output_section->lma < matching_lma)
5692 matching_lma = output_section->lma;
5693 first_matching_lma = FALSE;
5696 /* We assume that if the section fits within the segment
5697 then it does not overlap any other section within that
5698 segment. */
5699 map->sections[isec++] = output_section;
5701 else if (first_suggested_lma)
5703 suggested_lma = output_section->lma;
5704 first_suggested_lma = FALSE;
5707 if (j == section_count)
5708 break;
5712 BFD_ASSERT (j == section_count);
5714 /* Step Two: Adjust the physical address of the current segment,
5715 if necessary. */
5716 if (isec == section_count)
5718 /* All of the sections fitted within the segment as currently
5719 specified. This is the default case. Add the segment to
5720 the list of built segments and carry on to process the next
5721 program header in the input BFD. */
5722 map->count = section_count;
5723 *pointer_to_map = map;
5724 pointer_to_map = &map->next;
5726 if (p_paddr_valid
5727 && !bed->want_p_paddr_set_to_zero
5728 && matching_lma != map->p_paddr
5729 && !map->includes_filehdr
5730 && !map->includes_phdrs)
5731 /* There is some padding before the first section in the
5732 segment. So, we must account for that in the output
5733 segment's vma. */
5734 map->p_vaddr_offset = matching_lma - map->p_paddr;
5736 free (sections);
5737 continue;
5739 else
5741 if (!first_matching_lma)
5743 /* At least one section fits inside the current segment.
5744 Keep it, but modify its physical address to match the
5745 LMA of the first section that fitted. */
5746 map->p_paddr = matching_lma;
5748 else
5750 /* None of the sections fitted inside the current segment.
5751 Change the current segment's physical address to match
5752 the LMA of the first section. */
5753 map->p_paddr = suggested_lma;
5756 /* Offset the segment physical address from the lma
5757 to allow for space taken up by elf headers. */
5758 if (map->includes_filehdr)
5760 if (map->p_paddr >= iehdr->e_ehsize)
5761 map->p_paddr -= iehdr->e_ehsize;
5762 else
5764 map->includes_filehdr = FALSE;
5765 map->includes_phdrs = FALSE;
5769 if (map->includes_phdrs)
5771 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5773 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5775 /* iehdr->e_phnum is just an estimate of the number
5776 of program headers that we will need. Make a note
5777 here of the number we used and the segment we chose
5778 to hold these headers, so that we can adjust the
5779 offset when we know the correct value. */
5780 phdr_adjust_num = iehdr->e_phnum;
5781 phdr_adjust_seg = map;
5783 else
5784 map->includes_phdrs = FALSE;
5788 /* Step Three: Loop over the sections again, this time assigning
5789 those that fit to the current segment and removing them from the
5790 sections array; but making sure not to leave large gaps. Once all
5791 possible sections have been assigned to the current segment it is
5792 added to the list of built segments and if sections still remain
5793 to be assigned, a new segment is constructed before repeating
5794 the loop. */
5795 isec = 0;
5798 map->count = 0;
5799 suggested_lma = 0;
5800 first_suggested_lma = TRUE;
5802 /* Fill the current segment with sections that fit. */
5803 for (j = 0; j < section_count; j++)
5805 section = sections[j];
5807 if (section == NULL)
5808 continue;
5810 output_section = section->output_section;
5812 BFD_ASSERT (output_section != NULL);
5814 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5815 || IS_COREFILE_NOTE (segment, section))
5817 if (map->count == 0)
5819 /* If the first section in a segment does not start at
5820 the beginning of the segment, then something is
5821 wrong. */
5822 if (output_section->lma
5823 != (map->p_paddr
5824 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5825 + (map->includes_phdrs
5826 ? iehdr->e_phnum * iehdr->e_phentsize
5827 : 0)))
5828 abort ();
5830 else
5832 asection *prev_sec;
5834 prev_sec = map->sections[map->count - 1];
5836 /* If the gap between the end of the previous section
5837 and the start of this section is more than
5838 maxpagesize then we need to start a new segment. */
5839 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5840 maxpagesize)
5841 < BFD_ALIGN (output_section->lma, maxpagesize))
5842 || (prev_sec->lma + prev_sec->size
5843 > output_section->lma))
5845 if (first_suggested_lma)
5847 suggested_lma = output_section->lma;
5848 first_suggested_lma = FALSE;
5851 continue;
5855 map->sections[map->count++] = output_section;
5856 ++isec;
5857 sections[j] = NULL;
5858 section->segment_mark = TRUE;
5860 else if (first_suggested_lma)
5862 suggested_lma = output_section->lma;
5863 first_suggested_lma = FALSE;
5867 BFD_ASSERT (map->count > 0);
5869 /* Add the current segment to the list of built segments. */
5870 *pointer_to_map = map;
5871 pointer_to_map = &map->next;
5873 if (isec < section_count)
5875 /* We still have not allocated all of the sections to
5876 segments. Create a new segment here, initialise it
5877 and carry on looping. */
5878 amt = sizeof (struct elf_segment_map);
5879 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5880 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5881 if (map == NULL)
5883 free (sections);
5884 return FALSE;
5887 /* Initialise the fields of the segment map. Set the physical
5888 physical address to the LMA of the first section that has
5889 not yet been assigned. */
5890 map->next = NULL;
5891 map->p_type = segment->p_type;
5892 map->p_flags = segment->p_flags;
5893 map->p_flags_valid = 1;
5894 map->p_paddr = suggested_lma;
5895 map->p_paddr_valid = p_paddr_valid;
5896 map->includes_filehdr = 0;
5897 map->includes_phdrs = 0;
5900 while (isec < section_count);
5902 free (sections);
5905 elf_tdata (obfd)->segment_map = map_first;
5907 /* If we had to estimate the number of program headers that were
5908 going to be needed, then check our estimate now and adjust
5909 the offset if necessary. */
5910 if (phdr_adjust_seg != NULL)
5912 unsigned int count;
5914 for (count = 0, map = map_first; map != NULL; map = map->next)
5915 count++;
5917 if (count > phdr_adjust_num)
5918 phdr_adjust_seg->p_paddr
5919 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5922 #undef SEGMENT_END
5923 #undef SECTION_SIZE
5924 #undef IS_CONTAINED_BY_VMA
5925 #undef IS_CONTAINED_BY_LMA
5926 #undef IS_NOTE
5927 #undef IS_COREFILE_NOTE
5928 #undef IS_SOLARIS_PT_INTERP
5929 #undef IS_SECTION_IN_INPUT_SEGMENT
5930 #undef INCLUDE_SECTION_IN_SEGMENT
5931 #undef SEGMENT_AFTER_SEGMENT
5932 #undef SEGMENT_OVERLAPS
5933 return TRUE;
5936 /* Copy ELF program header information. */
5938 static bfd_boolean
5939 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5941 Elf_Internal_Ehdr *iehdr;
5942 struct elf_segment_map *map;
5943 struct elf_segment_map *map_first;
5944 struct elf_segment_map **pointer_to_map;
5945 Elf_Internal_Phdr *segment;
5946 unsigned int i;
5947 unsigned int num_segments;
5948 bfd_boolean phdr_included = FALSE;
5949 bfd_boolean p_paddr_valid;
5951 iehdr = elf_elfheader (ibfd);
5953 map_first = NULL;
5954 pointer_to_map = &map_first;
5956 /* If all the segment p_paddr fields are zero, don't set
5957 map->p_paddr_valid. */
5958 p_paddr_valid = FALSE;
5959 num_segments = elf_elfheader (ibfd)->e_phnum;
5960 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5961 i < num_segments;
5962 i++, segment++)
5963 if (segment->p_paddr != 0)
5965 p_paddr_valid = TRUE;
5966 break;
5969 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5970 i < num_segments;
5971 i++, segment++)
5973 asection *section;
5974 unsigned int section_count;
5975 bfd_size_type amt;
5976 Elf_Internal_Shdr *this_hdr;
5977 asection *first_section = NULL;
5978 asection *lowest_section;
5980 /* Compute how many sections are in this segment. */
5981 for (section = ibfd->sections, section_count = 0;
5982 section != NULL;
5983 section = section->next)
5985 this_hdr = &(elf_section_data(section)->this_hdr);
5986 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
5988 if (first_section == NULL)
5989 first_section = section;
5990 section_count++;
5994 /* Allocate a segment map big enough to contain
5995 all of the sections we have selected. */
5996 amt = sizeof (struct elf_segment_map);
5997 if (section_count != 0)
5998 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5999 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6000 if (map == NULL)
6001 return FALSE;
6003 /* Initialize the fields of the output segment map with the
6004 input segment. */
6005 map->next = NULL;
6006 map->p_type = segment->p_type;
6007 map->p_flags = segment->p_flags;
6008 map->p_flags_valid = 1;
6009 map->p_paddr = segment->p_paddr;
6010 map->p_paddr_valid = p_paddr_valid;
6011 map->p_align = segment->p_align;
6012 map->p_align_valid = 1;
6013 map->p_vaddr_offset = 0;
6015 if (map->p_type == PT_GNU_RELRO)
6017 /* The PT_GNU_RELRO segment may contain the first a few
6018 bytes in the .got.plt section even if the whole .got.plt
6019 section isn't in the PT_GNU_RELRO segment. We won't
6020 change the size of the PT_GNU_RELRO segment. */
6021 map->p_size = segment->p_memsz;
6022 map->p_size_valid = 1;
6025 /* Determine if this segment contains the ELF file header
6026 and if it contains the program headers themselves. */
6027 map->includes_filehdr = (segment->p_offset == 0
6028 && segment->p_filesz >= iehdr->e_ehsize);
6030 map->includes_phdrs = 0;
6031 if (! phdr_included || segment->p_type != PT_LOAD)
6033 map->includes_phdrs =
6034 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6035 && (segment->p_offset + segment->p_filesz
6036 >= ((bfd_vma) iehdr->e_phoff
6037 + iehdr->e_phnum * iehdr->e_phentsize)));
6039 if (segment->p_type == PT_LOAD && map->includes_phdrs)
6040 phdr_included = TRUE;
6043 lowest_section = first_section;
6044 if (section_count != 0)
6046 unsigned int isec = 0;
6048 for (section = first_section;
6049 section != NULL;
6050 section = section->next)
6052 this_hdr = &(elf_section_data(section)->this_hdr);
6053 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6055 map->sections[isec++] = section->output_section;
6056 if (section->lma < lowest_section->lma)
6057 lowest_section = section;
6058 if ((section->flags & SEC_ALLOC) != 0)
6060 bfd_vma seg_off;
6062 /* Section lmas are set up from PT_LOAD header
6063 p_paddr in _bfd_elf_make_section_from_shdr.
6064 If this header has a p_paddr that disagrees
6065 with the section lma, flag the p_paddr as
6066 invalid. */
6067 if ((section->flags & SEC_LOAD) != 0)
6068 seg_off = this_hdr->sh_offset - segment->p_offset;
6069 else
6070 seg_off = this_hdr->sh_addr - segment->p_vaddr;
6071 if (section->lma - segment->p_paddr != seg_off)
6072 map->p_paddr_valid = FALSE;
6074 if (isec == section_count)
6075 break;
6080 if (map->includes_filehdr && lowest_section != NULL)
6081 /* We need to keep the space used by the headers fixed. */
6082 map->header_size = lowest_section->vma - segment->p_vaddr;
6084 if (!map->includes_phdrs
6085 && !map->includes_filehdr
6086 && map->p_paddr_valid)
6087 /* There is some other padding before the first section. */
6088 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6089 - segment->p_paddr);
6091 map->count = section_count;
6092 *pointer_to_map = map;
6093 pointer_to_map = &map->next;
6096 elf_tdata (obfd)->segment_map = map_first;
6097 return TRUE;
6100 /* Copy private BFD data. This copies or rewrites ELF program header
6101 information. */
6103 static bfd_boolean
6104 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6106 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6107 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6108 return TRUE;
6110 if (elf_tdata (ibfd)->phdr == NULL)
6111 return TRUE;
6113 if (ibfd->xvec == obfd->xvec)
6115 /* Check to see if any sections in the input BFD
6116 covered by ELF program header have changed. */
6117 Elf_Internal_Phdr *segment;
6118 asection *section, *osec;
6119 unsigned int i, num_segments;
6120 Elf_Internal_Shdr *this_hdr;
6121 const struct elf_backend_data *bed;
6123 bed = get_elf_backend_data (ibfd);
6125 /* Regenerate the segment map if p_paddr is set to 0. */
6126 if (bed->want_p_paddr_set_to_zero)
6127 goto rewrite;
6129 /* Initialize the segment mark field. */
6130 for (section = obfd->sections; section != NULL;
6131 section = section->next)
6132 section->segment_mark = FALSE;
6134 num_segments = elf_elfheader (ibfd)->e_phnum;
6135 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6136 i < num_segments;
6137 i++, segment++)
6139 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6140 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6141 which severly confuses things, so always regenerate the segment
6142 map in this case. */
6143 if (segment->p_paddr == 0
6144 && segment->p_memsz == 0
6145 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6146 goto rewrite;
6148 for (section = ibfd->sections;
6149 section != NULL; section = section->next)
6151 /* We mark the output section so that we know it comes
6152 from the input BFD. */
6153 osec = section->output_section;
6154 if (osec)
6155 osec->segment_mark = TRUE;
6157 /* Check if this section is covered by the segment. */
6158 this_hdr = &(elf_section_data(section)->this_hdr);
6159 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6161 /* FIXME: Check if its output section is changed or
6162 removed. What else do we need to check? */
6163 if (osec == NULL
6164 || section->flags != osec->flags
6165 || section->lma != osec->lma
6166 || section->vma != osec->vma
6167 || section->size != osec->size
6168 || section->rawsize != osec->rawsize
6169 || section->alignment_power != osec->alignment_power)
6170 goto rewrite;
6175 /* Check to see if any output section do not come from the
6176 input BFD. */
6177 for (section = obfd->sections; section != NULL;
6178 section = section->next)
6180 if (section->segment_mark == FALSE)
6181 goto rewrite;
6182 else
6183 section->segment_mark = FALSE;
6186 return copy_elf_program_header (ibfd, obfd);
6189 rewrite:
6190 return rewrite_elf_program_header (ibfd, obfd);
6193 /* Initialize private output section information from input section. */
6195 bfd_boolean
6196 _bfd_elf_init_private_section_data (bfd *ibfd,
6197 asection *isec,
6198 bfd *obfd,
6199 asection *osec,
6200 struct bfd_link_info *link_info)
6203 Elf_Internal_Shdr *ihdr, *ohdr;
6204 bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6206 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6207 || obfd->xvec->flavour != bfd_target_elf_flavour)
6208 return TRUE;
6210 /* For objcopy and relocatable link, don't copy the output ELF
6211 section type from input if the output BFD section flags have been
6212 set to something different. For a final link allow some flags
6213 that the linker clears to differ. */
6214 if (elf_section_type (osec) == SHT_NULL
6215 && (osec->flags == isec->flags
6216 || (final_link
6217 && ((osec->flags ^ isec->flags)
6218 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6219 elf_section_type (osec) = elf_section_type (isec);
6221 /* FIXME: Is this correct for all OS/PROC specific flags? */
6222 elf_section_flags (osec) |= (elf_section_flags (isec)
6223 & (SHF_MASKOS | SHF_MASKPROC));
6225 /* Set things up for objcopy and relocatable link. The output
6226 SHT_GROUP section will have its elf_next_in_group pointing back
6227 to the input group members. Ignore linker created group section.
6228 See elfNN_ia64_object_p in elfxx-ia64.c. */
6229 if (!final_link)
6231 if (elf_sec_group (isec) == NULL
6232 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6234 if (elf_section_flags (isec) & SHF_GROUP)
6235 elf_section_flags (osec) |= SHF_GROUP;
6236 elf_next_in_group (osec) = elf_next_in_group (isec);
6237 elf_section_data (osec)->group = elf_section_data (isec)->group;
6241 ihdr = &elf_section_data (isec)->this_hdr;
6243 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6244 don't use the output section of the linked-to section since it
6245 may be NULL at this point. */
6246 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6248 ohdr = &elf_section_data (osec)->this_hdr;
6249 ohdr->sh_flags |= SHF_LINK_ORDER;
6250 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6253 osec->use_rela_p = isec->use_rela_p;
6255 return TRUE;
6258 /* Copy private section information. This copies over the entsize
6259 field, and sometimes the info field. */
6261 bfd_boolean
6262 _bfd_elf_copy_private_section_data (bfd *ibfd,
6263 asection *isec,
6264 bfd *obfd,
6265 asection *osec)
6267 Elf_Internal_Shdr *ihdr, *ohdr;
6269 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6270 || obfd->xvec->flavour != bfd_target_elf_flavour)
6271 return TRUE;
6273 ihdr = &elf_section_data (isec)->this_hdr;
6274 ohdr = &elf_section_data (osec)->this_hdr;
6276 ohdr->sh_entsize = ihdr->sh_entsize;
6278 if (ihdr->sh_type == SHT_SYMTAB
6279 || ihdr->sh_type == SHT_DYNSYM
6280 || ihdr->sh_type == SHT_GNU_verneed
6281 || ihdr->sh_type == SHT_GNU_verdef)
6282 ohdr->sh_info = ihdr->sh_info;
6284 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6285 NULL);
6288 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6289 necessary if we are removing either the SHT_GROUP section or any of
6290 the group member sections. DISCARDED is the value that a section's
6291 output_section has if the section will be discarded, NULL when this
6292 function is called from objcopy, bfd_abs_section_ptr when called
6293 from the linker. */
6295 bfd_boolean
6296 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6298 asection *isec;
6300 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6301 if (elf_section_type (isec) == SHT_GROUP)
6303 asection *first = elf_next_in_group (isec);
6304 asection *s = first;
6305 bfd_size_type removed = 0;
6307 while (s != NULL)
6309 /* If this member section is being output but the
6310 SHT_GROUP section is not, then clear the group info
6311 set up by _bfd_elf_copy_private_section_data. */
6312 if (s->output_section != discarded
6313 && isec->output_section == discarded)
6315 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6316 elf_group_name (s->output_section) = NULL;
6318 /* Conversely, if the member section is not being output
6319 but the SHT_GROUP section is, then adjust its size. */
6320 else if (s->output_section == discarded
6321 && isec->output_section != discarded)
6322 removed += 4;
6323 s = elf_next_in_group (s);
6324 if (s == first)
6325 break;
6327 if (removed != 0)
6329 if (discarded != NULL)
6331 /* If we've been called for ld -r, then we need to
6332 adjust the input section size. This function may
6333 be called multiple times, so save the original
6334 size. */
6335 if (isec->rawsize == 0)
6336 isec->rawsize = isec->size;
6337 isec->size = isec->rawsize - removed;
6339 else
6341 /* Adjust the output section size when called from
6342 objcopy. */
6343 isec->output_section->size -= removed;
6348 return TRUE;
6351 /* Copy private header information. */
6353 bfd_boolean
6354 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6356 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6357 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6358 return TRUE;
6360 /* Copy over private BFD data if it has not already been copied.
6361 This must be done here, rather than in the copy_private_bfd_data
6362 entry point, because the latter is called after the section
6363 contents have been set, which means that the program headers have
6364 already been worked out. */
6365 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6367 if (! copy_private_bfd_data (ibfd, obfd))
6368 return FALSE;
6371 return _bfd_elf_fixup_group_sections (ibfd, NULL);
6374 /* Copy private symbol information. If this symbol is in a section
6375 which we did not map into a BFD section, try to map the section
6376 index correctly. We use special macro definitions for the mapped
6377 section indices; these definitions are interpreted by the
6378 swap_out_syms function. */
6380 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6381 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6382 #define MAP_STRTAB (SHN_HIOS + 3)
6383 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6384 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6386 bfd_boolean
6387 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6388 asymbol *isymarg,
6389 bfd *obfd,
6390 asymbol *osymarg)
6392 elf_symbol_type *isym, *osym;
6394 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6395 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6396 return TRUE;
6398 isym = elf_symbol_from (ibfd, isymarg);
6399 osym = elf_symbol_from (obfd, osymarg);
6401 if (isym != NULL
6402 && isym->internal_elf_sym.st_shndx != 0
6403 && osym != NULL
6404 && bfd_is_abs_section (isym->symbol.section))
6406 unsigned int shndx;
6408 shndx = isym->internal_elf_sym.st_shndx;
6409 if (shndx == elf_onesymtab (ibfd))
6410 shndx = MAP_ONESYMTAB;
6411 else if (shndx == elf_dynsymtab (ibfd))
6412 shndx = MAP_DYNSYMTAB;
6413 else if (shndx == elf_tdata (ibfd)->strtab_section)
6414 shndx = MAP_STRTAB;
6415 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6416 shndx = MAP_SHSTRTAB;
6417 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6418 shndx = MAP_SYM_SHNDX;
6419 osym->internal_elf_sym.st_shndx = shndx;
6422 return TRUE;
6425 /* Swap out the symbols. */
6427 static bfd_boolean
6428 swap_out_syms (bfd *abfd,
6429 struct bfd_strtab_hash **sttp,
6430 int relocatable_p)
6432 const struct elf_backend_data *bed;
6433 int symcount;
6434 asymbol **syms;
6435 struct bfd_strtab_hash *stt;
6436 Elf_Internal_Shdr *symtab_hdr;
6437 Elf_Internal_Shdr *symtab_shndx_hdr;
6438 Elf_Internal_Shdr *symstrtab_hdr;
6439 bfd_byte *outbound_syms;
6440 bfd_byte *outbound_shndx;
6441 int idx;
6442 bfd_size_type amt;
6443 bfd_boolean name_local_sections;
6445 if (!elf_map_symbols (abfd))
6446 return FALSE;
6448 /* Dump out the symtabs. */
6449 stt = _bfd_elf_stringtab_init ();
6450 if (stt == NULL)
6451 return FALSE;
6453 bed = get_elf_backend_data (abfd);
6454 symcount = bfd_get_symcount (abfd);
6455 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6456 symtab_hdr->sh_type = SHT_SYMTAB;
6457 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6458 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6459 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6460 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6462 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6463 symstrtab_hdr->sh_type = SHT_STRTAB;
6465 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6466 bed->s->sizeof_sym);
6467 if (outbound_syms == NULL)
6469 _bfd_stringtab_free (stt);
6470 return FALSE;
6472 symtab_hdr->contents = outbound_syms;
6474 outbound_shndx = NULL;
6475 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6476 if (symtab_shndx_hdr->sh_name != 0)
6478 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6479 outbound_shndx = (bfd_byte *)
6480 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6481 if (outbound_shndx == NULL)
6483 _bfd_stringtab_free (stt);
6484 return FALSE;
6487 symtab_shndx_hdr->contents = outbound_shndx;
6488 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6489 symtab_shndx_hdr->sh_size = amt;
6490 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6491 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6494 /* Now generate the data (for "contents"). */
6496 /* Fill in zeroth symbol and swap it out. */
6497 Elf_Internal_Sym sym;
6498 sym.st_name = 0;
6499 sym.st_value = 0;
6500 sym.st_size = 0;
6501 sym.st_info = 0;
6502 sym.st_other = 0;
6503 sym.st_shndx = SHN_UNDEF;
6504 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6505 outbound_syms += bed->s->sizeof_sym;
6506 if (outbound_shndx != NULL)
6507 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6510 name_local_sections
6511 = (bed->elf_backend_name_local_section_symbols
6512 && bed->elf_backend_name_local_section_symbols (abfd));
6514 syms = bfd_get_outsymbols (abfd);
6515 for (idx = 0; idx < symcount; idx++)
6517 Elf_Internal_Sym sym;
6518 bfd_vma value = syms[idx]->value;
6519 elf_symbol_type *type_ptr;
6520 flagword flags = syms[idx]->flags;
6521 int type;
6523 if (!name_local_sections
6524 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6526 /* Local section symbols have no name. */
6527 sym.st_name = 0;
6529 else
6531 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6532 syms[idx]->name,
6533 TRUE, FALSE);
6534 if (sym.st_name == (unsigned long) -1)
6536 _bfd_stringtab_free (stt);
6537 return FALSE;
6541 type_ptr = elf_symbol_from (abfd, syms[idx]);
6543 if ((flags & BSF_SECTION_SYM) == 0
6544 && bfd_is_com_section (syms[idx]->section))
6546 /* ELF common symbols put the alignment into the `value' field,
6547 and the size into the `size' field. This is backwards from
6548 how BFD handles it, so reverse it here. */
6549 sym.st_size = value;
6550 if (type_ptr == NULL
6551 || type_ptr->internal_elf_sym.st_value == 0)
6552 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6553 else
6554 sym.st_value = type_ptr->internal_elf_sym.st_value;
6555 sym.st_shndx = _bfd_elf_section_from_bfd_section
6556 (abfd, syms[idx]->section);
6558 else
6560 asection *sec = syms[idx]->section;
6561 unsigned int shndx;
6563 if (sec->output_section)
6565 value += sec->output_offset;
6566 sec = sec->output_section;
6569 /* Don't add in the section vma for relocatable output. */
6570 if (! relocatable_p)
6571 value += sec->vma;
6572 sym.st_value = value;
6573 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6575 if (bfd_is_abs_section (sec)
6576 && type_ptr != NULL
6577 && type_ptr->internal_elf_sym.st_shndx != 0)
6579 /* This symbol is in a real ELF section which we did
6580 not create as a BFD section. Undo the mapping done
6581 by copy_private_symbol_data. */
6582 shndx = type_ptr->internal_elf_sym.st_shndx;
6583 switch (shndx)
6585 case MAP_ONESYMTAB:
6586 shndx = elf_onesymtab (abfd);
6587 break;
6588 case MAP_DYNSYMTAB:
6589 shndx = elf_dynsymtab (abfd);
6590 break;
6591 case MAP_STRTAB:
6592 shndx = elf_tdata (abfd)->strtab_section;
6593 break;
6594 case MAP_SHSTRTAB:
6595 shndx = elf_tdata (abfd)->shstrtab_section;
6596 break;
6597 case MAP_SYM_SHNDX:
6598 shndx = elf_tdata (abfd)->symtab_shndx_section;
6599 break;
6600 default:
6601 break;
6604 else
6606 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6608 if (shndx == SHN_BAD)
6610 asection *sec2;
6612 /* Writing this would be a hell of a lot easier if
6613 we had some decent documentation on bfd, and
6614 knew what to expect of the library, and what to
6615 demand of applications. For example, it
6616 appears that `objcopy' might not set the
6617 section of a symbol to be a section that is
6618 actually in the output file. */
6619 sec2 = bfd_get_section_by_name (abfd, sec->name);
6620 if (sec2 == NULL)
6622 _bfd_error_handler (_("\
6623 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6624 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6625 sec->name);
6626 bfd_set_error (bfd_error_invalid_operation);
6627 _bfd_stringtab_free (stt);
6628 return FALSE;
6631 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6632 BFD_ASSERT (shndx != SHN_BAD);
6636 sym.st_shndx = shndx;
6639 if ((flags & BSF_THREAD_LOCAL) != 0)
6640 type = STT_TLS;
6641 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6642 type = STT_GNU_IFUNC;
6643 else if ((flags & BSF_FUNCTION) != 0)
6644 type = STT_FUNC;
6645 else if ((flags & BSF_OBJECT) != 0)
6646 type = STT_OBJECT;
6647 else if ((flags & BSF_RELC) != 0)
6648 type = STT_RELC;
6649 else if ((flags & BSF_SRELC) != 0)
6650 type = STT_SRELC;
6651 else
6652 type = STT_NOTYPE;
6654 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6655 type = STT_TLS;
6657 /* Processor-specific types. */
6658 if (type_ptr != NULL
6659 && bed->elf_backend_get_symbol_type)
6660 type = ((*bed->elf_backend_get_symbol_type)
6661 (&type_ptr->internal_elf_sym, type));
6663 if (flags & BSF_SECTION_SYM)
6665 if (flags & BSF_GLOBAL)
6666 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6667 else
6668 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6670 else if (bfd_is_com_section (syms[idx]->section))
6672 #ifdef USE_STT_COMMON
6673 if (type == STT_OBJECT)
6674 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6675 else
6676 #endif
6677 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6679 else if (bfd_is_und_section (syms[idx]->section))
6680 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6681 ? STB_WEAK
6682 : STB_GLOBAL),
6683 type);
6684 else if (flags & BSF_FILE)
6685 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6686 else
6688 int bind = STB_LOCAL;
6690 if (flags & BSF_LOCAL)
6691 bind = STB_LOCAL;
6692 else if (flags & BSF_GNU_UNIQUE)
6693 bind = STB_GNU_UNIQUE;
6694 else if (flags & BSF_WEAK)
6695 bind = STB_WEAK;
6696 else if (flags & BSF_GLOBAL)
6697 bind = STB_GLOBAL;
6699 sym.st_info = ELF_ST_INFO (bind, type);
6702 if (type_ptr != NULL)
6703 sym.st_other = type_ptr->internal_elf_sym.st_other;
6704 else
6705 sym.st_other = 0;
6707 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6708 outbound_syms += bed->s->sizeof_sym;
6709 if (outbound_shndx != NULL)
6710 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6713 *sttp = stt;
6714 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6715 symstrtab_hdr->sh_type = SHT_STRTAB;
6717 symstrtab_hdr->sh_flags = 0;
6718 symstrtab_hdr->sh_addr = 0;
6719 symstrtab_hdr->sh_entsize = 0;
6720 symstrtab_hdr->sh_link = 0;
6721 symstrtab_hdr->sh_info = 0;
6722 symstrtab_hdr->sh_addralign = 1;
6724 return TRUE;
6727 /* Return the number of bytes required to hold the symtab vector.
6729 Note that we base it on the count plus 1, since we will null terminate
6730 the vector allocated based on this size. However, the ELF symbol table
6731 always has a dummy entry as symbol #0, so it ends up even. */
6733 long
6734 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6736 long symcount;
6737 long symtab_size;
6738 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6740 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6741 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6742 if (symcount > 0)
6743 symtab_size -= sizeof (asymbol *);
6745 return symtab_size;
6748 long
6749 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6751 long symcount;
6752 long symtab_size;
6753 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6755 if (elf_dynsymtab (abfd) == 0)
6757 bfd_set_error (bfd_error_invalid_operation);
6758 return -1;
6761 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6762 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6763 if (symcount > 0)
6764 symtab_size -= sizeof (asymbol *);
6766 return symtab_size;
6769 long
6770 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6771 sec_ptr asect)
6773 return (asect->reloc_count + 1) * sizeof (arelent *);
6776 /* Canonicalize the relocs. */
6778 long
6779 _bfd_elf_canonicalize_reloc (bfd *abfd,
6780 sec_ptr section,
6781 arelent **relptr,
6782 asymbol **symbols)
6784 arelent *tblptr;
6785 unsigned int i;
6786 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6788 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6789 return -1;
6791 tblptr = section->relocation;
6792 for (i = 0; i < section->reloc_count; i++)
6793 *relptr++ = tblptr++;
6795 *relptr = NULL;
6797 return section->reloc_count;
6800 long
6801 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6803 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6804 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6806 if (symcount >= 0)
6807 bfd_get_symcount (abfd) = symcount;
6808 return symcount;
6811 long
6812 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6813 asymbol **allocation)
6815 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6816 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6818 if (symcount >= 0)
6819 bfd_get_dynamic_symcount (abfd) = symcount;
6820 return symcount;
6823 /* Return the size required for the dynamic reloc entries. Any loadable
6824 section that was actually installed in the BFD, and has type SHT_REL
6825 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6826 dynamic reloc section. */
6828 long
6829 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6831 long ret;
6832 asection *s;
6834 if (elf_dynsymtab (abfd) == 0)
6836 bfd_set_error (bfd_error_invalid_operation);
6837 return -1;
6840 ret = sizeof (arelent *);
6841 for (s = abfd->sections; s != NULL; s = s->next)
6842 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6843 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6844 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6845 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6846 * sizeof (arelent *));
6848 return ret;
6851 /* Canonicalize the dynamic relocation entries. Note that we return the
6852 dynamic relocations as a single block, although they are actually
6853 associated with particular sections; the interface, which was
6854 designed for SunOS style shared libraries, expects that there is only
6855 one set of dynamic relocs. Any loadable section that was actually
6856 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6857 dynamic symbol table, is considered to be a dynamic reloc section. */
6859 long
6860 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6861 arelent **storage,
6862 asymbol **syms)
6864 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6865 asection *s;
6866 long ret;
6868 if (elf_dynsymtab (abfd) == 0)
6870 bfd_set_error (bfd_error_invalid_operation);
6871 return -1;
6874 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6875 ret = 0;
6876 for (s = abfd->sections; s != NULL; s = s->next)
6878 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6879 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6880 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6882 arelent *p;
6883 long count, i;
6885 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6886 return -1;
6887 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6888 p = s->relocation;
6889 for (i = 0; i < count; i++)
6890 *storage++ = p++;
6891 ret += count;
6895 *storage = NULL;
6897 return ret;
6900 /* Read in the version information. */
6902 bfd_boolean
6903 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6905 bfd_byte *contents = NULL;
6906 unsigned int freeidx = 0;
6908 if (elf_dynverref (abfd) != 0)
6910 Elf_Internal_Shdr *hdr;
6911 Elf_External_Verneed *everneed;
6912 Elf_Internal_Verneed *iverneed;
6913 unsigned int i;
6914 bfd_byte *contents_end;
6916 hdr = &elf_tdata (abfd)->dynverref_hdr;
6918 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
6919 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
6920 if (elf_tdata (abfd)->verref == NULL)
6921 goto error_return;
6923 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6925 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6926 if (contents == NULL)
6928 error_return_verref:
6929 elf_tdata (abfd)->verref = NULL;
6930 elf_tdata (abfd)->cverrefs = 0;
6931 goto error_return;
6933 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6934 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6935 goto error_return_verref;
6937 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6938 goto error_return_verref;
6940 BFD_ASSERT (sizeof (Elf_External_Verneed)
6941 == sizeof (Elf_External_Vernaux));
6942 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6943 everneed = (Elf_External_Verneed *) contents;
6944 iverneed = elf_tdata (abfd)->verref;
6945 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6947 Elf_External_Vernaux *evernaux;
6948 Elf_Internal_Vernaux *ivernaux;
6949 unsigned int j;
6951 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6953 iverneed->vn_bfd = abfd;
6955 iverneed->vn_filename =
6956 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6957 iverneed->vn_file);
6958 if (iverneed->vn_filename == NULL)
6959 goto error_return_verref;
6961 if (iverneed->vn_cnt == 0)
6962 iverneed->vn_auxptr = NULL;
6963 else
6965 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
6966 bfd_alloc2 (abfd, iverneed->vn_cnt,
6967 sizeof (Elf_Internal_Vernaux));
6968 if (iverneed->vn_auxptr == NULL)
6969 goto error_return_verref;
6972 if (iverneed->vn_aux
6973 > (size_t) (contents_end - (bfd_byte *) everneed))
6974 goto error_return_verref;
6976 evernaux = ((Elf_External_Vernaux *)
6977 ((bfd_byte *) everneed + iverneed->vn_aux));
6978 ivernaux = iverneed->vn_auxptr;
6979 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6981 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6983 ivernaux->vna_nodename =
6984 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6985 ivernaux->vna_name);
6986 if (ivernaux->vna_nodename == NULL)
6987 goto error_return_verref;
6989 if (j + 1 < iverneed->vn_cnt)
6990 ivernaux->vna_nextptr = ivernaux + 1;
6991 else
6992 ivernaux->vna_nextptr = NULL;
6994 if (ivernaux->vna_next
6995 > (size_t) (contents_end - (bfd_byte *) evernaux))
6996 goto error_return_verref;
6998 evernaux = ((Elf_External_Vernaux *)
6999 ((bfd_byte *) evernaux + ivernaux->vna_next));
7001 if (ivernaux->vna_other > freeidx)
7002 freeidx = ivernaux->vna_other;
7005 if (i + 1 < hdr->sh_info)
7006 iverneed->vn_nextref = iverneed + 1;
7007 else
7008 iverneed->vn_nextref = NULL;
7010 if (iverneed->vn_next
7011 > (size_t) (contents_end - (bfd_byte *) everneed))
7012 goto error_return_verref;
7014 everneed = ((Elf_External_Verneed *)
7015 ((bfd_byte *) everneed + iverneed->vn_next));
7018 free (contents);
7019 contents = NULL;
7022 if (elf_dynverdef (abfd) != 0)
7024 Elf_Internal_Shdr *hdr;
7025 Elf_External_Verdef *everdef;
7026 Elf_Internal_Verdef *iverdef;
7027 Elf_Internal_Verdef *iverdefarr;
7028 Elf_Internal_Verdef iverdefmem;
7029 unsigned int i;
7030 unsigned int maxidx;
7031 bfd_byte *contents_end_def, *contents_end_aux;
7033 hdr = &elf_tdata (abfd)->dynverdef_hdr;
7035 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7036 if (contents == NULL)
7037 goto error_return;
7038 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7039 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7040 goto error_return;
7042 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
7043 goto error_return;
7045 BFD_ASSERT (sizeof (Elf_External_Verdef)
7046 >= sizeof (Elf_External_Verdaux));
7047 contents_end_def = contents + hdr->sh_size
7048 - sizeof (Elf_External_Verdef);
7049 contents_end_aux = contents + hdr->sh_size
7050 - sizeof (Elf_External_Verdaux);
7052 /* We know the number of entries in the section but not the maximum
7053 index. Therefore we have to run through all entries and find
7054 the maximum. */
7055 everdef = (Elf_External_Verdef *) contents;
7056 maxidx = 0;
7057 for (i = 0; i < hdr->sh_info; ++i)
7059 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7061 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7062 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
7064 if (iverdefmem.vd_next
7065 > (size_t) (contents_end_def - (bfd_byte *) everdef))
7066 goto error_return;
7068 everdef = ((Elf_External_Verdef *)
7069 ((bfd_byte *) everdef + iverdefmem.vd_next));
7072 if (default_imported_symver)
7074 if (freeidx > maxidx)
7075 maxidx = ++freeidx;
7076 else
7077 freeidx = ++maxidx;
7079 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7080 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7081 if (elf_tdata (abfd)->verdef == NULL)
7082 goto error_return;
7084 elf_tdata (abfd)->cverdefs = maxidx;
7086 everdef = (Elf_External_Verdef *) contents;
7087 iverdefarr = elf_tdata (abfd)->verdef;
7088 for (i = 0; i < hdr->sh_info; i++)
7090 Elf_External_Verdaux *everdaux;
7091 Elf_Internal_Verdaux *iverdaux;
7092 unsigned int j;
7094 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7096 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7098 error_return_verdef:
7099 elf_tdata (abfd)->verdef = NULL;
7100 elf_tdata (abfd)->cverdefs = 0;
7101 goto error_return;
7104 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7105 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
7107 iverdef->vd_bfd = abfd;
7109 if (iverdef->vd_cnt == 0)
7110 iverdef->vd_auxptr = NULL;
7111 else
7113 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7114 bfd_alloc2 (abfd, iverdef->vd_cnt,
7115 sizeof (Elf_Internal_Verdaux));
7116 if (iverdef->vd_auxptr == NULL)
7117 goto error_return_verdef;
7120 if (iverdef->vd_aux
7121 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7122 goto error_return_verdef;
7124 everdaux = ((Elf_External_Verdaux *)
7125 ((bfd_byte *) everdef + iverdef->vd_aux));
7126 iverdaux = iverdef->vd_auxptr;
7127 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7129 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7131 iverdaux->vda_nodename =
7132 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7133 iverdaux->vda_name);
7134 if (iverdaux->vda_nodename == NULL)
7135 goto error_return_verdef;
7137 if (j + 1 < iverdef->vd_cnt)
7138 iverdaux->vda_nextptr = iverdaux + 1;
7139 else
7140 iverdaux->vda_nextptr = NULL;
7142 if (iverdaux->vda_next
7143 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7144 goto error_return_verdef;
7146 everdaux = ((Elf_External_Verdaux *)
7147 ((bfd_byte *) everdaux + iverdaux->vda_next));
7150 if (iverdef->vd_cnt)
7151 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
7153 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7154 iverdef->vd_nextdef = iverdef + 1;
7155 else
7156 iverdef->vd_nextdef = NULL;
7158 everdef = ((Elf_External_Verdef *)
7159 ((bfd_byte *) everdef + iverdef->vd_next));
7162 free (contents);
7163 contents = NULL;
7165 else if (default_imported_symver)
7167 if (freeidx < 3)
7168 freeidx = 3;
7169 else
7170 freeidx++;
7172 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7173 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7174 if (elf_tdata (abfd)->verdef == NULL)
7175 goto error_return;
7177 elf_tdata (abfd)->cverdefs = freeidx;
7180 /* Create a default version based on the soname. */
7181 if (default_imported_symver)
7183 Elf_Internal_Verdef *iverdef;
7184 Elf_Internal_Verdaux *iverdaux;
7186 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
7188 iverdef->vd_version = VER_DEF_CURRENT;
7189 iverdef->vd_flags = 0;
7190 iverdef->vd_ndx = freeidx;
7191 iverdef->vd_cnt = 1;
7193 iverdef->vd_bfd = abfd;
7195 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7196 if (iverdef->vd_nodename == NULL)
7197 goto error_return_verdef;
7198 iverdef->vd_nextdef = NULL;
7199 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7200 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
7201 if (iverdef->vd_auxptr == NULL)
7202 goto error_return_verdef;
7204 iverdaux = iverdef->vd_auxptr;
7205 iverdaux->vda_nodename = iverdef->vd_nodename;
7206 iverdaux->vda_nextptr = NULL;
7209 return TRUE;
7211 error_return:
7212 if (contents != NULL)
7213 free (contents);
7214 return FALSE;
7217 asymbol *
7218 _bfd_elf_make_empty_symbol (bfd *abfd)
7220 elf_symbol_type *newsym;
7221 bfd_size_type amt = sizeof (elf_symbol_type);
7223 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
7224 if (!newsym)
7225 return NULL;
7226 else
7228 newsym->symbol.the_bfd = abfd;
7229 return &newsym->symbol;
7233 void
7234 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7235 asymbol *symbol,
7236 symbol_info *ret)
7238 bfd_symbol_info (symbol, ret);
7241 /* Return whether a symbol name implies a local symbol. Most targets
7242 use this function for the is_local_label_name entry point, but some
7243 override it. */
7245 bfd_boolean
7246 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7247 const char *name)
7249 /* Normal local symbols start with ``.L''. */
7250 if (name[0] == '.' && name[1] == 'L')
7251 return TRUE;
7253 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7254 DWARF debugging symbols starting with ``..''. */
7255 if (name[0] == '.' && name[1] == '.')
7256 return TRUE;
7258 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7259 emitting DWARF debugging output. I suspect this is actually a
7260 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7261 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7262 underscore to be emitted on some ELF targets). For ease of use,
7263 we treat such symbols as local. */
7264 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7265 return TRUE;
7267 return FALSE;
7270 alent *
7271 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7272 asymbol *symbol ATTRIBUTE_UNUSED)
7274 abort ();
7275 return NULL;
7278 bfd_boolean
7279 _bfd_elf_set_arch_mach (bfd *abfd,
7280 enum bfd_architecture arch,
7281 unsigned long machine)
7283 /* If this isn't the right architecture for this backend, and this
7284 isn't the generic backend, fail. */
7285 if (arch != get_elf_backend_data (abfd)->arch
7286 && arch != bfd_arch_unknown
7287 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7288 return FALSE;
7290 return bfd_default_set_arch_mach (abfd, arch, machine);
7293 /* Find the function to a particular section and offset,
7294 for error reporting. */
7296 static bfd_boolean
7297 elf_find_function (bfd *abfd,
7298 asection *section,
7299 asymbol **symbols,
7300 bfd_vma offset,
7301 const char **filename_ptr,
7302 const char **functionname_ptr)
7304 const char *filename;
7305 asymbol *func, *file;
7306 bfd_vma low_func;
7307 asymbol **p;
7308 /* ??? Given multiple file symbols, it is impossible to reliably
7309 choose the right file name for global symbols. File symbols are
7310 local symbols, and thus all file symbols must sort before any
7311 global symbols. The ELF spec may be interpreted to say that a
7312 file symbol must sort before other local symbols, but currently
7313 ld -r doesn't do this. So, for ld -r output, it is possible to
7314 make a better choice of file name for local symbols by ignoring
7315 file symbols appearing after a given local symbol. */
7316 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7317 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7319 filename = NULL;
7320 func = NULL;
7321 file = NULL;
7322 low_func = 0;
7323 state = nothing_seen;
7325 for (p = symbols; *p != NULL; p++)
7327 elf_symbol_type *q;
7328 unsigned int type;
7330 q = (elf_symbol_type *) *p;
7332 type = ELF_ST_TYPE (q->internal_elf_sym.st_info);
7333 switch (type)
7335 case STT_FILE:
7336 file = &q->symbol;
7337 if (state == symbol_seen)
7338 state = file_after_symbol_seen;
7339 continue;
7340 default:
7341 if (!bed->is_function_type (type))
7342 break;
7343 case STT_NOTYPE:
7344 if (bfd_get_section (&q->symbol) == section
7345 && q->symbol.value >= low_func
7346 && q->symbol.value <= offset)
7348 func = (asymbol *) q;
7349 low_func = q->symbol.value;
7350 filename = NULL;
7351 if (file != NULL
7352 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7353 || state != file_after_symbol_seen))
7354 filename = bfd_asymbol_name (file);
7356 break;
7358 if (state == nothing_seen)
7359 state = symbol_seen;
7362 if (func == NULL)
7363 return FALSE;
7365 if (filename_ptr)
7366 *filename_ptr = filename;
7367 if (functionname_ptr)
7368 *functionname_ptr = bfd_asymbol_name (func);
7370 return TRUE;
7373 /* Find the nearest line to a particular section and offset,
7374 for error reporting. */
7376 bfd_boolean
7377 _bfd_elf_find_nearest_line (bfd *abfd,
7378 asection *section,
7379 asymbol **symbols,
7380 bfd_vma offset,
7381 const char **filename_ptr,
7382 const char **functionname_ptr,
7383 unsigned int *line_ptr)
7385 bfd_boolean found;
7387 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7388 filename_ptr, functionname_ptr,
7389 line_ptr))
7391 if (!*functionname_ptr)
7392 elf_find_function (abfd, section, symbols, offset,
7393 *filename_ptr ? NULL : filename_ptr,
7394 functionname_ptr);
7396 return TRUE;
7399 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7400 filename_ptr, functionname_ptr,
7401 line_ptr, 0,
7402 &elf_tdata (abfd)->dwarf2_find_line_info))
7404 if (!*functionname_ptr)
7405 elf_find_function (abfd, section, symbols, offset,
7406 *filename_ptr ? NULL : filename_ptr,
7407 functionname_ptr);
7409 return TRUE;
7412 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7413 &found, filename_ptr,
7414 functionname_ptr, line_ptr,
7415 &elf_tdata (abfd)->line_info))
7416 return FALSE;
7417 if (found && (*functionname_ptr || *line_ptr))
7418 return TRUE;
7420 if (symbols == NULL)
7421 return FALSE;
7423 if (! elf_find_function (abfd, section, symbols, offset,
7424 filename_ptr, functionname_ptr))
7425 return FALSE;
7427 *line_ptr = 0;
7428 return TRUE;
7431 /* Find the line for a symbol. */
7433 bfd_boolean
7434 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7435 const char **filename_ptr, unsigned int *line_ptr)
7437 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7438 filename_ptr, line_ptr, 0,
7439 &elf_tdata (abfd)->dwarf2_find_line_info);
7442 /* After a call to bfd_find_nearest_line, successive calls to
7443 bfd_find_inliner_info can be used to get source information about
7444 each level of function inlining that terminated at the address
7445 passed to bfd_find_nearest_line. Currently this is only supported
7446 for DWARF2 with appropriate DWARF3 extensions. */
7448 bfd_boolean
7449 _bfd_elf_find_inliner_info (bfd *abfd,
7450 const char **filename_ptr,
7451 const char **functionname_ptr,
7452 unsigned int *line_ptr)
7454 bfd_boolean found;
7455 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7456 functionname_ptr, line_ptr,
7457 & elf_tdata (abfd)->dwarf2_find_line_info);
7458 return found;
7462 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7464 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7465 int ret = bed->s->sizeof_ehdr;
7467 if (!info->relocatable)
7469 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7471 if (phdr_size == (bfd_size_type) -1)
7473 struct elf_segment_map *m;
7475 phdr_size = 0;
7476 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7477 phdr_size += bed->s->sizeof_phdr;
7479 if (phdr_size == 0)
7480 phdr_size = get_program_header_size (abfd, info);
7483 elf_tdata (abfd)->program_header_size = phdr_size;
7484 ret += phdr_size;
7487 return ret;
7490 bfd_boolean
7491 _bfd_elf_set_section_contents (bfd *abfd,
7492 sec_ptr section,
7493 const void *location,
7494 file_ptr offset,
7495 bfd_size_type count)
7497 Elf_Internal_Shdr *hdr;
7498 bfd_signed_vma pos;
7500 if (! abfd->output_has_begun
7501 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7502 return FALSE;
7504 hdr = &elf_section_data (section)->this_hdr;
7505 pos = hdr->sh_offset + offset;
7506 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7507 || bfd_bwrite (location, count, abfd) != count)
7508 return FALSE;
7510 return TRUE;
7513 void
7514 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7515 arelent *cache_ptr ATTRIBUTE_UNUSED,
7516 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7518 abort ();
7521 /* Try to convert a non-ELF reloc into an ELF one. */
7523 bfd_boolean
7524 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7526 /* Check whether we really have an ELF howto. */
7528 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7530 bfd_reloc_code_real_type code;
7531 reloc_howto_type *howto;
7533 /* Alien reloc: Try to determine its type to replace it with an
7534 equivalent ELF reloc. */
7536 if (areloc->howto->pc_relative)
7538 switch (areloc->howto->bitsize)
7540 case 8:
7541 code = BFD_RELOC_8_PCREL;
7542 break;
7543 case 12:
7544 code = BFD_RELOC_12_PCREL;
7545 break;
7546 case 16:
7547 code = BFD_RELOC_16_PCREL;
7548 break;
7549 case 24:
7550 code = BFD_RELOC_24_PCREL;
7551 break;
7552 case 32:
7553 code = BFD_RELOC_32_PCREL;
7554 break;
7555 case 64:
7556 code = BFD_RELOC_64_PCREL;
7557 break;
7558 default:
7559 goto fail;
7562 howto = bfd_reloc_type_lookup (abfd, code);
7564 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7566 if (howto->pcrel_offset)
7567 areloc->addend += areloc->address;
7568 else
7569 areloc->addend -= areloc->address; /* addend is unsigned!! */
7572 else
7574 switch (areloc->howto->bitsize)
7576 case 8:
7577 code = BFD_RELOC_8;
7578 break;
7579 case 14:
7580 code = BFD_RELOC_14;
7581 break;
7582 case 16:
7583 code = BFD_RELOC_16;
7584 break;
7585 case 26:
7586 code = BFD_RELOC_26;
7587 break;
7588 case 32:
7589 code = BFD_RELOC_32;
7590 break;
7591 case 64:
7592 code = BFD_RELOC_64;
7593 break;
7594 default:
7595 goto fail;
7598 howto = bfd_reloc_type_lookup (abfd, code);
7601 if (howto)
7602 areloc->howto = howto;
7603 else
7604 goto fail;
7607 return TRUE;
7609 fail:
7610 (*_bfd_error_handler)
7611 (_("%B: unsupported relocation type %s"),
7612 abfd, areloc->howto->name);
7613 bfd_set_error (bfd_error_bad_value);
7614 return FALSE;
7617 bfd_boolean
7618 _bfd_elf_close_and_cleanup (bfd *abfd)
7620 if (bfd_get_format (abfd) == bfd_object)
7622 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7623 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7624 _bfd_dwarf2_cleanup_debug_info (abfd);
7627 return _bfd_generic_close_and_cleanup (abfd);
7630 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7631 in the relocation's offset. Thus we cannot allow any sort of sanity
7632 range-checking to interfere. There is nothing else to do in processing
7633 this reloc. */
7635 bfd_reloc_status_type
7636 _bfd_elf_rel_vtable_reloc_fn
7637 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7638 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7639 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7640 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7642 return bfd_reloc_ok;
7645 /* Elf core file support. Much of this only works on native
7646 toolchains, since we rely on knowing the
7647 machine-dependent procfs structure in order to pick
7648 out details about the corefile. */
7650 #ifdef HAVE_SYS_PROCFS_H
7651 /* Needed for new procfs interface on sparc-solaris. */
7652 # define _STRUCTURED_PROC 1
7653 # include <sys/procfs.h>
7654 #endif
7656 /* Return a PID that identifies a "thread" for threaded cores, or the
7657 PID of the main process for non-threaded cores. */
7659 static int
7660 elfcore_make_pid (bfd *abfd)
7662 int pid;
7664 pid = elf_tdata (abfd)->core_lwpid;
7665 if (pid == 0)
7666 pid = elf_tdata (abfd)->core_pid;
7668 return pid;
7671 /* If there isn't a section called NAME, make one, using
7672 data from SECT. Note, this function will generate a
7673 reference to NAME, so you shouldn't deallocate or
7674 overwrite it. */
7676 static bfd_boolean
7677 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7679 asection *sect2;
7681 if (bfd_get_section_by_name (abfd, name) != NULL)
7682 return TRUE;
7684 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7685 if (sect2 == NULL)
7686 return FALSE;
7688 sect2->size = sect->size;
7689 sect2->filepos = sect->filepos;
7690 sect2->alignment_power = sect->alignment_power;
7691 return TRUE;
7694 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7695 actually creates up to two pseudosections:
7696 - For the single-threaded case, a section named NAME, unless
7697 such a section already exists.
7698 - For the multi-threaded case, a section named "NAME/PID", where
7699 PID is elfcore_make_pid (abfd).
7700 Both pseudosections have identical contents. */
7701 bfd_boolean
7702 _bfd_elfcore_make_pseudosection (bfd *abfd,
7703 char *name,
7704 size_t size,
7705 ufile_ptr filepos)
7707 char buf[100];
7708 char *threaded_name;
7709 size_t len;
7710 asection *sect;
7712 /* Build the section name. */
7714 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7715 len = strlen (buf) + 1;
7716 threaded_name = (char *) bfd_alloc (abfd, len);
7717 if (threaded_name == NULL)
7718 return FALSE;
7719 memcpy (threaded_name, buf, len);
7721 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7722 SEC_HAS_CONTENTS);
7723 if (sect == NULL)
7724 return FALSE;
7725 sect->size = size;
7726 sect->filepos = filepos;
7727 sect->alignment_power = 2;
7729 return elfcore_maybe_make_sect (abfd, name, sect);
7732 /* prstatus_t exists on:
7733 solaris 2.5+
7734 linux 2.[01] + glibc
7735 unixware 4.2
7738 #if defined (HAVE_PRSTATUS_T)
7740 static bfd_boolean
7741 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7743 size_t size;
7744 int offset;
7746 if (note->descsz == sizeof (prstatus_t))
7748 prstatus_t prstat;
7750 size = sizeof (prstat.pr_reg);
7751 offset = offsetof (prstatus_t, pr_reg);
7752 memcpy (&prstat, note->descdata, sizeof (prstat));
7754 /* Do not overwrite the core signal if it
7755 has already been set by another thread. */
7756 if (elf_tdata (abfd)->core_signal == 0)
7757 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7758 if (elf_tdata (abfd)->core_pid == 0)
7759 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7761 /* pr_who exists on:
7762 solaris 2.5+
7763 unixware 4.2
7764 pr_who doesn't exist on:
7765 linux 2.[01]
7767 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7768 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7769 #else
7770 elf_tdata (abfd)->core_lwpid = prstat.pr_pid;
7771 #endif
7773 #if defined (HAVE_PRSTATUS32_T)
7774 else if (note->descsz == sizeof (prstatus32_t))
7776 /* 64-bit host, 32-bit corefile */
7777 prstatus32_t prstat;
7779 size = sizeof (prstat.pr_reg);
7780 offset = offsetof (prstatus32_t, pr_reg);
7781 memcpy (&prstat, note->descdata, sizeof (prstat));
7783 /* Do not overwrite the core signal if it
7784 has already been set by another thread. */
7785 if (elf_tdata (abfd)->core_signal == 0)
7786 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7787 if (elf_tdata (abfd)->core_pid == 0)
7788 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7790 /* pr_who exists on:
7791 solaris 2.5+
7792 unixware 4.2
7793 pr_who doesn't exist on:
7794 linux 2.[01]
7796 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7797 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7798 #else
7799 elf_tdata (abfd)->core_lwpid = prstat.pr_pid;
7800 #endif
7802 #endif /* HAVE_PRSTATUS32_T */
7803 else
7805 /* Fail - we don't know how to handle any other
7806 note size (ie. data object type). */
7807 return TRUE;
7810 /* Make a ".reg/999" section and a ".reg" section. */
7811 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7812 size, note->descpos + offset);
7814 #endif /* defined (HAVE_PRSTATUS_T) */
7816 /* Create a pseudosection containing the exact contents of NOTE. */
7817 static bfd_boolean
7818 elfcore_make_note_pseudosection (bfd *abfd,
7819 char *name,
7820 Elf_Internal_Note *note)
7822 return _bfd_elfcore_make_pseudosection (abfd, name,
7823 note->descsz, note->descpos);
7826 /* There isn't a consistent prfpregset_t across platforms,
7827 but it doesn't matter, because we don't have to pick this
7828 data structure apart. */
7830 static bfd_boolean
7831 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7833 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7836 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7837 type of NT_PRXFPREG. Just include the whole note's contents
7838 literally. */
7840 static bfd_boolean
7841 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7843 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7846 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
7847 with a note type of NT_X86_XSTATE. Just include the whole note's
7848 contents literally. */
7850 static bfd_boolean
7851 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
7853 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
7856 static bfd_boolean
7857 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7859 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7862 static bfd_boolean
7863 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7865 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7868 static bfd_boolean
7869 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
7871 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
7874 static bfd_boolean
7875 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
7877 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
7880 static bfd_boolean
7881 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
7883 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
7886 static bfd_boolean
7887 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
7889 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
7892 static bfd_boolean
7893 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
7895 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
7898 static bfd_boolean
7899 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
7901 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
7904 #if defined (HAVE_PRPSINFO_T)
7905 typedef prpsinfo_t elfcore_psinfo_t;
7906 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7907 typedef prpsinfo32_t elfcore_psinfo32_t;
7908 #endif
7909 #endif
7911 #if defined (HAVE_PSINFO_T)
7912 typedef psinfo_t elfcore_psinfo_t;
7913 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7914 typedef psinfo32_t elfcore_psinfo32_t;
7915 #endif
7916 #endif
7918 /* return a malloc'ed copy of a string at START which is at
7919 most MAX bytes long, possibly without a terminating '\0'.
7920 the copy will always have a terminating '\0'. */
7922 char *
7923 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7925 char *dups;
7926 char *end = (char *) memchr (start, '\0', max);
7927 size_t len;
7929 if (end == NULL)
7930 len = max;
7931 else
7932 len = end - start;
7934 dups = (char *) bfd_alloc (abfd, len + 1);
7935 if (dups == NULL)
7936 return NULL;
7938 memcpy (dups, start, len);
7939 dups[len] = '\0';
7941 return dups;
7944 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7945 static bfd_boolean
7946 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7948 if (note->descsz == sizeof (elfcore_psinfo_t))
7950 elfcore_psinfo_t psinfo;
7952 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7954 elf_tdata (abfd)->core_program
7955 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7956 sizeof (psinfo.pr_fname));
7958 elf_tdata (abfd)->core_command
7959 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7960 sizeof (psinfo.pr_psargs));
7962 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7963 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7965 /* 64-bit host, 32-bit corefile */
7966 elfcore_psinfo32_t psinfo;
7968 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7970 elf_tdata (abfd)->core_program
7971 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7972 sizeof (psinfo.pr_fname));
7974 elf_tdata (abfd)->core_command
7975 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7976 sizeof (psinfo.pr_psargs));
7978 #endif
7980 else
7982 /* Fail - we don't know how to handle any other
7983 note size (ie. data object type). */
7984 return TRUE;
7987 /* Note that for some reason, a spurious space is tacked
7988 onto the end of the args in some (at least one anyway)
7989 implementations, so strip it off if it exists. */
7992 char *command = elf_tdata (abfd)->core_command;
7993 int n = strlen (command);
7995 if (0 < n && command[n - 1] == ' ')
7996 command[n - 1] = '\0';
7999 return TRUE;
8001 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8003 #if defined (HAVE_PSTATUS_T)
8004 static bfd_boolean
8005 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8007 if (note->descsz == sizeof (pstatus_t)
8008 #if defined (HAVE_PXSTATUS_T)
8009 || note->descsz == sizeof (pxstatus_t)
8010 #endif
8013 pstatus_t pstat;
8015 memcpy (&pstat, note->descdata, sizeof (pstat));
8017 elf_tdata (abfd)->core_pid = pstat.pr_pid;
8019 #if defined (HAVE_PSTATUS32_T)
8020 else if (note->descsz == sizeof (pstatus32_t))
8022 /* 64-bit host, 32-bit corefile */
8023 pstatus32_t pstat;
8025 memcpy (&pstat, note->descdata, sizeof (pstat));
8027 elf_tdata (abfd)->core_pid = pstat.pr_pid;
8029 #endif
8030 /* Could grab some more details from the "representative"
8031 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8032 NT_LWPSTATUS note, presumably. */
8034 return TRUE;
8036 #endif /* defined (HAVE_PSTATUS_T) */
8038 #if defined (HAVE_LWPSTATUS_T)
8039 static bfd_boolean
8040 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8042 lwpstatus_t lwpstat;
8043 char buf[100];
8044 char *name;
8045 size_t len;
8046 asection *sect;
8048 if (note->descsz != sizeof (lwpstat)
8049 #if defined (HAVE_LWPXSTATUS_T)
8050 && note->descsz != sizeof (lwpxstatus_t)
8051 #endif
8053 return TRUE;
8055 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8057 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
8058 /* Do not overwrite the core signal if it has already been set by
8059 another thread. */
8060 if (elf_tdata (abfd)->core_signal == 0)
8061 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
8063 /* Make a ".reg/999" section. */
8065 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8066 len = strlen (buf) + 1;
8067 name = bfd_alloc (abfd, len);
8068 if (name == NULL)
8069 return FALSE;
8070 memcpy (name, buf, len);
8072 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8073 if (sect == NULL)
8074 return FALSE;
8076 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8077 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8078 sect->filepos = note->descpos
8079 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8080 #endif
8082 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8083 sect->size = sizeof (lwpstat.pr_reg);
8084 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8085 #endif
8087 sect->alignment_power = 2;
8089 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8090 return FALSE;
8092 /* Make a ".reg2/999" section */
8094 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8095 len = strlen (buf) + 1;
8096 name = bfd_alloc (abfd, len);
8097 if (name == NULL)
8098 return FALSE;
8099 memcpy (name, buf, len);
8101 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8102 if (sect == NULL)
8103 return FALSE;
8105 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8106 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8107 sect->filepos = note->descpos
8108 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8109 #endif
8111 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8112 sect->size = sizeof (lwpstat.pr_fpreg);
8113 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8114 #endif
8116 sect->alignment_power = 2;
8118 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8120 #endif /* defined (HAVE_LWPSTATUS_T) */
8122 static bfd_boolean
8123 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8125 char buf[30];
8126 char *name;
8127 size_t len;
8128 asection *sect;
8129 int type;
8130 int is_active_thread;
8131 bfd_vma base_addr;
8133 if (note->descsz < 728)
8134 return TRUE;
8136 if (! CONST_STRNEQ (note->namedata, "win32"))
8137 return TRUE;
8139 type = bfd_get_32 (abfd, note->descdata);
8141 switch (type)
8143 case 1 /* NOTE_INFO_PROCESS */:
8144 /* FIXME: need to add ->core_command. */
8145 /* process_info.pid */
8146 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
8147 /* process_info.signal */
8148 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
8149 break;
8151 case 2 /* NOTE_INFO_THREAD */:
8152 /* Make a ".reg/999" section. */
8153 /* thread_info.tid */
8154 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8156 len = strlen (buf) + 1;
8157 name = (char *) bfd_alloc (abfd, len);
8158 if (name == NULL)
8159 return FALSE;
8161 memcpy (name, buf, len);
8163 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8164 if (sect == NULL)
8165 return FALSE;
8167 /* sizeof (thread_info.thread_context) */
8168 sect->size = 716;
8169 /* offsetof (thread_info.thread_context) */
8170 sect->filepos = note->descpos + 12;
8171 sect->alignment_power = 2;
8173 /* thread_info.is_active_thread */
8174 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8176 if (is_active_thread)
8177 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8178 return FALSE;
8179 break;
8181 case 3 /* NOTE_INFO_MODULE */:
8182 /* Make a ".module/xxxxxxxx" section. */
8183 /* module_info.base_address */
8184 base_addr = bfd_get_32 (abfd, note->descdata + 4);
8185 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8187 len = strlen (buf) + 1;
8188 name = (char *) bfd_alloc (abfd, len);
8189 if (name == NULL)
8190 return FALSE;
8192 memcpy (name, buf, len);
8194 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8196 if (sect == NULL)
8197 return FALSE;
8199 sect->size = note->descsz;
8200 sect->filepos = note->descpos;
8201 sect->alignment_power = 2;
8202 break;
8204 default:
8205 return TRUE;
8208 return TRUE;
8211 static bfd_boolean
8212 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8214 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8216 switch (note->type)
8218 default:
8219 return TRUE;
8221 case NT_PRSTATUS:
8222 if (bed->elf_backend_grok_prstatus)
8223 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8224 return TRUE;
8225 #if defined (HAVE_PRSTATUS_T)
8226 return elfcore_grok_prstatus (abfd, note);
8227 #else
8228 return TRUE;
8229 #endif
8231 #if defined (HAVE_PSTATUS_T)
8232 case NT_PSTATUS:
8233 return elfcore_grok_pstatus (abfd, note);
8234 #endif
8236 #if defined (HAVE_LWPSTATUS_T)
8237 case NT_LWPSTATUS:
8238 return elfcore_grok_lwpstatus (abfd, note);
8239 #endif
8241 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
8242 return elfcore_grok_prfpreg (abfd, note);
8244 case NT_WIN32PSTATUS:
8245 return elfcore_grok_win32pstatus (abfd, note);
8247 case NT_PRXFPREG: /* Linux SSE extension */
8248 if (note->namesz == 6
8249 && strcmp (note->namedata, "LINUX") == 0)
8250 return elfcore_grok_prxfpreg (abfd, note);
8251 else
8252 return TRUE;
8254 case NT_X86_XSTATE: /* Linux XSAVE extension */
8255 if (note->namesz == 6
8256 && strcmp (note->namedata, "LINUX") == 0)
8257 return elfcore_grok_xstatereg (abfd, note);
8258 else
8259 return TRUE;
8261 case NT_PPC_VMX:
8262 if (note->namesz == 6
8263 && strcmp (note->namedata, "LINUX") == 0)
8264 return elfcore_grok_ppc_vmx (abfd, note);
8265 else
8266 return TRUE;
8268 case NT_PPC_VSX:
8269 if (note->namesz == 6
8270 && strcmp (note->namedata, "LINUX") == 0)
8271 return elfcore_grok_ppc_vsx (abfd, note);
8272 else
8273 return TRUE;
8275 case NT_S390_HIGH_GPRS:
8276 if (note->namesz == 6
8277 && strcmp (note->namedata, "LINUX") == 0)
8278 return elfcore_grok_s390_high_gprs (abfd, note);
8279 else
8280 return TRUE;
8282 case NT_S390_TIMER:
8283 if (note->namesz == 6
8284 && strcmp (note->namedata, "LINUX") == 0)
8285 return elfcore_grok_s390_timer (abfd, note);
8286 else
8287 return TRUE;
8289 case NT_S390_TODCMP:
8290 if (note->namesz == 6
8291 && strcmp (note->namedata, "LINUX") == 0)
8292 return elfcore_grok_s390_todcmp (abfd, note);
8293 else
8294 return TRUE;
8296 case NT_S390_TODPREG:
8297 if (note->namesz == 6
8298 && strcmp (note->namedata, "LINUX") == 0)
8299 return elfcore_grok_s390_todpreg (abfd, note);
8300 else
8301 return TRUE;
8303 case NT_S390_CTRS:
8304 if (note->namesz == 6
8305 && strcmp (note->namedata, "LINUX") == 0)
8306 return elfcore_grok_s390_ctrs (abfd, note);
8307 else
8308 return TRUE;
8310 case NT_S390_PREFIX:
8311 if (note->namesz == 6
8312 && strcmp (note->namedata, "LINUX") == 0)
8313 return elfcore_grok_s390_prefix (abfd, note);
8314 else
8315 return TRUE;
8317 case NT_PRPSINFO:
8318 case NT_PSINFO:
8319 if (bed->elf_backend_grok_psinfo)
8320 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8321 return TRUE;
8322 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8323 return elfcore_grok_psinfo (abfd, note);
8324 #else
8325 return TRUE;
8326 #endif
8328 case NT_AUXV:
8330 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8331 SEC_HAS_CONTENTS);
8333 if (sect == NULL)
8334 return FALSE;
8335 sect->size = note->descsz;
8336 sect->filepos = note->descpos;
8337 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8339 return TRUE;
8344 static bfd_boolean
8345 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8347 elf_tdata (abfd)->build_id_size = note->descsz;
8348 elf_tdata (abfd)->build_id = (bfd_byte *) bfd_alloc (abfd, note->descsz);
8349 if (elf_tdata (abfd)->build_id == NULL)
8350 return FALSE;
8352 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
8354 return TRUE;
8357 static bfd_boolean
8358 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8360 switch (note->type)
8362 default:
8363 return TRUE;
8365 case NT_GNU_BUILD_ID:
8366 return elfobj_grok_gnu_build_id (abfd, note);
8370 static bfd_boolean
8371 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8373 char *cp;
8375 cp = strchr (note->namedata, '@');
8376 if (cp != NULL)
8378 *lwpidp = atoi(cp + 1);
8379 return TRUE;
8381 return FALSE;
8384 static bfd_boolean
8385 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8387 /* Signal number at offset 0x08. */
8388 elf_tdata (abfd)->core_signal
8389 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8391 /* Process ID at offset 0x50. */
8392 elf_tdata (abfd)->core_pid
8393 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8395 /* Command name at 0x7c (max 32 bytes, including nul). */
8396 elf_tdata (abfd)->core_command
8397 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8399 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8400 note);
8403 static bfd_boolean
8404 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8406 int lwp;
8408 if (elfcore_netbsd_get_lwpid (note, &lwp))
8409 elf_tdata (abfd)->core_lwpid = lwp;
8411 if (note->type == NT_NETBSDCORE_PROCINFO)
8413 /* NetBSD-specific core "procinfo". Note that we expect to
8414 find this note before any of the others, which is fine,
8415 since the kernel writes this note out first when it
8416 creates a core file. */
8418 return elfcore_grok_netbsd_procinfo (abfd, note);
8421 /* As of Jan 2002 there are no other machine-independent notes
8422 defined for NetBSD core files. If the note type is less
8423 than the start of the machine-dependent note types, we don't
8424 understand it. */
8426 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8427 return TRUE;
8430 switch (bfd_get_arch (abfd))
8432 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8433 PT_GETFPREGS == mach+2. */
8435 case bfd_arch_alpha:
8436 case bfd_arch_sparc:
8437 switch (note->type)
8439 case NT_NETBSDCORE_FIRSTMACH+0:
8440 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8442 case NT_NETBSDCORE_FIRSTMACH+2:
8443 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8445 default:
8446 return TRUE;
8449 /* On all other arch's, PT_GETREGS == mach+1 and
8450 PT_GETFPREGS == mach+3. */
8452 default:
8453 switch (note->type)
8455 case NT_NETBSDCORE_FIRSTMACH+1:
8456 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8458 case NT_NETBSDCORE_FIRSTMACH+3:
8459 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8461 default:
8462 return TRUE;
8465 /* NOTREACHED */
8468 static bfd_boolean
8469 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8471 /* Signal number at offset 0x08. */
8472 elf_tdata (abfd)->core_signal
8473 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8475 /* Process ID at offset 0x20. */
8476 elf_tdata (abfd)->core_pid
8477 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8479 /* Command name at 0x48 (max 32 bytes, including nul). */
8480 elf_tdata (abfd)->core_command
8481 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8483 return TRUE;
8486 static bfd_boolean
8487 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8489 if (note->type == NT_OPENBSD_PROCINFO)
8490 return elfcore_grok_openbsd_procinfo (abfd, note);
8492 if (note->type == NT_OPENBSD_REGS)
8493 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8495 if (note->type == NT_OPENBSD_FPREGS)
8496 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8498 if (note->type == NT_OPENBSD_XFPREGS)
8499 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8501 if (note->type == NT_OPENBSD_AUXV)
8503 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8504 SEC_HAS_CONTENTS);
8506 if (sect == NULL)
8507 return FALSE;
8508 sect->size = note->descsz;
8509 sect->filepos = note->descpos;
8510 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8512 return TRUE;
8515 if (note->type == NT_OPENBSD_WCOOKIE)
8517 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8518 SEC_HAS_CONTENTS);
8520 if (sect == NULL)
8521 return FALSE;
8522 sect->size = note->descsz;
8523 sect->filepos = note->descpos;
8524 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8526 return TRUE;
8529 return TRUE;
8532 static bfd_boolean
8533 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8535 void *ddata = note->descdata;
8536 char buf[100];
8537 char *name;
8538 asection *sect;
8539 short sig;
8540 unsigned flags;
8542 /* nto_procfs_status 'pid' field is at offset 0. */
8543 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8545 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8546 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8548 /* nto_procfs_status 'flags' field is at offset 8. */
8549 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8551 /* nto_procfs_status 'what' field is at offset 14. */
8552 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8554 elf_tdata (abfd)->core_signal = sig;
8555 elf_tdata (abfd)->core_lwpid = *tid;
8558 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8559 do not come from signals so we make sure we set the current
8560 thread just in case. */
8561 if (flags & 0x00000080)
8562 elf_tdata (abfd)->core_lwpid = *tid;
8564 /* Make a ".qnx_core_status/%d" section. */
8565 sprintf (buf, ".qnx_core_status/%ld", *tid);
8567 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8568 if (name == NULL)
8569 return FALSE;
8570 strcpy (name, buf);
8572 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8573 if (sect == NULL)
8574 return FALSE;
8576 sect->size = note->descsz;
8577 sect->filepos = note->descpos;
8578 sect->alignment_power = 2;
8580 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8583 static bfd_boolean
8584 elfcore_grok_nto_regs (bfd *abfd,
8585 Elf_Internal_Note *note,
8586 long tid,
8587 char *base)
8589 char buf[100];
8590 char *name;
8591 asection *sect;
8593 /* Make a "(base)/%d" section. */
8594 sprintf (buf, "%s/%ld", base, tid);
8596 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8597 if (name == NULL)
8598 return FALSE;
8599 strcpy (name, buf);
8601 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8602 if (sect == NULL)
8603 return FALSE;
8605 sect->size = note->descsz;
8606 sect->filepos = note->descpos;
8607 sect->alignment_power = 2;
8609 /* This is the current thread. */
8610 if (elf_tdata (abfd)->core_lwpid == tid)
8611 return elfcore_maybe_make_sect (abfd, base, sect);
8613 return TRUE;
8616 #define BFD_QNT_CORE_INFO 7
8617 #define BFD_QNT_CORE_STATUS 8
8618 #define BFD_QNT_CORE_GREG 9
8619 #define BFD_QNT_CORE_FPREG 10
8621 static bfd_boolean
8622 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8624 /* Every GREG section has a STATUS section before it. Store the
8625 tid from the previous call to pass down to the next gregs
8626 function. */
8627 static long tid = 1;
8629 switch (note->type)
8631 case BFD_QNT_CORE_INFO:
8632 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8633 case BFD_QNT_CORE_STATUS:
8634 return elfcore_grok_nto_status (abfd, note, &tid);
8635 case BFD_QNT_CORE_GREG:
8636 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8637 case BFD_QNT_CORE_FPREG:
8638 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8639 default:
8640 return TRUE;
8644 static bfd_boolean
8645 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8647 char *name;
8648 asection *sect;
8649 size_t len;
8651 /* Use note name as section name. */
8652 len = note->namesz;
8653 name = (char *) bfd_alloc (abfd, len);
8654 if (name == NULL)
8655 return FALSE;
8656 memcpy (name, note->namedata, len);
8657 name[len - 1] = '\0';
8659 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8660 if (sect == NULL)
8661 return FALSE;
8663 sect->size = note->descsz;
8664 sect->filepos = note->descpos;
8665 sect->alignment_power = 1;
8667 return TRUE;
8670 /* Function: elfcore_write_note
8672 Inputs:
8673 buffer to hold note, and current size of buffer
8674 name of note
8675 type of note
8676 data for note
8677 size of data for note
8679 Writes note to end of buffer. ELF64 notes are written exactly as
8680 for ELF32, despite the current (as of 2006) ELF gabi specifying
8681 that they ought to have 8-byte namesz and descsz field, and have
8682 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8684 Return:
8685 Pointer to realloc'd buffer, *BUFSIZ updated. */
8687 char *
8688 elfcore_write_note (bfd *abfd,
8689 char *buf,
8690 int *bufsiz,
8691 const char *name,
8692 int type,
8693 const void *input,
8694 int size)
8696 Elf_External_Note *xnp;
8697 size_t namesz;
8698 size_t newspace;
8699 char *dest;
8701 namesz = 0;
8702 if (name != NULL)
8703 namesz = strlen (name) + 1;
8705 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8707 buf = (char *) realloc (buf, *bufsiz + newspace);
8708 if (buf == NULL)
8709 return buf;
8710 dest = buf + *bufsiz;
8711 *bufsiz += newspace;
8712 xnp = (Elf_External_Note *) dest;
8713 H_PUT_32 (abfd, namesz, xnp->namesz);
8714 H_PUT_32 (abfd, size, xnp->descsz);
8715 H_PUT_32 (abfd, type, xnp->type);
8716 dest = xnp->name;
8717 if (name != NULL)
8719 memcpy (dest, name, namesz);
8720 dest += namesz;
8721 while (namesz & 3)
8723 *dest++ = '\0';
8724 ++namesz;
8727 memcpy (dest, input, size);
8728 dest += size;
8729 while (size & 3)
8731 *dest++ = '\0';
8732 ++size;
8734 return buf;
8737 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8738 char *
8739 elfcore_write_prpsinfo (bfd *abfd,
8740 char *buf,
8741 int *bufsiz,
8742 const char *fname,
8743 const char *psargs)
8745 const char *note_name = "CORE";
8746 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8748 if (bed->elf_backend_write_core_note != NULL)
8750 char *ret;
8751 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8752 NT_PRPSINFO, fname, psargs);
8753 if (ret != NULL)
8754 return ret;
8757 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8758 if (bed->s->elfclass == ELFCLASS32)
8760 #if defined (HAVE_PSINFO32_T)
8761 psinfo32_t data;
8762 int note_type = NT_PSINFO;
8763 #else
8764 prpsinfo32_t data;
8765 int note_type = NT_PRPSINFO;
8766 #endif
8768 memset (&data, 0, sizeof (data));
8769 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8770 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8771 return elfcore_write_note (abfd, buf, bufsiz,
8772 note_name, note_type, &data, sizeof (data));
8774 else
8775 #endif
8777 #if defined (HAVE_PSINFO_T)
8778 psinfo_t data;
8779 int note_type = NT_PSINFO;
8780 #else
8781 prpsinfo_t data;
8782 int note_type = NT_PRPSINFO;
8783 #endif
8785 memset (&data, 0, sizeof (data));
8786 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8787 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8788 return elfcore_write_note (abfd, buf, bufsiz,
8789 note_name, note_type, &data, sizeof (data));
8792 #endif /* PSINFO_T or PRPSINFO_T */
8794 #if defined (HAVE_PRSTATUS_T)
8795 char *
8796 elfcore_write_prstatus (bfd *abfd,
8797 char *buf,
8798 int *bufsiz,
8799 long pid,
8800 int cursig,
8801 const void *gregs)
8803 const char *note_name = "CORE";
8804 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8806 if (bed->elf_backend_write_core_note != NULL)
8808 char *ret;
8809 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8810 NT_PRSTATUS,
8811 pid, cursig, gregs);
8812 if (ret != NULL)
8813 return ret;
8816 #if defined (HAVE_PRSTATUS32_T)
8817 if (bed->s->elfclass == ELFCLASS32)
8819 prstatus32_t prstat;
8821 memset (&prstat, 0, sizeof (prstat));
8822 prstat.pr_pid = pid;
8823 prstat.pr_cursig = cursig;
8824 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8825 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8826 NT_PRSTATUS, &prstat, sizeof (prstat));
8828 else
8829 #endif
8831 prstatus_t prstat;
8833 memset (&prstat, 0, sizeof (prstat));
8834 prstat.pr_pid = pid;
8835 prstat.pr_cursig = cursig;
8836 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8837 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8838 NT_PRSTATUS, &prstat, sizeof (prstat));
8841 #endif /* HAVE_PRSTATUS_T */
8843 #if defined (HAVE_LWPSTATUS_T)
8844 char *
8845 elfcore_write_lwpstatus (bfd *abfd,
8846 char *buf,
8847 int *bufsiz,
8848 long pid,
8849 int cursig,
8850 const void *gregs)
8852 lwpstatus_t lwpstat;
8853 const char *note_name = "CORE";
8855 memset (&lwpstat, 0, sizeof (lwpstat));
8856 lwpstat.pr_lwpid = pid >> 16;
8857 lwpstat.pr_cursig = cursig;
8858 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8859 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8860 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8861 #if !defined(gregs)
8862 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8863 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8864 #else
8865 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8866 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8867 #endif
8868 #endif
8869 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8870 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8872 #endif /* HAVE_LWPSTATUS_T */
8874 #if defined (HAVE_PSTATUS_T)
8875 char *
8876 elfcore_write_pstatus (bfd *abfd,
8877 char *buf,
8878 int *bufsiz,
8879 long pid,
8880 int cursig ATTRIBUTE_UNUSED,
8881 const void *gregs ATTRIBUTE_UNUSED)
8883 const char *note_name = "CORE";
8884 #if defined (HAVE_PSTATUS32_T)
8885 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8887 if (bed->s->elfclass == ELFCLASS32)
8889 pstatus32_t pstat;
8891 memset (&pstat, 0, sizeof (pstat));
8892 pstat.pr_pid = pid & 0xffff;
8893 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8894 NT_PSTATUS, &pstat, sizeof (pstat));
8895 return buf;
8897 else
8898 #endif
8900 pstatus_t pstat;
8902 memset (&pstat, 0, sizeof (pstat));
8903 pstat.pr_pid = pid & 0xffff;
8904 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8905 NT_PSTATUS, &pstat, sizeof (pstat));
8906 return buf;
8909 #endif /* HAVE_PSTATUS_T */
8911 char *
8912 elfcore_write_prfpreg (bfd *abfd,
8913 char *buf,
8914 int *bufsiz,
8915 const void *fpregs,
8916 int size)
8918 const char *note_name = "CORE";
8919 return elfcore_write_note (abfd, buf, bufsiz,
8920 note_name, NT_FPREGSET, fpregs, size);
8923 char *
8924 elfcore_write_prxfpreg (bfd *abfd,
8925 char *buf,
8926 int *bufsiz,
8927 const void *xfpregs,
8928 int size)
8930 char *note_name = "LINUX";
8931 return elfcore_write_note (abfd, buf, bufsiz,
8932 note_name, NT_PRXFPREG, xfpregs, size);
8935 char *
8936 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
8937 const void *xfpregs, int size)
8939 char *note_name = "LINUX";
8940 return elfcore_write_note (abfd, buf, bufsiz,
8941 note_name, NT_X86_XSTATE, xfpregs, size);
8944 char *
8945 elfcore_write_ppc_vmx (bfd *abfd,
8946 char *buf,
8947 int *bufsiz,
8948 const void *ppc_vmx,
8949 int size)
8951 char *note_name = "LINUX";
8952 return elfcore_write_note (abfd, buf, bufsiz,
8953 note_name, NT_PPC_VMX, ppc_vmx, size);
8956 char *
8957 elfcore_write_ppc_vsx (bfd *abfd,
8958 char *buf,
8959 int *bufsiz,
8960 const void *ppc_vsx,
8961 int size)
8963 char *note_name = "LINUX";
8964 return elfcore_write_note (abfd, buf, bufsiz,
8965 note_name, NT_PPC_VSX, ppc_vsx, size);
8968 static char *
8969 elfcore_write_s390_high_gprs (bfd *abfd,
8970 char *buf,
8971 int *bufsiz,
8972 const void *s390_high_gprs,
8973 int size)
8975 char *note_name = "LINUX";
8976 return elfcore_write_note (abfd, buf, bufsiz,
8977 note_name, NT_S390_HIGH_GPRS,
8978 s390_high_gprs, size);
8981 char *
8982 elfcore_write_s390_timer (bfd *abfd,
8983 char *buf,
8984 int *bufsiz,
8985 const void *s390_timer,
8986 int size)
8988 char *note_name = "LINUX";
8989 return elfcore_write_note (abfd, buf, bufsiz,
8990 note_name, NT_S390_TIMER, s390_timer, size);
8993 char *
8994 elfcore_write_s390_todcmp (bfd *abfd,
8995 char *buf,
8996 int *bufsiz,
8997 const void *s390_todcmp,
8998 int size)
9000 char *note_name = "LINUX";
9001 return elfcore_write_note (abfd, buf, bufsiz,
9002 note_name, NT_S390_TODCMP, s390_todcmp, size);
9005 char *
9006 elfcore_write_s390_todpreg (bfd *abfd,
9007 char *buf,
9008 int *bufsiz,
9009 const void *s390_todpreg,
9010 int size)
9012 char *note_name = "LINUX";
9013 return elfcore_write_note (abfd, buf, bufsiz,
9014 note_name, NT_S390_TODPREG, s390_todpreg, size);
9017 char *
9018 elfcore_write_s390_ctrs (bfd *abfd,
9019 char *buf,
9020 int *bufsiz,
9021 const void *s390_ctrs,
9022 int size)
9024 char *note_name = "LINUX";
9025 return elfcore_write_note (abfd, buf, bufsiz,
9026 note_name, NT_S390_CTRS, s390_ctrs, size);
9029 char *
9030 elfcore_write_s390_prefix (bfd *abfd,
9031 char *buf,
9032 int *bufsiz,
9033 const void *s390_prefix,
9034 int size)
9036 char *note_name = "LINUX";
9037 return elfcore_write_note (abfd, buf, bufsiz,
9038 note_name, NT_S390_PREFIX, s390_prefix, size);
9041 char *
9042 elfcore_write_register_note (bfd *abfd,
9043 char *buf,
9044 int *bufsiz,
9045 const char *section,
9046 const void *data,
9047 int size)
9049 if (strcmp (section, ".reg2") == 0)
9050 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
9051 if (strcmp (section, ".reg-xfp") == 0)
9052 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
9053 if (strcmp (section, ".reg-xstate") == 0)
9054 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
9055 if (strcmp (section, ".reg-ppc-vmx") == 0)
9056 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
9057 if (strcmp (section, ".reg-ppc-vsx") == 0)
9058 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
9059 if (strcmp (section, ".reg-s390-high-gprs") == 0)
9060 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
9061 if (strcmp (section, ".reg-s390-timer") == 0)
9062 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
9063 if (strcmp (section, ".reg-s390-todcmp") == 0)
9064 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
9065 if (strcmp (section, ".reg-s390-todpreg") == 0)
9066 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
9067 if (strcmp (section, ".reg-s390-ctrs") == 0)
9068 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
9069 if (strcmp (section, ".reg-s390-prefix") == 0)
9070 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
9071 return NULL;
9074 static bfd_boolean
9075 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
9077 char *p;
9079 p = buf;
9080 while (p < buf + size)
9082 /* FIXME: bad alignment assumption. */
9083 Elf_External_Note *xnp = (Elf_External_Note *) p;
9084 Elf_Internal_Note in;
9086 if (offsetof (Elf_External_Note, name) > buf - p + size)
9087 return FALSE;
9089 in.type = H_GET_32 (abfd, xnp->type);
9091 in.namesz = H_GET_32 (abfd, xnp->namesz);
9092 in.namedata = xnp->name;
9093 if (in.namesz > buf - in.namedata + size)
9094 return FALSE;
9096 in.descsz = H_GET_32 (abfd, xnp->descsz);
9097 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
9098 in.descpos = offset + (in.descdata - buf);
9099 if (in.descsz != 0
9100 && (in.descdata >= buf + size
9101 || in.descsz > buf - in.descdata + size))
9102 return FALSE;
9104 switch (bfd_get_format (abfd))
9106 default:
9107 return TRUE;
9109 case bfd_core:
9110 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
9112 if (! elfcore_grok_netbsd_note (abfd, &in))
9113 return FALSE;
9115 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
9117 if (! elfcore_grok_openbsd_note (abfd, &in))
9118 return FALSE;
9120 else if (CONST_STRNEQ (in.namedata, "QNX"))
9122 if (! elfcore_grok_nto_note (abfd, &in))
9123 return FALSE;
9125 else if (CONST_STRNEQ (in.namedata, "SPU/"))
9127 if (! elfcore_grok_spu_note (abfd, &in))
9128 return FALSE;
9130 else
9132 if (! elfcore_grok_note (abfd, &in))
9133 return FALSE;
9135 break;
9137 case bfd_object:
9138 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9140 if (! elfobj_grok_gnu_note (abfd, &in))
9141 return FALSE;
9143 break;
9146 p = in.descdata + BFD_ALIGN (in.descsz, 4);
9149 return TRUE;
9152 static bfd_boolean
9153 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
9155 char *buf;
9157 if (size <= 0)
9158 return TRUE;
9160 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9161 return FALSE;
9163 buf = (char *) bfd_malloc (size);
9164 if (buf == NULL)
9165 return FALSE;
9167 if (bfd_bread (buf, size, abfd) != size
9168 || !elf_parse_notes (abfd, buf, size, offset))
9170 free (buf);
9171 return FALSE;
9174 free (buf);
9175 return TRUE;
9178 /* Providing external access to the ELF program header table. */
9180 /* Return an upper bound on the number of bytes required to store a
9181 copy of ABFD's program header table entries. Return -1 if an error
9182 occurs; bfd_get_error will return an appropriate code. */
9184 long
9185 bfd_get_elf_phdr_upper_bound (bfd *abfd)
9187 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9189 bfd_set_error (bfd_error_wrong_format);
9190 return -1;
9193 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9196 /* Copy ABFD's program header table entries to *PHDRS. The entries
9197 will be stored as an array of Elf_Internal_Phdr structures, as
9198 defined in include/elf/internal.h. To find out how large the
9199 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9201 Return the number of program header table entries read, or -1 if an
9202 error occurs; bfd_get_error will return an appropriate code. */
9205 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9207 int num_phdrs;
9209 if (abfd->xvec->flavour != bfd_target_elf_flavour)
9211 bfd_set_error (bfd_error_wrong_format);
9212 return -1;
9215 num_phdrs = elf_elfheader (abfd)->e_phnum;
9216 memcpy (phdrs, elf_tdata (abfd)->phdr,
9217 num_phdrs * sizeof (Elf_Internal_Phdr));
9219 return num_phdrs;
9222 enum elf_reloc_type_class
9223 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9225 return reloc_class_normal;
9228 /* For RELA architectures, return the relocation value for a
9229 relocation against a local symbol. */
9231 bfd_vma
9232 _bfd_elf_rela_local_sym (bfd *abfd,
9233 Elf_Internal_Sym *sym,
9234 asection **psec,
9235 Elf_Internal_Rela *rel)
9237 asection *sec = *psec;
9238 bfd_vma relocation;
9240 relocation = (sec->output_section->vma
9241 + sec->output_offset
9242 + sym->st_value);
9243 if ((sec->flags & SEC_MERGE)
9244 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9245 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
9247 rel->r_addend =
9248 _bfd_merged_section_offset (abfd, psec,
9249 elf_section_data (sec)->sec_info,
9250 sym->st_value + rel->r_addend);
9251 if (sec != *psec)
9253 /* If we have changed the section, and our original section is
9254 marked with SEC_EXCLUDE, it means that the original
9255 SEC_MERGE section has been completely subsumed in some
9256 other SEC_MERGE section. In this case, we need to leave
9257 some info around for --emit-relocs. */
9258 if ((sec->flags & SEC_EXCLUDE) != 0)
9259 sec->kept_section = *psec;
9260 sec = *psec;
9262 rel->r_addend -= relocation;
9263 rel->r_addend += sec->output_section->vma + sec->output_offset;
9265 return relocation;
9268 bfd_vma
9269 _bfd_elf_rel_local_sym (bfd *abfd,
9270 Elf_Internal_Sym *sym,
9271 asection **psec,
9272 bfd_vma addend)
9274 asection *sec = *psec;
9276 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
9277 return sym->st_value + addend;
9279 return _bfd_merged_section_offset (abfd, psec,
9280 elf_section_data (sec)->sec_info,
9281 sym->st_value + addend);
9284 bfd_vma
9285 _bfd_elf_section_offset (bfd *abfd,
9286 struct bfd_link_info *info,
9287 asection *sec,
9288 bfd_vma offset)
9290 switch (sec->sec_info_type)
9292 case ELF_INFO_TYPE_STABS:
9293 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9294 offset);
9295 case ELF_INFO_TYPE_EH_FRAME:
9296 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9297 default:
9298 return offset;
9302 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9303 reconstruct an ELF file by reading the segments out of remote memory
9304 based on the ELF file header at EHDR_VMA and the ELF program headers it
9305 points to. If not null, *LOADBASEP is filled in with the difference
9306 between the VMAs from which the segments were read, and the VMAs the
9307 file headers (and hence BFD's idea of each section's VMA) put them at.
9309 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9310 remote memory at target address VMA into the local buffer at MYADDR; it
9311 should return zero on success or an `errno' code on failure. TEMPL must
9312 be a BFD for an ELF target with the word size and byte order found in
9313 the remote memory. */
9315 bfd *
9316 bfd_elf_bfd_from_remote_memory
9317 (bfd *templ,
9318 bfd_vma ehdr_vma,
9319 bfd_vma *loadbasep,
9320 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
9322 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9323 (templ, ehdr_vma, loadbasep, target_read_memory);
9326 long
9327 _bfd_elf_get_synthetic_symtab (bfd *abfd,
9328 long symcount ATTRIBUTE_UNUSED,
9329 asymbol **syms ATTRIBUTE_UNUSED,
9330 long dynsymcount,
9331 asymbol **dynsyms,
9332 asymbol **ret)
9334 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9335 asection *relplt;
9336 asymbol *s;
9337 const char *relplt_name;
9338 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
9339 arelent *p;
9340 long count, i, n;
9341 size_t size;
9342 Elf_Internal_Shdr *hdr;
9343 char *names;
9344 asection *plt;
9346 *ret = NULL;
9348 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
9349 return 0;
9351 if (dynsymcount <= 0)
9352 return 0;
9354 if (!bed->plt_sym_val)
9355 return 0;
9357 relplt_name = bed->relplt_name;
9358 if (relplt_name == NULL)
9359 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
9360 relplt = bfd_get_section_by_name (abfd, relplt_name);
9361 if (relplt == NULL)
9362 return 0;
9364 hdr = &elf_section_data (relplt)->this_hdr;
9365 if (hdr->sh_link != elf_dynsymtab (abfd)
9366 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
9367 return 0;
9369 plt = bfd_get_section_by_name (abfd, ".plt");
9370 if (plt == NULL)
9371 return 0;
9373 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9374 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
9375 return -1;
9377 count = relplt->size / hdr->sh_entsize;
9378 size = count * sizeof (asymbol);
9379 p = relplt->relocation;
9380 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9382 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
9383 if (p->addend != 0)
9385 #ifdef BFD64
9386 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
9387 #else
9388 size += sizeof ("+0x") - 1 + 8;
9389 #endif
9393 s = *ret = (asymbol *) bfd_malloc (size);
9394 if (s == NULL)
9395 return -1;
9397 names = (char *) (s + count);
9398 p = relplt->relocation;
9399 n = 0;
9400 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9402 size_t len;
9403 bfd_vma addr;
9405 addr = bed->plt_sym_val (i, plt, p);
9406 if (addr == (bfd_vma) -1)
9407 continue;
9409 *s = **p->sym_ptr_ptr;
9410 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9411 we are defining a symbol, ensure one of them is set. */
9412 if ((s->flags & BSF_LOCAL) == 0)
9413 s->flags |= BSF_GLOBAL;
9414 s->flags |= BSF_SYNTHETIC;
9415 s->section = plt;
9416 s->value = addr - plt->vma;
9417 s->name = names;
9418 s->udata.p = NULL;
9419 len = strlen ((*p->sym_ptr_ptr)->name);
9420 memcpy (names, (*p->sym_ptr_ptr)->name, len);
9421 names += len;
9422 if (p->addend != 0)
9424 char buf[30], *a;
9426 memcpy (names, "+0x", sizeof ("+0x") - 1);
9427 names += sizeof ("+0x") - 1;
9428 bfd_sprintf_vma (abfd, buf, p->addend);
9429 for (a = buf; *a == '0'; ++a)
9431 len = strlen (a);
9432 memcpy (names, a, len);
9433 names += len;
9435 memcpy (names, "@plt", sizeof ("@plt"));
9436 names += sizeof ("@plt");
9437 ++s, ++n;
9440 return n;
9443 /* It is only used by x86-64 so far. */
9444 asection _bfd_elf_large_com_section
9445 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
9446 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
9448 void
9449 _bfd_elf_set_osabi (bfd * abfd,
9450 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
9452 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
9454 i_ehdrp = elf_elfheader (abfd);
9456 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
9458 /* To make things simpler for the loader on Linux systems we set the
9459 osabi field to ELFOSABI_LINUX if the binary contains symbols of
9460 the STT_GNU_IFUNC type. */
9461 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
9462 && elf_tdata (abfd)->has_ifunc_symbols)
9463 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
9467 /* Return TRUE for ELF symbol types that represent functions.
9468 This is the default version of this function, which is sufficient for
9469 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
9471 bfd_boolean
9472 _bfd_elf_is_function_type (unsigned int type)
9474 return (type == STT_FUNC
9475 || type == STT_GNU_IFUNC);