bfd/
[binutils.git] / bfd / elf.c
blobd65c78d963a413792fb4bbd254894e695cc736c9
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 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
25 SECTION
26 ELF backends
28 BFD support for ELF formats is being worked on.
29 Currently, the best supported back ends are for sparc and i386
30 (running svr4 or Solaris 2).
32 Documentation of the internals of the support code still needs
33 to be written. The code is changing quickly enough that we
34 haven't bothered yet. */
36 /* For sparc64-cross-sparc32. */
37 #define _SYSCALL32
38 #include "sysdep.h"
39 #include "bfd.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44 #include "libiberty.h"
45 #include "safe-ctype.h"
47 static int elf_sort_sections (const void *, const void *);
48 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
49 static bfd_boolean prep_headers (bfd *);
50 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
51 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
52 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
53 file_ptr offset);
55 /* Swap version information in and out. The version information is
56 currently size independent. If that ever changes, this code will
57 need to move into elfcode.h. */
59 /* Swap in a Verdef structure. */
61 void
62 _bfd_elf_swap_verdef_in (bfd *abfd,
63 const Elf_External_Verdef *src,
64 Elf_Internal_Verdef *dst)
66 dst->vd_version = H_GET_16 (abfd, src->vd_version);
67 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
68 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
69 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
70 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
71 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
72 dst->vd_next = H_GET_32 (abfd, src->vd_next);
75 /* Swap out a Verdef structure. */
77 void
78 _bfd_elf_swap_verdef_out (bfd *abfd,
79 const Elf_Internal_Verdef *src,
80 Elf_External_Verdef *dst)
82 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
83 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
84 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
85 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
86 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
87 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
88 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
91 /* Swap in a Verdaux structure. */
93 void
94 _bfd_elf_swap_verdaux_in (bfd *abfd,
95 const Elf_External_Verdaux *src,
96 Elf_Internal_Verdaux *dst)
98 dst->vda_name = H_GET_32 (abfd, src->vda_name);
99 dst->vda_next = H_GET_32 (abfd, src->vda_next);
102 /* Swap out a Verdaux structure. */
104 void
105 _bfd_elf_swap_verdaux_out (bfd *abfd,
106 const Elf_Internal_Verdaux *src,
107 Elf_External_Verdaux *dst)
109 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
110 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
113 /* Swap in a Verneed structure. */
115 void
116 _bfd_elf_swap_verneed_in (bfd *abfd,
117 const Elf_External_Verneed *src,
118 Elf_Internal_Verneed *dst)
120 dst->vn_version = H_GET_16 (abfd, src->vn_version);
121 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
122 dst->vn_file = H_GET_32 (abfd, src->vn_file);
123 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
124 dst->vn_next = H_GET_32 (abfd, src->vn_next);
127 /* Swap out a Verneed structure. */
129 void
130 _bfd_elf_swap_verneed_out (bfd *abfd,
131 const Elf_Internal_Verneed *src,
132 Elf_External_Verneed *dst)
134 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
135 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
136 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
137 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
138 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
141 /* Swap in a Vernaux structure. */
143 void
144 _bfd_elf_swap_vernaux_in (bfd *abfd,
145 const Elf_External_Vernaux *src,
146 Elf_Internal_Vernaux *dst)
148 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
149 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
150 dst->vna_other = H_GET_16 (abfd, src->vna_other);
151 dst->vna_name = H_GET_32 (abfd, src->vna_name);
152 dst->vna_next = H_GET_32 (abfd, src->vna_next);
155 /* Swap out a Vernaux structure. */
157 void
158 _bfd_elf_swap_vernaux_out (bfd *abfd,
159 const Elf_Internal_Vernaux *src,
160 Elf_External_Vernaux *dst)
162 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
163 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
164 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
165 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
166 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
169 /* Swap in a Versym structure. */
171 void
172 _bfd_elf_swap_versym_in (bfd *abfd,
173 const Elf_External_Versym *src,
174 Elf_Internal_Versym *dst)
176 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
179 /* Swap out a Versym structure. */
181 void
182 _bfd_elf_swap_versym_out (bfd *abfd,
183 const Elf_Internal_Versym *src,
184 Elf_External_Versym *dst)
186 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
189 /* Standard ELF hash function. Do not change this function; you will
190 cause invalid hash tables to be generated. */
192 unsigned long
193 bfd_elf_hash (const char *namearg)
195 const unsigned char *name = (const unsigned char *) namearg;
196 unsigned long h = 0;
197 unsigned long g;
198 int ch;
200 while ((ch = *name++) != '\0')
202 h = (h << 4) + ch;
203 if ((g = (h & 0xf0000000)) != 0)
205 h ^= g >> 24;
206 /* The ELF ABI says `h &= ~g', but this is equivalent in
207 this case and on some machines one insn instead of two. */
208 h ^= g;
211 return h & 0xffffffff;
214 /* DT_GNU_HASH hash function. Do not change this function; you will
215 cause invalid hash tables to be generated. */
217 unsigned long
218 bfd_elf_gnu_hash (const char *namearg)
220 const unsigned char *name = (const unsigned char *) namearg;
221 unsigned long h = 5381;
222 unsigned char ch;
224 while ((ch = *name++) != '\0')
225 h = (h << 5) + h + ch;
226 return h & 0xffffffff;
229 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
230 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
231 bfd_boolean
232 bfd_elf_allocate_object (bfd *abfd,
233 size_t object_size,
234 enum elf_object_id object_id)
236 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
237 abfd->tdata.any = bfd_zalloc (abfd, object_size);
238 if (abfd->tdata.any == NULL)
239 return FALSE;
241 elf_object_id (abfd) = object_id;
242 elf_program_header_size (abfd) = (bfd_size_type) -1;
243 return TRUE;
247 bfd_boolean
248 bfd_elf_make_generic_object (bfd *abfd)
250 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
251 GENERIC_ELF_TDATA);
254 bfd_boolean
255 bfd_elf_mkcorefile (bfd *abfd)
257 /* I think this can be done just like an object file. */
258 return bfd_elf_make_generic_object (abfd);
261 char *
262 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
264 Elf_Internal_Shdr **i_shdrp;
265 bfd_byte *shstrtab = NULL;
266 file_ptr offset;
267 bfd_size_type shstrtabsize;
269 i_shdrp = elf_elfsections (abfd);
270 if (i_shdrp == 0
271 || shindex >= elf_numsections (abfd)
272 || i_shdrp[shindex] == 0)
273 return NULL;
275 shstrtab = i_shdrp[shindex]->contents;
276 if (shstrtab == NULL)
278 /* No cached one, attempt to read, and cache what we read. */
279 offset = i_shdrp[shindex]->sh_offset;
280 shstrtabsize = i_shdrp[shindex]->sh_size;
282 /* Allocate and clear an extra byte at the end, to prevent crashes
283 in case the string table is not terminated. */
284 if (shstrtabsize + 1 <= 1
285 || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL
286 || bfd_seek (abfd, offset, SEEK_SET) != 0)
287 shstrtab = NULL;
288 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
290 if (bfd_get_error () != bfd_error_system_call)
291 bfd_set_error (bfd_error_file_truncated);
292 shstrtab = NULL;
293 /* Once we've failed to read it, make sure we don't keep
294 trying. Otherwise, we'll keep allocating space for
295 the string table over and over. */
296 i_shdrp[shindex]->sh_size = 0;
298 else
299 shstrtab[shstrtabsize] = '\0';
300 i_shdrp[shindex]->contents = shstrtab;
302 return (char *) shstrtab;
305 char *
306 bfd_elf_string_from_elf_section (bfd *abfd,
307 unsigned int shindex,
308 unsigned int strindex)
310 Elf_Internal_Shdr *hdr;
312 if (strindex == 0)
313 return "";
315 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
316 return NULL;
318 hdr = elf_elfsections (abfd)[shindex];
320 if (hdr->contents == NULL
321 && bfd_elf_get_str_section (abfd, shindex) == NULL)
322 return NULL;
324 if (strindex >= hdr->sh_size)
326 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
327 (*_bfd_error_handler)
328 (_("%B: invalid string offset %u >= %lu for section `%s'"),
329 abfd, strindex, (unsigned long) hdr->sh_size,
330 (shindex == shstrndx && strindex == hdr->sh_name
331 ? ".shstrtab"
332 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
333 return "";
336 return ((char *) hdr->contents) + strindex;
339 /* Read and convert symbols to internal format.
340 SYMCOUNT specifies the number of symbols to read, starting from
341 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
342 are non-NULL, they are used to store the internal symbols, external
343 symbols, and symbol section index extensions, respectively.
344 Returns a pointer to the internal symbol buffer (malloced if necessary)
345 or NULL if there were no symbols or some kind of problem. */
347 Elf_Internal_Sym *
348 bfd_elf_get_elf_syms (bfd *ibfd,
349 Elf_Internal_Shdr *symtab_hdr,
350 size_t symcount,
351 size_t symoffset,
352 Elf_Internal_Sym *intsym_buf,
353 void *extsym_buf,
354 Elf_External_Sym_Shndx *extshndx_buf)
356 Elf_Internal_Shdr *shndx_hdr;
357 void *alloc_ext;
358 const bfd_byte *esym;
359 Elf_External_Sym_Shndx *alloc_extshndx;
360 Elf_External_Sym_Shndx *shndx;
361 Elf_Internal_Sym *isym;
362 Elf_Internal_Sym *isymend;
363 const struct elf_backend_data *bed;
364 size_t extsym_size;
365 bfd_size_type amt;
366 file_ptr pos;
368 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
369 abort ();
371 if (symcount == 0)
372 return intsym_buf;
374 /* Normal syms might have section extension entries. */
375 shndx_hdr = NULL;
376 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
377 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
379 /* Read the symbols. */
380 alloc_ext = NULL;
381 alloc_extshndx = NULL;
382 bed = get_elf_backend_data (ibfd);
383 extsym_size = bed->s->sizeof_sym;
384 amt = symcount * extsym_size;
385 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
386 if (extsym_buf == NULL)
388 alloc_ext = bfd_malloc2 (symcount, extsym_size);
389 extsym_buf = alloc_ext;
391 if (extsym_buf == NULL
392 || bfd_seek (ibfd, pos, SEEK_SET) != 0
393 || bfd_bread (extsym_buf, amt, ibfd) != amt)
395 intsym_buf = NULL;
396 goto out;
399 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
400 extshndx_buf = NULL;
401 else
403 amt = symcount * sizeof (Elf_External_Sym_Shndx);
404 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
405 if (extshndx_buf == NULL)
407 alloc_extshndx = bfd_malloc2 (symcount,
408 sizeof (Elf_External_Sym_Shndx));
409 extshndx_buf = alloc_extshndx;
411 if (extshndx_buf == NULL
412 || bfd_seek (ibfd, pos, SEEK_SET) != 0
413 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
415 intsym_buf = NULL;
416 goto out;
420 if (intsym_buf == NULL)
422 intsym_buf = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
423 if (intsym_buf == NULL)
424 goto out;
427 /* Convert the symbols to internal form. */
428 isymend = intsym_buf + symcount;
429 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
430 isym < isymend;
431 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
432 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
434 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
435 (*_bfd_error_handler) (_("%B symbol number %lu references "
436 "nonexistent SHT_SYMTAB_SHNDX section"),
437 ibfd, (unsigned long) symoffset);
438 intsym_buf = NULL;
439 goto out;
442 out:
443 if (alloc_ext != NULL)
444 free (alloc_ext);
445 if (alloc_extshndx != NULL)
446 free (alloc_extshndx);
448 return intsym_buf;
451 /* Look up a symbol name. */
452 const char *
453 bfd_elf_sym_name (bfd *abfd,
454 Elf_Internal_Shdr *symtab_hdr,
455 Elf_Internal_Sym *isym,
456 asection *sym_sec)
458 const char *name;
459 unsigned int iname = isym->st_name;
460 unsigned int shindex = symtab_hdr->sh_link;
462 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
463 /* Check for a bogus st_shndx to avoid crashing. */
464 && isym->st_shndx < elf_numsections (abfd))
466 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
467 shindex = elf_elfheader (abfd)->e_shstrndx;
470 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
471 if (name == NULL)
472 name = "(null)";
473 else if (sym_sec && *name == '\0')
474 name = bfd_section_name (abfd, sym_sec);
476 return name;
479 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
480 sections. The first element is the flags, the rest are section
481 pointers. */
483 typedef union elf_internal_group {
484 Elf_Internal_Shdr *shdr;
485 unsigned int flags;
486 } Elf_Internal_Group;
488 /* Return the name of the group signature symbol. Why isn't the
489 signature just a string? */
491 static const char *
492 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
494 Elf_Internal_Shdr *hdr;
495 unsigned char esym[sizeof (Elf64_External_Sym)];
496 Elf_External_Sym_Shndx eshndx;
497 Elf_Internal_Sym isym;
499 /* First we need to ensure the symbol table is available. Make sure
500 that it is a symbol table section. */
501 if (ghdr->sh_link >= elf_numsections (abfd))
502 return NULL;
503 hdr = elf_elfsections (abfd) [ghdr->sh_link];
504 if (hdr->sh_type != SHT_SYMTAB
505 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
506 return NULL;
508 /* Go read the symbol. */
509 hdr = &elf_tdata (abfd)->symtab_hdr;
510 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
511 &isym, esym, &eshndx) == NULL)
512 return NULL;
514 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
517 /* Set next_in_group list pointer, and group name for NEWSECT. */
519 static bfd_boolean
520 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
522 unsigned int num_group = elf_tdata (abfd)->num_group;
524 /* If num_group is zero, read in all SHT_GROUP sections. The count
525 is set to -1 if there are no SHT_GROUP sections. */
526 if (num_group == 0)
528 unsigned int i, shnum;
530 /* First count the number of groups. If we have a SHT_GROUP
531 section with just a flag word (ie. sh_size is 4), ignore it. */
532 shnum = elf_numsections (abfd);
533 num_group = 0;
535 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
536 ( (shdr)->sh_type == SHT_GROUP \
537 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
538 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
539 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
541 for (i = 0; i < shnum; i++)
543 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
545 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
546 num_group += 1;
549 if (num_group == 0)
551 num_group = (unsigned) -1;
552 elf_tdata (abfd)->num_group = num_group;
554 else
556 /* We keep a list of elf section headers for group sections,
557 so we can find them quickly. */
558 bfd_size_type amt;
560 elf_tdata (abfd)->num_group = num_group;
561 elf_tdata (abfd)->group_sect_ptr
562 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
563 if (elf_tdata (abfd)->group_sect_ptr == NULL)
564 return FALSE;
566 num_group = 0;
567 for (i = 0; i < shnum; i++)
569 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
571 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
573 unsigned char *src;
574 Elf_Internal_Group *dest;
576 /* Add to list of sections. */
577 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
578 num_group += 1;
580 /* Read the raw contents. */
581 BFD_ASSERT (sizeof (*dest) >= 4);
582 amt = shdr->sh_size * sizeof (*dest) / 4;
583 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
584 sizeof (*dest) / 4);
585 /* PR binutils/4110: Handle corrupt group headers. */
586 if (shdr->contents == NULL)
588 _bfd_error_handler
589 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
590 bfd_set_error (bfd_error_bad_value);
591 return FALSE;
594 memset (shdr->contents, 0, amt);
596 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
597 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
598 != shdr->sh_size))
599 return FALSE;
601 /* Translate raw contents, a flag word followed by an
602 array of elf section indices all in target byte order,
603 to the flag word followed by an array of elf section
604 pointers. */
605 src = shdr->contents + shdr->sh_size;
606 dest = (Elf_Internal_Group *) (shdr->contents + amt);
607 while (1)
609 unsigned int idx;
611 src -= 4;
612 --dest;
613 idx = H_GET_32 (abfd, src);
614 if (src == shdr->contents)
616 dest->flags = idx;
617 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
618 shdr->bfd_section->flags
619 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
620 break;
622 if (idx >= shnum)
624 ((*_bfd_error_handler)
625 (_("%B: invalid SHT_GROUP entry"), abfd));
626 idx = 0;
628 dest->shdr = elf_elfsections (abfd)[idx];
635 if (num_group != (unsigned) -1)
637 unsigned int i;
639 for (i = 0; i < num_group; i++)
641 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
642 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
643 unsigned int n_elt = shdr->sh_size / 4;
645 /* Look through this group's sections to see if current
646 section is a member. */
647 while (--n_elt != 0)
648 if ((++idx)->shdr == hdr)
650 asection *s = NULL;
652 /* We are a member of this group. Go looking through
653 other members to see if any others are linked via
654 next_in_group. */
655 idx = (Elf_Internal_Group *) shdr->contents;
656 n_elt = shdr->sh_size / 4;
657 while (--n_elt != 0)
658 if ((s = (++idx)->shdr->bfd_section) != NULL
659 && elf_next_in_group (s) != NULL)
660 break;
661 if (n_elt != 0)
663 /* Snarf the group name from other member, and
664 insert current section in circular list. */
665 elf_group_name (newsect) = elf_group_name (s);
666 elf_next_in_group (newsect) = elf_next_in_group (s);
667 elf_next_in_group (s) = newsect;
669 else
671 const char *gname;
673 gname = group_signature (abfd, shdr);
674 if (gname == NULL)
675 return FALSE;
676 elf_group_name (newsect) = gname;
678 /* Start a circular list with one element. */
679 elf_next_in_group (newsect) = newsect;
682 /* If the group section has been created, point to the
683 new member. */
684 if (shdr->bfd_section != NULL)
685 elf_next_in_group (shdr->bfd_section) = newsect;
687 i = num_group - 1;
688 break;
693 if (elf_group_name (newsect) == NULL)
695 (*_bfd_error_handler) (_("%B: no group info for section %A"),
696 abfd, newsect);
698 return TRUE;
701 bfd_boolean
702 _bfd_elf_setup_sections (bfd *abfd)
704 unsigned int i;
705 unsigned int num_group = elf_tdata (abfd)->num_group;
706 bfd_boolean result = TRUE;
707 asection *s;
709 /* Process SHF_LINK_ORDER. */
710 for (s = abfd->sections; s != NULL; s = s->next)
712 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
713 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
715 unsigned int elfsec = this_hdr->sh_link;
716 /* FIXME: The old Intel compiler and old strip/objcopy may
717 not set the sh_link or sh_info fields. Hence we could
718 get the situation where elfsec is 0. */
719 if (elfsec == 0)
721 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
722 if (bed->link_order_error_handler)
723 bed->link_order_error_handler
724 (_("%B: warning: sh_link not set for section `%A'"),
725 abfd, s);
727 else
729 asection *link = NULL;
731 if (elfsec < elf_numsections (abfd))
733 this_hdr = elf_elfsections (abfd)[elfsec];
734 link = this_hdr->bfd_section;
737 /* PR 1991, 2008:
738 Some strip/objcopy may leave an incorrect value in
739 sh_link. We don't want to proceed. */
740 if (link == NULL)
742 (*_bfd_error_handler)
743 (_("%B: sh_link [%d] in section `%A' is incorrect"),
744 s->owner, s, elfsec);
745 result = FALSE;
748 elf_linked_to_section (s) = link;
753 /* Process section groups. */
754 if (num_group == (unsigned) -1)
755 return result;
757 for (i = 0; i < num_group; i++)
759 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
760 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
761 unsigned int n_elt = shdr->sh_size / 4;
763 while (--n_elt != 0)
764 if ((++idx)->shdr->bfd_section)
765 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
766 else if (idx->shdr->sh_type == SHT_RELA
767 || idx->shdr->sh_type == SHT_REL)
768 /* We won't include relocation sections in section groups in
769 output object files. We adjust the group section size here
770 so that relocatable link will work correctly when
771 relocation sections are in section group in input object
772 files. */
773 shdr->bfd_section->size -= 4;
774 else
776 /* There are some unknown sections in the group. */
777 (*_bfd_error_handler)
778 (_("%B: unknown [%d] section `%s' in group [%s]"),
779 abfd,
780 (unsigned int) idx->shdr->sh_type,
781 bfd_elf_string_from_elf_section (abfd,
782 (elf_elfheader (abfd)
783 ->e_shstrndx),
784 idx->shdr->sh_name),
785 shdr->bfd_section->name);
786 result = FALSE;
789 return result;
792 bfd_boolean
793 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
795 return elf_next_in_group (sec) != NULL;
798 /* Make a BFD section from an ELF section. We store a pointer to the
799 BFD section in the bfd_section field of the header. */
801 bfd_boolean
802 _bfd_elf_make_section_from_shdr (bfd *abfd,
803 Elf_Internal_Shdr *hdr,
804 const char *name,
805 int shindex)
807 asection *newsect;
808 flagword flags;
809 const struct elf_backend_data *bed;
811 if (hdr->bfd_section != NULL)
813 BFD_ASSERT (strcmp (name,
814 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
815 return TRUE;
818 newsect = bfd_make_section_anyway (abfd, name);
819 if (newsect == NULL)
820 return FALSE;
822 hdr->bfd_section = newsect;
823 elf_section_data (newsect)->this_hdr = *hdr;
824 elf_section_data (newsect)->this_idx = shindex;
826 /* Always use the real type/flags. */
827 elf_section_type (newsect) = hdr->sh_type;
828 elf_section_flags (newsect) = hdr->sh_flags;
830 newsect->filepos = hdr->sh_offset;
832 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
833 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
834 || ! bfd_set_section_alignment (abfd, newsect,
835 bfd_log2 (hdr->sh_addralign)))
836 return FALSE;
838 flags = SEC_NO_FLAGS;
839 if (hdr->sh_type != SHT_NOBITS)
840 flags |= SEC_HAS_CONTENTS;
841 if (hdr->sh_type == SHT_GROUP)
842 flags |= SEC_GROUP | SEC_EXCLUDE;
843 if ((hdr->sh_flags & SHF_ALLOC) != 0)
845 flags |= SEC_ALLOC;
846 if (hdr->sh_type != SHT_NOBITS)
847 flags |= SEC_LOAD;
849 if ((hdr->sh_flags & SHF_WRITE) == 0)
850 flags |= SEC_READONLY;
851 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
852 flags |= SEC_CODE;
853 else if ((flags & SEC_LOAD) != 0)
854 flags |= SEC_DATA;
855 if ((hdr->sh_flags & SHF_MERGE) != 0)
857 flags |= SEC_MERGE;
858 newsect->entsize = hdr->sh_entsize;
859 if ((hdr->sh_flags & SHF_STRINGS) != 0)
860 flags |= SEC_STRINGS;
862 if (hdr->sh_flags & SHF_GROUP)
863 if (!setup_group (abfd, hdr, newsect))
864 return FALSE;
865 if ((hdr->sh_flags & SHF_TLS) != 0)
866 flags |= SEC_THREAD_LOCAL;
868 if ((flags & SEC_ALLOC) == 0)
870 /* The debugging sections appear to be recognized only by name,
871 not any sort of flag. Their SEC_ALLOC bits are cleared. */
872 static const struct
874 const char *name;
875 int len;
876 } debug_sections [] =
878 { STRING_COMMA_LEN ("debug") }, /* 'd' */
879 { NULL, 0 }, /* 'e' */
880 { NULL, 0 }, /* 'f' */
881 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
882 { NULL, 0 }, /* 'h' */
883 { NULL, 0 }, /* 'i' */
884 { NULL, 0 }, /* 'j' */
885 { NULL, 0 }, /* 'k' */
886 { STRING_COMMA_LEN ("line") }, /* 'l' */
887 { NULL, 0 }, /* 'm' */
888 { NULL, 0 }, /* 'n' */
889 { NULL, 0 }, /* 'o' */
890 { NULL, 0 }, /* 'p' */
891 { NULL, 0 }, /* 'q' */
892 { NULL, 0 }, /* 'r' */
893 { STRING_COMMA_LEN ("stab") }, /* 's' */
894 { NULL, 0 }, /* 't' */
895 { NULL, 0 }, /* 'u' */
896 { NULL, 0 }, /* 'v' */
897 { NULL, 0 }, /* 'w' */
898 { NULL, 0 }, /* 'x' */
899 { NULL, 0 }, /* 'y' */
900 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
903 if (name [0] == '.')
905 int i = name [1] - 'd';
906 if (i >= 0
907 && i < (int) ARRAY_SIZE (debug_sections)
908 && debug_sections [i].name != NULL
909 && strncmp (&name [1], debug_sections [i].name,
910 debug_sections [i].len) == 0)
911 flags |= SEC_DEBUGGING;
915 /* As a GNU extension, if the name begins with .gnu.linkonce, we
916 only link a single copy of the section. This is used to support
917 g++. g++ will emit each template expansion in its own section.
918 The symbols will be defined as weak, so that multiple definitions
919 are permitted. The GNU linker extension is to actually discard
920 all but one of the sections. */
921 if (CONST_STRNEQ (name, ".gnu.linkonce")
922 && elf_next_in_group (newsect) == NULL)
923 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
925 bed = get_elf_backend_data (abfd);
926 if (bed->elf_backend_section_flags)
927 if (! bed->elf_backend_section_flags (&flags, hdr))
928 return FALSE;
930 if (! bfd_set_section_flags (abfd, newsect, flags))
931 return FALSE;
933 /* We do not parse the PT_NOTE segments as we are interested even in the
934 separate debug info files which may have the segments offsets corrupted.
935 PT_NOTEs from the core files are currently not parsed using BFD. */
936 if (hdr->sh_type == SHT_NOTE)
938 char *contents;
940 contents = bfd_malloc (hdr->sh_size);
941 if (!contents)
942 return FALSE;
944 if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0,
945 hdr->sh_size)
946 || !elf_parse_notes (abfd, contents, hdr->sh_size, -1))
948 free (contents);
949 return FALSE;
952 free (contents);
955 if ((flags & SEC_ALLOC) != 0)
957 Elf_Internal_Phdr *phdr;
958 unsigned int i, nload;
960 /* Some ELF linkers produce binaries with all the program header
961 p_paddr fields zero. If we have such a binary with more than
962 one PT_LOAD header, then leave the section lma equal to vma
963 so that we don't create sections with overlapping lma. */
964 phdr = elf_tdata (abfd)->phdr;
965 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
966 if (phdr->p_paddr != 0)
967 break;
968 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
969 ++nload;
970 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
971 return TRUE;
973 phdr = elf_tdata (abfd)->phdr;
974 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
976 /* This section is part of this segment if its file
977 offset plus size lies within the segment's memory
978 span and, if the section is loaded, the extent of the
979 loaded data lies within the extent of the segment.
981 Note - we used to check the p_paddr field as well, and
982 refuse to set the LMA if it was 0. This is wrong
983 though, as a perfectly valid initialised segment can
984 have a p_paddr of zero. Some architectures, eg ARM,
985 place special significance on the address 0 and
986 executables need to be able to have a segment which
987 covers this address. */
988 if (phdr->p_type == PT_LOAD
989 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
990 && (hdr->sh_offset + hdr->sh_size
991 <= phdr->p_offset + phdr->p_memsz)
992 && ((flags & SEC_LOAD) == 0
993 || (hdr->sh_offset + hdr->sh_size
994 <= phdr->p_offset + phdr->p_filesz)))
996 if ((flags & SEC_LOAD) == 0)
997 newsect->lma = (phdr->p_paddr
998 + hdr->sh_addr - phdr->p_vaddr);
999 else
1000 /* We used to use the same adjustment for SEC_LOAD
1001 sections, but that doesn't work if the segment
1002 is packed with code from multiple VMAs.
1003 Instead we calculate the section LMA based on
1004 the segment LMA. It is assumed that the
1005 segment will contain sections with contiguous
1006 LMAs, even if the VMAs are not. */
1007 newsect->lma = (phdr->p_paddr
1008 + hdr->sh_offset - phdr->p_offset);
1010 /* With contiguous segments, we can't tell from file
1011 offsets whether a section with zero size should
1012 be placed at the end of one segment or the
1013 beginning of the next. Decide based on vaddr. */
1014 if (hdr->sh_addr >= phdr->p_vaddr
1015 && (hdr->sh_addr + hdr->sh_size
1016 <= phdr->p_vaddr + phdr->p_memsz))
1017 break;
1022 return TRUE;
1026 INTERNAL_FUNCTION
1027 bfd_elf_find_section
1029 SYNOPSIS
1030 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
1032 DESCRIPTION
1033 Helper functions for GDB to locate the string tables.
1034 Since BFD hides string tables from callers, GDB needs to use an
1035 internal hook to find them. Sun's .stabstr, in particular,
1036 isn't even pointed to by the .stab section, so ordinary
1037 mechanisms wouldn't work to find it, even if we had some.
1040 struct elf_internal_shdr *
1041 bfd_elf_find_section (bfd *abfd, char *name)
1043 Elf_Internal_Shdr **i_shdrp;
1044 char *shstrtab;
1045 unsigned int max;
1046 unsigned int i;
1048 i_shdrp = elf_elfsections (abfd);
1049 if (i_shdrp != NULL)
1051 shstrtab = bfd_elf_get_str_section (abfd,
1052 elf_elfheader (abfd)->e_shstrndx);
1053 if (shstrtab != NULL)
1055 max = elf_numsections (abfd);
1056 for (i = 1; i < max; i++)
1057 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
1058 return i_shdrp[i];
1061 return 0;
1064 const char *const bfd_elf_section_type_names[] = {
1065 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1066 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1067 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1070 /* ELF relocs are against symbols. If we are producing relocatable
1071 output, and the reloc is against an external symbol, and nothing
1072 has given us any additional addend, the resulting reloc will also
1073 be against the same symbol. In such a case, we don't want to
1074 change anything about the way the reloc is handled, since it will
1075 all be done at final link time. Rather than put special case code
1076 into bfd_perform_relocation, all the reloc types use this howto
1077 function. It just short circuits the reloc if producing
1078 relocatable output against an external symbol. */
1080 bfd_reloc_status_type
1081 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1082 arelent *reloc_entry,
1083 asymbol *symbol,
1084 void *data ATTRIBUTE_UNUSED,
1085 asection *input_section,
1086 bfd *output_bfd,
1087 char **error_message ATTRIBUTE_UNUSED)
1089 if (output_bfd != NULL
1090 && (symbol->flags & BSF_SECTION_SYM) == 0
1091 && (! reloc_entry->howto->partial_inplace
1092 || reloc_entry->addend == 0))
1094 reloc_entry->address += input_section->output_offset;
1095 return bfd_reloc_ok;
1098 return bfd_reloc_continue;
1101 /* Copy the program header and other data from one object module to
1102 another. */
1104 bfd_boolean
1105 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1107 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1108 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1109 return TRUE;
1111 BFD_ASSERT (!elf_flags_init (obfd)
1112 || (elf_elfheader (obfd)->e_flags
1113 == elf_elfheader (ibfd)->e_flags));
1115 elf_gp (obfd) = elf_gp (ibfd);
1116 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1117 elf_flags_init (obfd) = TRUE;
1119 /* Copy object attributes. */
1120 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1122 return TRUE;
1125 static const char *
1126 get_segment_type (unsigned int p_type)
1128 const char *pt;
1129 switch (p_type)
1131 case PT_NULL: pt = "NULL"; break;
1132 case PT_LOAD: pt = "LOAD"; break;
1133 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1134 case PT_INTERP: pt = "INTERP"; break;
1135 case PT_NOTE: pt = "NOTE"; break;
1136 case PT_SHLIB: pt = "SHLIB"; break;
1137 case PT_PHDR: pt = "PHDR"; break;
1138 case PT_TLS: pt = "TLS"; break;
1139 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1140 case PT_GNU_STACK: pt = "STACK"; break;
1141 case PT_GNU_RELRO: pt = "RELRO"; break;
1142 default: pt = NULL; break;
1144 return pt;
1147 /* Print out the program headers. */
1149 bfd_boolean
1150 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1152 FILE *f = farg;
1153 Elf_Internal_Phdr *p;
1154 asection *s;
1155 bfd_byte *dynbuf = NULL;
1157 p = elf_tdata (abfd)->phdr;
1158 if (p != NULL)
1160 unsigned int i, c;
1162 fprintf (f, _("\nProgram Header:\n"));
1163 c = elf_elfheader (abfd)->e_phnum;
1164 for (i = 0; i < c; i++, p++)
1166 const char *pt = get_segment_type (p->p_type);
1167 char buf[20];
1169 if (pt == NULL)
1171 sprintf (buf, "0x%lx", p->p_type);
1172 pt = buf;
1174 fprintf (f, "%8s off 0x", pt);
1175 bfd_fprintf_vma (abfd, f, p->p_offset);
1176 fprintf (f, " vaddr 0x");
1177 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1178 fprintf (f, " paddr 0x");
1179 bfd_fprintf_vma (abfd, f, p->p_paddr);
1180 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1181 fprintf (f, " filesz 0x");
1182 bfd_fprintf_vma (abfd, f, p->p_filesz);
1183 fprintf (f, " memsz 0x");
1184 bfd_fprintf_vma (abfd, f, p->p_memsz);
1185 fprintf (f, " flags %c%c%c",
1186 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1187 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1188 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1189 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1190 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1191 fprintf (f, "\n");
1195 s = bfd_get_section_by_name (abfd, ".dynamic");
1196 if (s != NULL)
1198 unsigned int elfsec;
1199 unsigned long shlink;
1200 bfd_byte *extdyn, *extdynend;
1201 size_t extdynsize;
1202 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1204 fprintf (f, _("\nDynamic Section:\n"));
1206 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1207 goto error_return;
1209 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1210 if (elfsec == SHN_BAD)
1211 goto error_return;
1212 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1214 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1215 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1217 extdyn = dynbuf;
1218 extdynend = extdyn + s->size;
1219 for (; extdyn < extdynend; extdyn += extdynsize)
1221 Elf_Internal_Dyn dyn;
1222 const char *name = "";
1223 char ab[20];
1224 bfd_boolean stringp;
1225 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1227 (*swap_dyn_in) (abfd, extdyn, &dyn);
1229 if (dyn.d_tag == DT_NULL)
1230 break;
1232 stringp = FALSE;
1233 switch (dyn.d_tag)
1235 default:
1236 if (bed->elf_backend_get_target_dtag)
1237 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1239 if (!strcmp (name, ""))
1241 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1242 name = ab;
1244 break;
1246 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1247 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1248 case DT_PLTGOT: name = "PLTGOT"; break;
1249 case DT_HASH: name = "HASH"; break;
1250 case DT_STRTAB: name = "STRTAB"; break;
1251 case DT_SYMTAB: name = "SYMTAB"; break;
1252 case DT_RELA: name = "RELA"; break;
1253 case DT_RELASZ: name = "RELASZ"; break;
1254 case DT_RELAENT: name = "RELAENT"; break;
1255 case DT_STRSZ: name = "STRSZ"; break;
1256 case DT_SYMENT: name = "SYMENT"; break;
1257 case DT_INIT: name = "INIT"; break;
1258 case DT_FINI: name = "FINI"; break;
1259 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1260 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1261 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1262 case DT_REL: name = "REL"; break;
1263 case DT_RELSZ: name = "RELSZ"; break;
1264 case DT_RELENT: name = "RELENT"; break;
1265 case DT_PLTREL: name = "PLTREL"; break;
1266 case DT_DEBUG: name = "DEBUG"; break;
1267 case DT_TEXTREL: name = "TEXTREL"; break;
1268 case DT_JMPREL: name = "JMPREL"; break;
1269 case DT_BIND_NOW: name = "BIND_NOW"; break;
1270 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1271 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1272 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1273 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1274 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1275 case DT_FLAGS: name = "FLAGS"; break;
1276 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1277 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1278 case DT_CHECKSUM: name = "CHECKSUM"; break;
1279 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1280 case DT_MOVEENT: name = "MOVEENT"; break;
1281 case DT_MOVESZ: name = "MOVESZ"; break;
1282 case DT_FEATURE: name = "FEATURE"; break;
1283 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1284 case DT_SYMINSZ: name = "SYMINSZ"; break;
1285 case DT_SYMINENT: name = "SYMINENT"; break;
1286 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1287 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1288 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1289 case DT_PLTPAD: name = "PLTPAD"; break;
1290 case DT_MOVETAB: name = "MOVETAB"; break;
1291 case DT_SYMINFO: name = "SYMINFO"; break;
1292 case DT_RELACOUNT: name = "RELACOUNT"; break;
1293 case DT_RELCOUNT: name = "RELCOUNT"; break;
1294 case DT_FLAGS_1: name = "FLAGS_1"; break;
1295 case DT_VERSYM: name = "VERSYM"; break;
1296 case DT_VERDEF: name = "VERDEF"; break;
1297 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1298 case DT_VERNEED: name = "VERNEED"; break;
1299 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1300 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1301 case DT_USED: name = "USED"; break;
1302 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1303 case DT_GNU_HASH: name = "GNU_HASH"; break;
1306 fprintf (f, " %-20s ", name);
1307 if (! stringp)
1309 fprintf (f, "0x");
1310 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1312 else
1314 const char *string;
1315 unsigned int tagv = dyn.d_un.d_val;
1317 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1318 if (string == NULL)
1319 goto error_return;
1320 fprintf (f, "%s", string);
1322 fprintf (f, "\n");
1325 free (dynbuf);
1326 dynbuf = NULL;
1329 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1330 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1332 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1333 return FALSE;
1336 if (elf_dynverdef (abfd) != 0)
1338 Elf_Internal_Verdef *t;
1340 fprintf (f, _("\nVersion definitions:\n"));
1341 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1343 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1344 t->vd_flags, t->vd_hash,
1345 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1346 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1348 Elf_Internal_Verdaux *a;
1350 fprintf (f, "\t");
1351 for (a = t->vd_auxptr->vda_nextptr;
1352 a != NULL;
1353 a = a->vda_nextptr)
1354 fprintf (f, "%s ",
1355 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1356 fprintf (f, "\n");
1361 if (elf_dynverref (abfd) != 0)
1363 Elf_Internal_Verneed *t;
1365 fprintf (f, _("\nVersion References:\n"));
1366 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1368 Elf_Internal_Vernaux *a;
1370 fprintf (f, _(" required from %s:\n"),
1371 t->vn_filename ? t->vn_filename : "<corrupt>");
1372 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1373 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1374 a->vna_flags, a->vna_other,
1375 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1379 return TRUE;
1381 error_return:
1382 if (dynbuf != NULL)
1383 free (dynbuf);
1384 return FALSE;
1387 /* Display ELF-specific fields of a symbol. */
1389 void
1390 bfd_elf_print_symbol (bfd *abfd,
1391 void *filep,
1392 asymbol *symbol,
1393 bfd_print_symbol_type how)
1395 FILE *file = filep;
1396 switch (how)
1398 case bfd_print_symbol_name:
1399 fprintf (file, "%s", symbol->name);
1400 break;
1401 case bfd_print_symbol_more:
1402 fprintf (file, "elf ");
1403 bfd_fprintf_vma (abfd, file, symbol->value);
1404 fprintf (file, " %lx", (long) symbol->flags);
1405 break;
1406 case bfd_print_symbol_all:
1408 const char *section_name;
1409 const char *name = NULL;
1410 const struct elf_backend_data *bed;
1411 unsigned char st_other;
1412 bfd_vma val;
1414 section_name = symbol->section ? symbol->section->name : "(*none*)";
1416 bed = get_elf_backend_data (abfd);
1417 if (bed->elf_backend_print_symbol_all)
1418 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1420 if (name == NULL)
1422 name = symbol->name;
1423 bfd_print_symbol_vandf (abfd, file, symbol);
1426 fprintf (file, " %s\t", section_name);
1427 /* Print the "other" value for a symbol. For common symbols,
1428 we've already printed the size; now print the alignment.
1429 For other symbols, we have no specified alignment, and
1430 we've printed the address; now print the size. */
1431 if (symbol->section && bfd_is_com_section (symbol->section))
1432 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1433 else
1434 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1435 bfd_fprintf_vma (abfd, file, val);
1437 /* If we have version information, print it. */
1438 if (elf_tdata (abfd)->dynversym_section != 0
1439 && (elf_tdata (abfd)->dynverdef_section != 0
1440 || elf_tdata (abfd)->dynverref_section != 0))
1442 unsigned int vernum;
1443 const char *version_string;
1445 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1447 if (vernum == 0)
1448 version_string = "";
1449 else if (vernum == 1)
1450 version_string = "Base";
1451 else if (vernum <= elf_tdata (abfd)->cverdefs)
1452 version_string =
1453 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1454 else
1456 Elf_Internal_Verneed *t;
1458 version_string = "";
1459 for (t = elf_tdata (abfd)->verref;
1460 t != NULL;
1461 t = t->vn_nextref)
1463 Elf_Internal_Vernaux *a;
1465 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1467 if (a->vna_other == vernum)
1469 version_string = a->vna_nodename;
1470 break;
1476 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1477 fprintf (file, " %-11s", version_string);
1478 else
1480 int i;
1482 fprintf (file, " (%s)", version_string);
1483 for (i = 10 - strlen (version_string); i > 0; --i)
1484 putc (' ', file);
1488 /* If the st_other field is not zero, print it. */
1489 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1491 switch (st_other)
1493 case 0: break;
1494 case STV_INTERNAL: fprintf (file, " .internal"); break;
1495 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1496 case STV_PROTECTED: fprintf (file, " .protected"); break;
1497 default:
1498 /* Some other non-defined flags are also present, so print
1499 everything hex. */
1500 fprintf (file, " 0x%02x", (unsigned int) st_other);
1503 fprintf (file, " %s", name);
1505 break;
1509 /* Allocate an ELF string table--force the first byte to be zero. */
1511 struct bfd_strtab_hash *
1512 _bfd_elf_stringtab_init (void)
1514 struct bfd_strtab_hash *ret;
1516 ret = _bfd_stringtab_init ();
1517 if (ret != NULL)
1519 bfd_size_type loc;
1521 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1522 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1523 if (loc == (bfd_size_type) -1)
1525 _bfd_stringtab_free (ret);
1526 ret = NULL;
1529 return ret;
1532 /* ELF .o/exec file reading */
1534 /* Create a new bfd section from an ELF section header. */
1536 bfd_boolean
1537 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1539 Elf_Internal_Shdr *hdr;
1540 Elf_Internal_Ehdr *ehdr;
1541 const struct elf_backend_data *bed;
1542 const char *name;
1544 if (shindex >= elf_numsections (abfd))
1545 return FALSE;
1547 hdr = elf_elfsections (abfd)[shindex];
1548 ehdr = elf_elfheader (abfd);
1549 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1550 hdr->sh_name);
1551 if (name == NULL)
1552 return FALSE;
1554 bed = get_elf_backend_data (abfd);
1555 switch (hdr->sh_type)
1557 case SHT_NULL:
1558 /* Inactive section. Throw it away. */
1559 return TRUE;
1561 case SHT_PROGBITS: /* Normal section with contents. */
1562 case SHT_NOBITS: /* .bss section. */
1563 case SHT_HASH: /* .hash section. */
1564 case SHT_NOTE: /* .note section. */
1565 case SHT_INIT_ARRAY: /* .init_array section. */
1566 case SHT_FINI_ARRAY: /* .fini_array section. */
1567 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1568 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
1569 case SHT_GNU_HASH: /* .gnu.hash section. */
1570 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1572 case SHT_DYNAMIC: /* Dynamic linking information. */
1573 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1574 return FALSE;
1575 if (hdr->sh_link > elf_numsections (abfd)
1576 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1577 return FALSE;
1578 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1580 Elf_Internal_Shdr *dynsymhdr;
1582 /* The shared libraries distributed with hpux11 have a bogus
1583 sh_link field for the ".dynamic" section. Find the
1584 string table for the ".dynsym" section instead. */
1585 if (elf_dynsymtab (abfd) != 0)
1587 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1588 hdr->sh_link = dynsymhdr->sh_link;
1590 else
1592 unsigned int i, num_sec;
1594 num_sec = elf_numsections (abfd);
1595 for (i = 1; i < num_sec; i++)
1597 dynsymhdr = elf_elfsections (abfd)[i];
1598 if (dynsymhdr->sh_type == SHT_DYNSYM)
1600 hdr->sh_link = dynsymhdr->sh_link;
1601 break;
1606 break;
1608 case SHT_SYMTAB: /* A symbol table */
1609 if (elf_onesymtab (abfd) == shindex)
1610 return TRUE;
1612 if (hdr->sh_entsize != bed->s->sizeof_sym)
1613 return FALSE;
1614 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1615 elf_onesymtab (abfd) = shindex;
1616 elf_tdata (abfd)->symtab_hdr = *hdr;
1617 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1618 abfd->flags |= HAS_SYMS;
1620 /* Sometimes a shared object will map in the symbol table. If
1621 SHF_ALLOC is set, and this is a shared object, then we also
1622 treat this section as a BFD section. We can not base the
1623 decision purely on SHF_ALLOC, because that flag is sometimes
1624 set in a relocatable object file, which would confuse the
1625 linker. */
1626 if ((hdr->sh_flags & SHF_ALLOC) != 0
1627 && (abfd->flags & DYNAMIC) != 0
1628 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1629 shindex))
1630 return FALSE;
1632 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1633 can't read symbols without that section loaded as well. It
1634 is most likely specified by the next section header. */
1635 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1637 unsigned int i, num_sec;
1639 num_sec = elf_numsections (abfd);
1640 for (i = shindex + 1; i < num_sec; i++)
1642 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1643 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1644 && hdr2->sh_link == shindex)
1645 break;
1647 if (i == num_sec)
1648 for (i = 1; i < shindex; i++)
1650 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1651 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1652 && hdr2->sh_link == shindex)
1653 break;
1655 if (i != shindex)
1656 return bfd_section_from_shdr (abfd, i);
1658 return TRUE;
1660 case SHT_DYNSYM: /* A dynamic symbol table */
1661 if (elf_dynsymtab (abfd) == shindex)
1662 return TRUE;
1664 if (hdr->sh_entsize != bed->s->sizeof_sym)
1665 return FALSE;
1666 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1667 elf_dynsymtab (abfd) = shindex;
1668 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1669 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1670 abfd->flags |= HAS_SYMS;
1672 /* Besides being a symbol table, we also treat this as a regular
1673 section, so that objcopy can handle it. */
1674 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1676 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1677 if (elf_symtab_shndx (abfd) == shindex)
1678 return TRUE;
1680 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1681 elf_symtab_shndx (abfd) = shindex;
1682 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1683 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1684 return TRUE;
1686 case SHT_STRTAB: /* A string table */
1687 if (hdr->bfd_section != NULL)
1688 return TRUE;
1689 if (ehdr->e_shstrndx == shindex)
1691 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1692 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1693 return TRUE;
1695 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1697 symtab_strtab:
1698 elf_tdata (abfd)->strtab_hdr = *hdr;
1699 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1700 return TRUE;
1702 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1704 dynsymtab_strtab:
1705 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1706 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1707 elf_elfsections (abfd)[shindex] = hdr;
1708 /* We also treat this as a regular section, so that objcopy
1709 can handle it. */
1710 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1711 shindex);
1714 /* If the string table isn't one of the above, then treat it as a
1715 regular section. We need to scan all the headers to be sure,
1716 just in case this strtab section appeared before the above. */
1717 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1719 unsigned int i, num_sec;
1721 num_sec = elf_numsections (abfd);
1722 for (i = 1; i < num_sec; i++)
1724 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1725 if (hdr2->sh_link == shindex)
1727 /* Prevent endless recursion on broken objects. */
1728 if (i == shindex)
1729 return FALSE;
1730 if (! bfd_section_from_shdr (abfd, i))
1731 return FALSE;
1732 if (elf_onesymtab (abfd) == i)
1733 goto symtab_strtab;
1734 if (elf_dynsymtab (abfd) == i)
1735 goto dynsymtab_strtab;
1739 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1741 case SHT_REL:
1742 case SHT_RELA:
1743 /* *These* do a lot of work -- but build no sections! */
1745 asection *target_sect;
1746 Elf_Internal_Shdr *hdr2;
1747 unsigned int num_sec = elf_numsections (abfd);
1749 if (hdr->sh_entsize
1750 != (bfd_size_type) (hdr->sh_type == SHT_REL
1751 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1752 return FALSE;
1754 /* Check for a bogus link to avoid crashing. */
1755 if (hdr->sh_link >= num_sec)
1757 ((*_bfd_error_handler)
1758 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1759 abfd, hdr->sh_link, name, shindex));
1760 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1761 shindex);
1764 /* For some incomprehensible reason Oracle distributes
1765 libraries for Solaris in which some of the objects have
1766 bogus sh_link fields. It would be nice if we could just
1767 reject them, but, unfortunately, some people need to use
1768 them. We scan through the section headers; if we find only
1769 one suitable symbol table, we clobber the sh_link to point
1770 to it. I hope this doesn't break anything. */
1771 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1772 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1774 unsigned int scan;
1775 int found;
1777 found = 0;
1778 for (scan = 1; scan < num_sec; scan++)
1780 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1781 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1783 if (found != 0)
1785 found = 0;
1786 break;
1788 found = scan;
1791 if (found != 0)
1792 hdr->sh_link = found;
1795 /* Get the symbol table. */
1796 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1797 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1798 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1799 return FALSE;
1801 /* If this reloc section does not use the main symbol table we
1802 don't treat it as a reloc section. BFD can't adequately
1803 represent such a section, so at least for now, we don't
1804 try. We just present it as a normal section. We also
1805 can't use it as a reloc section if it points to the null
1806 section, an invalid section, or another reloc section. */
1807 if (hdr->sh_link != elf_onesymtab (abfd)
1808 || hdr->sh_info == SHN_UNDEF
1809 || hdr->sh_info >= num_sec
1810 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1811 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1812 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1813 shindex);
1815 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1816 return FALSE;
1817 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1818 if (target_sect == NULL)
1819 return FALSE;
1821 if ((target_sect->flags & SEC_RELOC) == 0
1822 || target_sect->reloc_count == 0)
1823 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1824 else
1826 bfd_size_type amt;
1827 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1828 amt = sizeof (*hdr2);
1829 hdr2 = bfd_alloc (abfd, amt);
1830 if (hdr2 == NULL)
1831 return FALSE;
1832 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1834 *hdr2 = *hdr;
1835 elf_elfsections (abfd)[shindex] = hdr2;
1836 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1837 target_sect->flags |= SEC_RELOC;
1838 target_sect->relocation = NULL;
1839 target_sect->rel_filepos = hdr->sh_offset;
1840 /* In the section to which the relocations apply, mark whether
1841 its relocations are of the REL or RELA variety. */
1842 if (hdr->sh_size != 0)
1843 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1844 abfd->flags |= HAS_RELOC;
1845 return TRUE;
1848 case SHT_GNU_verdef:
1849 elf_dynverdef (abfd) = shindex;
1850 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1851 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1853 case SHT_GNU_versym:
1854 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1855 return FALSE;
1856 elf_dynversym (abfd) = shindex;
1857 elf_tdata (abfd)->dynversym_hdr = *hdr;
1858 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1860 case SHT_GNU_verneed:
1861 elf_dynverref (abfd) = shindex;
1862 elf_tdata (abfd)->dynverref_hdr = *hdr;
1863 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1865 case SHT_SHLIB:
1866 return TRUE;
1868 case SHT_GROUP:
1869 /* We need a BFD section for objcopy and relocatable linking,
1870 and it's handy to have the signature available as the section
1871 name. */
1872 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1873 return FALSE;
1874 name = group_signature (abfd, hdr);
1875 if (name == NULL)
1876 return FALSE;
1877 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1878 return FALSE;
1879 if (hdr->contents != NULL)
1881 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1882 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1883 asection *s;
1885 if (idx->flags & GRP_COMDAT)
1886 hdr->bfd_section->flags
1887 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1889 /* We try to keep the same section order as it comes in. */
1890 idx += n_elt;
1891 while (--n_elt != 0)
1893 --idx;
1895 if (idx->shdr != NULL
1896 && (s = idx->shdr->bfd_section) != NULL
1897 && elf_next_in_group (s) != NULL)
1899 elf_next_in_group (hdr->bfd_section) = s;
1900 break;
1904 break;
1906 default:
1907 /* Possibly an attributes section. */
1908 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1909 || hdr->sh_type == bed->obj_attrs_section_type)
1911 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1912 return FALSE;
1913 _bfd_elf_parse_attributes (abfd, hdr);
1914 return TRUE;
1917 /* Check for any processor-specific section types. */
1918 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1919 return TRUE;
1921 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1923 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1924 /* FIXME: How to properly handle allocated section reserved
1925 for applications? */
1926 (*_bfd_error_handler)
1927 (_("%B: don't know how to handle allocated, application "
1928 "specific section `%s' [0x%8x]"),
1929 abfd, name, hdr->sh_type);
1930 else
1931 /* Allow sections reserved for applications. */
1932 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1933 shindex);
1935 else if (hdr->sh_type >= SHT_LOPROC
1936 && hdr->sh_type <= SHT_HIPROC)
1937 /* FIXME: We should handle this section. */
1938 (*_bfd_error_handler)
1939 (_("%B: don't know how to handle processor specific section "
1940 "`%s' [0x%8x]"),
1941 abfd, name, hdr->sh_type);
1942 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1944 /* Unrecognised OS-specific sections. */
1945 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1946 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1947 required to correctly process the section and the file should
1948 be rejected with an error message. */
1949 (*_bfd_error_handler)
1950 (_("%B: don't know how to handle OS specific section "
1951 "`%s' [0x%8x]"),
1952 abfd, name, hdr->sh_type);
1953 else
1954 /* Otherwise it should be processed. */
1955 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1957 else
1958 /* FIXME: We should handle this section. */
1959 (*_bfd_error_handler)
1960 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1961 abfd, name, hdr->sh_type);
1963 return FALSE;
1966 return TRUE;
1969 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1970 Return SEC for sections that have no elf section, and NULL on error. */
1972 asection *
1973 bfd_section_from_r_symndx (bfd *abfd,
1974 struct sym_sec_cache *cache,
1975 asection *sec,
1976 unsigned long r_symndx)
1978 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1979 asection *s;
1981 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1983 Elf_Internal_Shdr *symtab_hdr;
1984 unsigned char esym[sizeof (Elf64_External_Sym)];
1985 Elf_External_Sym_Shndx eshndx;
1986 Elf_Internal_Sym isym;
1988 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1989 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1990 &isym, esym, &eshndx) == NULL)
1991 return NULL;
1993 if (cache->abfd != abfd)
1995 memset (cache->indx, -1, sizeof (cache->indx));
1996 cache->abfd = abfd;
1998 cache->indx[ent] = r_symndx;
1999 cache->shndx[ent] = isym.st_shndx;
2002 s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
2003 if (s != NULL)
2004 return s;
2006 return sec;
2009 /* Given an ELF section number, retrieve the corresponding BFD
2010 section. */
2012 asection *
2013 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2015 if (index >= elf_numsections (abfd))
2016 return NULL;
2017 return elf_elfsections (abfd)[index]->bfd_section;
2020 static const struct bfd_elf_special_section special_sections_b[] =
2022 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2023 { NULL, 0, 0, 0, 0 }
2026 static const struct bfd_elf_special_section special_sections_c[] =
2028 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2029 { NULL, 0, 0, 0, 0 }
2032 static const struct bfd_elf_special_section special_sections_d[] =
2034 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2035 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2036 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2037 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2038 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2039 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2040 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2041 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2042 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2043 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2044 { NULL, 0, 0, 0, 0 }
2047 static const struct bfd_elf_special_section special_sections_f[] =
2049 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2050 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2051 { NULL, 0, 0, 0, 0 }
2054 static const struct bfd_elf_special_section special_sections_g[] =
2056 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2057 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2058 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2059 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2060 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2061 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2062 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2063 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2064 { NULL, 0, 0, 0, 0 }
2067 static const struct bfd_elf_special_section special_sections_h[] =
2069 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2070 { NULL, 0, 0, 0, 0 }
2073 static const struct bfd_elf_special_section special_sections_i[] =
2075 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2076 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2077 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2078 { NULL, 0, 0, 0, 0 }
2081 static const struct bfd_elf_special_section special_sections_l[] =
2083 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2084 { NULL, 0, 0, 0, 0 }
2087 static const struct bfd_elf_special_section special_sections_n[] =
2089 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2090 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2091 { NULL, 0, 0, 0, 0 }
2094 static const struct bfd_elf_special_section special_sections_p[] =
2096 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2097 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2098 { NULL, 0, 0, 0, 0 }
2101 static const struct bfd_elf_special_section special_sections_r[] =
2103 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2104 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2105 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2106 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2107 { NULL, 0, 0, 0, 0 }
2110 static const struct bfd_elf_special_section special_sections_s[] =
2112 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2113 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2114 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2115 /* See struct bfd_elf_special_section declaration for the semantics of
2116 this special case where .prefix_length != strlen (.prefix). */
2117 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2118 { NULL, 0, 0, 0, 0 }
2121 static const struct bfd_elf_special_section special_sections_t[] =
2123 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2124 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2125 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2126 { NULL, 0, 0, 0, 0 }
2129 static const struct bfd_elf_special_section special_sections_z[] =
2131 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2132 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2133 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2134 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2135 { NULL, 0, 0, 0, 0 }
2138 static const struct bfd_elf_special_section *special_sections[] =
2140 special_sections_b, /* 'b' */
2141 special_sections_c, /* 'c' */
2142 special_sections_d, /* 'd' */
2143 NULL, /* 'e' */
2144 special_sections_f, /* 'f' */
2145 special_sections_g, /* 'g' */
2146 special_sections_h, /* 'h' */
2147 special_sections_i, /* 'i' */
2148 NULL, /* 'j' */
2149 NULL, /* 'k' */
2150 special_sections_l, /* 'l' */
2151 NULL, /* 'm' */
2152 special_sections_n, /* 'n' */
2153 NULL, /* 'o' */
2154 special_sections_p, /* 'p' */
2155 NULL, /* 'q' */
2156 special_sections_r, /* 'r' */
2157 special_sections_s, /* 's' */
2158 special_sections_t, /* 't' */
2159 NULL, /* 'u' */
2160 NULL, /* 'v' */
2161 NULL, /* 'w' */
2162 NULL, /* 'x' */
2163 NULL, /* 'y' */
2164 special_sections_z /* 'z' */
2167 const struct bfd_elf_special_section *
2168 _bfd_elf_get_special_section (const char *name,
2169 const struct bfd_elf_special_section *spec,
2170 unsigned int rela)
2172 int i;
2173 int len;
2175 len = strlen (name);
2177 for (i = 0; spec[i].prefix != NULL; i++)
2179 int suffix_len;
2180 int prefix_len = spec[i].prefix_length;
2182 if (len < prefix_len)
2183 continue;
2184 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2185 continue;
2187 suffix_len = spec[i].suffix_length;
2188 if (suffix_len <= 0)
2190 if (name[prefix_len] != 0)
2192 if (suffix_len == 0)
2193 continue;
2194 if (name[prefix_len] != '.'
2195 && (suffix_len == -2
2196 || (rela && spec[i].type == SHT_REL)))
2197 continue;
2200 else
2202 if (len < prefix_len + suffix_len)
2203 continue;
2204 if (memcmp (name + len - suffix_len,
2205 spec[i].prefix + prefix_len,
2206 suffix_len) != 0)
2207 continue;
2209 return &spec[i];
2212 return NULL;
2215 const struct bfd_elf_special_section *
2216 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2218 int i;
2219 const struct bfd_elf_special_section *spec;
2220 const struct elf_backend_data *bed;
2222 /* See if this is one of the special sections. */
2223 if (sec->name == NULL)
2224 return NULL;
2226 bed = get_elf_backend_data (abfd);
2227 spec = bed->special_sections;
2228 if (spec)
2230 spec = _bfd_elf_get_special_section (sec->name,
2231 bed->special_sections,
2232 sec->use_rela_p);
2233 if (spec != NULL)
2234 return spec;
2237 if (sec->name[0] != '.')
2238 return NULL;
2240 i = sec->name[1] - 'b';
2241 if (i < 0 || i > 'z' - 'b')
2242 return NULL;
2244 spec = special_sections[i];
2246 if (spec == NULL)
2247 return NULL;
2249 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2252 bfd_boolean
2253 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2255 struct bfd_elf_section_data *sdata;
2256 const struct elf_backend_data *bed;
2257 const struct bfd_elf_special_section *ssect;
2259 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2260 if (sdata == NULL)
2262 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2263 if (sdata == NULL)
2264 return FALSE;
2265 sec->used_by_bfd = sdata;
2268 /* Indicate whether or not this section should use RELA relocations. */
2269 bed = get_elf_backend_data (abfd);
2270 sec->use_rela_p = bed->default_use_rela_p;
2272 /* When we read a file, we don't need to set ELF section type and
2273 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2274 anyway. We will set ELF section type and flags for all linker
2275 created sections. If user specifies BFD section flags, we will
2276 set ELF section type and flags based on BFD section flags in
2277 elf_fake_sections. */
2278 if ((!sec->flags && abfd->direction != read_direction)
2279 || (sec->flags & SEC_LINKER_CREATED) != 0)
2281 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2282 if (ssect != NULL)
2284 elf_section_type (sec) = ssect->type;
2285 elf_section_flags (sec) = ssect->attr;
2289 return _bfd_generic_new_section_hook (abfd, sec);
2292 /* Create a new bfd section from an ELF program header.
2294 Since program segments have no names, we generate a synthetic name
2295 of the form segment<NUM>, where NUM is generally the index in the
2296 program header table. For segments that are split (see below) we
2297 generate the names segment<NUM>a and segment<NUM>b.
2299 Note that some program segments may have a file size that is different than
2300 (less than) the memory size. All this means is that at execution the
2301 system must allocate the amount of memory specified by the memory size,
2302 but only initialize it with the first "file size" bytes read from the
2303 file. This would occur for example, with program segments consisting
2304 of combined data+bss.
2306 To handle the above situation, this routine generates TWO bfd sections
2307 for the single program segment. The first has the length specified by
2308 the file size of the segment, and the second has the length specified
2309 by the difference between the two sizes. In effect, the segment is split
2310 into its initialized and uninitialized parts.
2314 bfd_boolean
2315 _bfd_elf_make_section_from_phdr (bfd *abfd,
2316 Elf_Internal_Phdr *hdr,
2317 int index,
2318 const char *typename)
2320 asection *newsect;
2321 char *name;
2322 char namebuf[64];
2323 size_t len;
2324 int split;
2326 split = ((hdr->p_memsz > 0)
2327 && (hdr->p_filesz > 0)
2328 && (hdr->p_memsz > hdr->p_filesz));
2330 if (hdr->p_filesz > 0)
2332 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2333 len = strlen (namebuf) + 1;
2334 name = bfd_alloc (abfd, len);
2335 if (!name)
2336 return FALSE;
2337 memcpy (name, namebuf, len);
2338 newsect = bfd_make_section (abfd, name);
2339 if (newsect == NULL)
2340 return FALSE;
2341 newsect->vma = hdr->p_vaddr;
2342 newsect->lma = hdr->p_paddr;
2343 newsect->size = hdr->p_filesz;
2344 newsect->filepos = hdr->p_offset;
2345 newsect->flags |= SEC_HAS_CONTENTS;
2346 newsect->alignment_power = bfd_log2 (hdr->p_align);
2347 if (hdr->p_type == PT_LOAD)
2349 newsect->flags |= SEC_ALLOC;
2350 newsect->flags |= SEC_LOAD;
2351 if (hdr->p_flags & PF_X)
2353 /* FIXME: all we known is that it has execute PERMISSION,
2354 may be data. */
2355 newsect->flags |= SEC_CODE;
2358 if (!(hdr->p_flags & PF_W))
2360 newsect->flags |= SEC_READONLY;
2364 if (hdr->p_memsz > hdr->p_filesz)
2366 bfd_vma align;
2368 sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : "");
2369 len = strlen (namebuf) + 1;
2370 name = bfd_alloc (abfd, len);
2371 if (!name)
2372 return FALSE;
2373 memcpy (name, namebuf, len);
2374 newsect = bfd_make_section (abfd, name);
2375 if (newsect == NULL)
2376 return FALSE;
2377 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2378 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2379 newsect->size = hdr->p_memsz - hdr->p_filesz;
2380 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2381 align = newsect->vma & -newsect->vma;
2382 if (align == 0 || align > hdr->p_align)
2383 align = hdr->p_align;
2384 newsect->alignment_power = bfd_log2 (align);
2385 if (hdr->p_type == PT_LOAD)
2387 /* Hack for gdb. Segments that have not been modified do
2388 not have their contents written to a core file, on the
2389 assumption that a debugger can find the contents in the
2390 executable. We flag this case by setting the fake
2391 section size to zero. Note that "real" bss sections will
2392 always have their contents dumped to the core file. */
2393 if (bfd_get_format (abfd) == bfd_core)
2394 newsect->size = 0;
2395 newsect->flags |= SEC_ALLOC;
2396 if (hdr->p_flags & PF_X)
2397 newsect->flags |= SEC_CODE;
2399 if (!(hdr->p_flags & PF_W))
2400 newsect->flags |= SEC_READONLY;
2403 return TRUE;
2406 bfd_boolean
2407 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2409 const struct elf_backend_data *bed;
2411 switch (hdr->p_type)
2413 case PT_NULL:
2414 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2416 case PT_LOAD:
2417 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2419 case PT_DYNAMIC:
2420 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2422 case PT_INTERP:
2423 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2425 case PT_NOTE:
2426 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2427 return FALSE;
2428 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2429 return FALSE;
2430 return TRUE;
2432 case PT_SHLIB:
2433 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2435 case PT_PHDR:
2436 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2438 case PT_GNU_EH_FRAME:
2439 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2440 "eh_frame_hdr");
2442 case PT_GNU_STACK:
2443 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2445 case PT_GNU_RELRO:
2446 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2448 default:
2449 /* Check for any processor-specific program segment types. */
2450 bed = get_elf_backend_data (abfd);
2451 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2455 /* Initialize REL_HDR, the section-header for new section, containing
2456 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2457 relocations; otherwise, we use REL relocations. */
2459 bfd_boolean
2460 _bfd_elf_init_reloc_shdr (bfd *abfd,
2461 Elf_Internal_Shdr *rel_hdr,
2462 asection *asect,
2463 bfd_boolean use_rela_p)
2465 char *name;
2466 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2467 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2469 name = bfd_alloc (abfd, amt);
2470 if (name == NULL)
2471 return FALSE;
2472 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2473 rel_hdr->sh_name =
2474 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2475 FALSE);
2476 if (rel_hdr->sh_name == (unsigned int) -1)
2477 return FALSE;
2478 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2479 rel_hdr->sh_entsize = (use_rela_p
2480 ? bed->s->sizeof_rela
2481 : bed->s->sizeof_rel);
2482 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2483 rel_hdr->sh_flags = 0;
2484 rel_hdr->sh_addr = 0;
2485 rel_hdr->sh_size = 0;
2486 rel_hdr->sh_offset = 0;
2488 return TRUE;
2491 /* Set up an ELF internal section header for a section. */
2493 static void
2494 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2496 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2497 bfd_boolean *failedptr = failedptrarg;
2498 Elf_Internal_Shdr *this_hdr;
2499 unsigned int sh_type;
2501 if (*failedptr)
2503 /* We already failed; just get out of the bfd_map_over_sections
2504 loop. */
2505 return;
2508 this_hdr = &elf_section_data (asect)->this_hdr;
2510 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2511 asect->name, FALSE);
2512 if (this_hdr->sh_name == (unsigned int) -1)
2514 *failedptr = TRUE;
2515 return;
2518 /* Don't clear sh_flags. Assembler may set additional bits. */
2520 if ((asect->flags & SEC_ALLOC) != 0
2521 || asect->user_set_vma)
2522 this_hdr->sh_addr = asect->vma;
2523 else
2524 this_hdr->sh_addr = 0;
2526 this_hdr->sh_offset = 0;
2527 this_hdr->sh_size = asect->size;
2528 this_hdr->sh_link = 0;
2529 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2530 /* The sh_entsize and sh_info fields may have been set already by
2531 copy_private_section_data. */
2533 this_hdr->bfd_section = asect;
2534 this_hdr->contents = NULL;
2536 /* If the section type is unspecified, we set it based on
2537 asect->flags. */
2538 if ((asect->flags & SEC_GROUP) != 0)
2539 sh_type = SHT_GROUP;
2540 else if ((asect->flags & SEC_ALLOC) != 0
2541 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2542 || (asect->flags & SEC_NEVER_LOAD) != 0))
2543 sh_type = SHT_NOBITS;
2544 else
2545 sh_type = SHT_PROGBITS;
2547 if (this_hdr->sh_type == SHT_NULL)
2548 this_hdr->sh_type = sh_type;
2549 else if (this_hdr->sh_type == SHT_NOBITS
2550 && sh_type == SHT_PROGBITS
2551 && (asect->flags & SEC_ALLOC) != 0)
2553 /* Warn if we are changing a NOBITS section to PROGBITS, but
2554 allow the link to proceed. This can happen when users link
2555 non-bss input sections to bss output sections, or emit data
2556 to a bss output section via a linker script. */
2557 (*_bfd_error_handler)
2558 (_("warning: section `%A' type changed to PROGBITS"), asect);
2559 this_hdr->sh_type = sh_type;
2562 switch (this_hdr->sh_type)
2564 default:
2565 break;
2567 case SHT_STRTAB:
2568 case SHT_INIT_ARRAY:
2569 case SHT_FINI_ARRAY:
2570 case SHT_PREINIT_ARRAY:
2571 case SHT_NOTE:
2572 case SHT_NOBITS:
2573 case SHT_PROGBITS:
2574 break;
2576 case SHT_HASH:
2577 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2578 break;
2580 case SHT_DYNSYM:
2581 this_hdr->sh_entsize = bed->s->sizeof_sym;
2582 break;
2584 case SHT_DYNAMIC:
2585 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2586 break;
2588 case SHT_RELA:
2589 if (get_elf_backend_data (abfd)->may_use_rela_p)
2590 this_hdr->sh_entsize = bed->s->sizeof_rela;
2591 break;
2593 case SHT_REL:
2594 if (get_elf_backend_data (abfd)->may_use_rel_p)
2595 this_hdr->sh_entsize = bed->s->sizeof_rel;
2596 break;
2598 case SHT_GNU_versym:
2599 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2600 break;
2602 case SHT_GNU_verdef:
2603 this_hdr->sh_entsize = 0;
2604 /* objcopy or strip will copy over sh_info, but may not set
2605 cverdefs. The linker will set cverdefs, but sh_info will be
2606 zero. */
2607 if (this_hdr->sh_info == 0)
2608 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2609 else
2610 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2611 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2612 break;
2614 case SHT_GNU_verneed:
2615 this_hdr->sh_entsize = 0;
2616 /* objcopy or strip will copy over sh_info, but may not set
2617 cverrefs. The linker will set cverrefs, but sh_info will be
2618 zero. */
2619 if (this_hdr->sh_info == 0)
2620 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2621 else
2622 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2623 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2624 break;
2626 case SHT_GROUP:
2627 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2628 break;
2630 case SHT_GNU_HASH:
2631 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2632 break;
2635 if ((asect->flags & SEC_ALLOC) != 0)
2636 this_hdr->sh_flags |= SHF_ALLOC;
2637 if ((asect->flags & SEC_READONLY) == 0)
2638 this_hdr->sh_flags |= SHF_WRITE;
2639 if ((asect->flags & SEC_CODE) != 0)
2640 this_hdr->sh_flags |= SHF_EXECINSTR;
2641 if ((asect->flags & SEC_MERGE) != 0)
2643 this_hdr->sh_flags |= SHF_MERGE;
2644 this_hdr->sh_entsize = asect->entsize;
2645 if ((asect->flags & SEC_STRINGS) != 0)
2646 this_hdr->sh_flags |= SHF_STRINGS;
2648 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2649 this_hdr->sh_flags |= SHF_GROUP;
2650 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2652 this_hdr->sh_flags |= SHF_TLS;
2653 if (asect->size == 0
2654 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2656 struct bfd_link_order *o = asect->map_tail.link_order;
2658 this_hdr->sh_size = 0;
2659 if (o != NULL)
2661 this_hdr->sh_size = o->offset + o->size;
2662 if (this_hdr->sh_size != 0)
2663 this_hdr->sh_type = SHT_NOBITS;
2668 /* Check for processor-specific section types. */
2669 sh_type = this_hdr->sh_type;
2670 if (bed->elf_backend_fake_sections
2671 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2672 *failedptr = TRUE;
2674 if (sh_type == SHT_NOBITS && asect->size != 0)
2676 /* Don't change the header type from NOBITS if we are being
2677 called for objcopy --only-keep-debug. */
2678 this_hdr->sh_type = sh_type;
2681 /* If the section has relocs, set up a section header for the
2682 SHT_REL[A] section. If two relocation sections are required for
2683 this section, it is up to the processor-specific back-end to
2684 create the other. */
2685 if ((asect->flags & SEC_RELOC) != 0
2686 && !_bfd_elf_init_reloc_shdr (abfd,
2687 &elf_section_data (asect)->rel_hdr,
2688 asect,
2689 asect->use_rela_p))
2690 *failedptr = TRUE;
2693 /* Fill in the contents of a SHT_GROUP section. */
2695 void
2696 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2698 bfd_boolean *failedptr = failedptrarg;
2699 unsigned long symindx;
2700 asection *elt, *first;
2701 unsigned char *loc;
2702 bfd_boolean gas;
2704 /* Ignore linker created group section. See elfNN_ia64_object_p in
2705 elfxx-ia64.c. */
2706 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2707 || *failedptr)
2708 return;
2710 symindx = 0;
2711 if (elf_group_id (sec) != NULL)
2712 symindx = elf_group_id (sec)->udata.i;
2714 if (symindx == 0)
2716 /* If called from the assembler, swap_out_syms will have set up
2717 elf_section_syms; If called for "ld -r", use target_index. */
2718 if (elf_section_syms (abfd) != NULL)
2719 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2720 else
2721 symindx = sec->target_index;
2723 elf_section_data (sec)->this_hdr.sh_info = symindx;
2725 /* The contents won't be allocated for "ld -r" or objcopy. */
2726 gas = TRUE;
2727 if (sec->contents == NULL)
2729 gas = FALSE;
2730 sec->contents = bfd_alloc (abfd, sec->size);
2732 /* Arrange for the section to be written out. */
2733 elf_section_data (sec)->this_hdr.contents = sec->contents;
2734 if (sec->contents == NULL)
2736 *failedptr = TRUE;
2737 return;
2741 loc = sec->contents + sec->size;
2743 /* Get the pointer to the first section in the group that gas
2744 squirreled away here. objcopy arranges for this to be set to the
2745 start of the input section group. */
2746 first = elt = elf_next_in_group (sec);
2748 /* First element is a flag word. Rest of section is elf section
2749 indices for all the sections of the group. Write them backwards
2750 just to keep the group in the same order as given in .section
2751 directives, not that it matters. */
2752 while (elt != NULL)
2754 asection *s;
2755 unsigned int idx;
2757 loc -= 4;
2758 s = elt;
2759 if (!gas)
2760 s = s->output_section;
2761 idx = 0;
2762 if (s != NULL)
2763 idx = elf_section_data (s)->this_idx;
2764 H_PUT_32 (abfd, idx, loc);
2765 elt = elf_next_in_group (elt);
2766 if (elt == first)
2767 break;
2770 if ((loc -= 4) != sec->contents)
2771 abort ();
2773 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2776 /* Assign all ELF section numbers. The dummy first section is handled here
2777 too. The link/info pointers for the standard section types are filled
2778 in here too, while we're at it. */
2780 static bfd_boolean
2781 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2783 struct elf_obj_tdata *t = elf_tdata (abfd);
2784 asection *sec;
2785 unsigned int section_number, secn;
2786 Elf_Internal_Shdr **i_shdrp;
2787 struct bfd_elf_section_data *d;
2789 section_number = 1;
2791 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2793 /* SHT_GROUP sections are in relocatable files only. */
2794 if (link_info == NULL || link_info->relocatable)
2796 /* Put SHT_GROUP sections first. */
2797 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2799 d = elf_section_data (sec);
2801 if (d->this_hdr.sh_type == SHT_GROUP)
2803 if (sec->flags & SEC_LINKER_CREATED)
2805 /* Remove the linker created SHT_GROUP sections. */
2806 bfd_section_list_remove (abfd, sec);
2807 abfd->section_count--;
2809 else
2810 d->this_idx = section_number++;
2815 for (sec = abfd->sections; sec; sec = sec->next)
2817 d = elf_section_data (sec);
2819 if (d->this_hdr.sh_type != SHT_GROUP)
2820 d->this_idx = section_number++;
2821 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2822 if ((sec->flags & SEC_RELOC) == 0)
2823 d->rel_idx = 0;
2824 else
2826 d->rel_idx = section_number++;
2827 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2830 if (d->rel_hdr2)
2832 d->rel_idx2 = section_number++;
2833 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2835 else
2836 d->rel_idx2 = 0;
2839 t->shstrtab_section = section_number++;
2840 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2841 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2843 if (bfd_get_symcount (abfd) > 0)
2845 t->symtab_section = section_number++;
2846 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2847 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2849 t->symtab_shndx_section = section_number++;
2850 t->symtab_shndx_hdr.sh_name
2851 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2852 ".symtab_shndx", FALSE);
2853 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2854 return FALSE;
2856 t->strtab_section = section_number++;
2857 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2860 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2861 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2863 elf_numsections (abfd) = section_number;
2864 elf_elfheader (abfd)->e_shnum = section_number;
2866 /* Set up the list of section header pointers, in agreement with the
2867 indices. */
2868 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
2869 if (i_shdrp == NULL)
2870 return FALSE;
2872 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2873 if (i_shdrp[0] == NULL)
2875 bfd_release (abfd, i_shdrp);
2876 return FALSE;
2879 elf_elfsections (abfd) = i_shdrp;
2881 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2882 if (bfd_get_symcount (abfd) > 0)
2884 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2885 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2887 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2888 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2890 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2891 t->symtab_hdr.sh_link = t->strtab_section;
2894 for (sec = abfd->sections; sec; sec = sec->next)
2896 struct bfd_elf_section_data *d = elf_section_data (sec);
2897 asection *s;
2898 const char *name;
2900 i_shdrp[d->this_idx] = &d->this_hdr;
2901 if (d->rel_idx != 0)
2902 i_shdrp[d->rel_idx] = &d->rel_hdr;
2903 if (d->rel_idx2 != 0)
2904 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2906 /* Fill in the sh_link and sh_info fields while we're at it. */
2908 /* sh_link of a reloc section is the section index of the symbol
2909 table. sh_info is the section index of the section to which
2910 the relocation entries apply. */
2911 if (d->rel_idx != 0)
2913 d->rel_hdr.sh_link = t->symtab_section;
2914 d->rel_hdr.sh_info = d->this_idx;
2916 if (d->rel_idx2 != 0)
2918 d->rel_hdr2->sh_link = t->symtab_section;
2919 d->rel_hdr2->sh_info = d->this_idx;
2922 /* We need to set up sh_link for SHF_LINK_ORDER. */
2923 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2925 s = elf_linked_to_section (sec);
2926 if (s)
2928 /* elf_linked_to_section points to the input section. */
2929 if (link_info != NULL)
2931 /* Check discarded linkonce section. */
2932 if (elf_discarded_section (s))
2934 asection *kept;
2935 (*_bfd_error_handler)
2936 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2937 abfd, d->this_hdr.bfd_section,
2938 s, s->owner);
2939 /* Point to the kept section if it has the same
2940 size as the discarded one. */
2941 kept = _bfd_elf_check_kept_section (s, link_info);
2942 if (kept == NULL)
2944 bfd_set_error (bfd_error_bad_value);
2945 return FALSE;
2947 s = kept;
2950 s = s->output_section;
2951 BFD_ASSERT (s != NULL);
2953 else
2955 /* Handle objcopy. */
2956 if (s->output_section == NULL)
2958 (*_bfd_error_handler)
2959 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2960 abfd, d->this_hdr.bfd_section, s, s->owner);
2961 bfd_set_error (bfd_error_bad_value);
2962 return FALSE;
2964 s = s->output_section;
2966 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2968 else
2970 /* PR 290:
2971 The Intel C compiler generates SHT_IA_64_UNWIND with
2972 SHF_LINK_ORDER. But it doesn't set the sh_link or
2973 sh_info fields. Hence we could get the situation
2974 where s is NULL. */
2975 const struct elf_backend_data *bed
2976 = get_elf_backend_data (abfd);
2977 if (bed->link_order_error_handler)
2978 bed->link_order_error_handler
2979 (_("%B: warning: sh_link not set for section `%A'"),
2980 abfd, sec);
2984 switch (d->this_hdr.sh_type)
2986 case SHT_REL:
2987 case SHT_RELA:
2988 /* A reloc section which we are treating as a normal BFD
2989 section. sh_link is the section index of the symbol
2990 table. sh_info is the section index of the section to
2991 which the relocation entries apply. We assume that an
2992 allocated reloc section uses the dynamic symbol table.
2993 FIXME: How can we be sure? */
2994 s = bfd_get_section_by_name (abfd, ".dynsym");
2995 if (s != NULL)
2996 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2998 /* We look up the section the relocs apply to by name. */
2999 name = sec->name;
3000 if (d->this_hdr.sh_type == SHT_REL)
3001 name += 4;
3002 else
3003 name += 5;
3004 s = bfd_get_section_by_name (abfd, name);
3005 if (s != NULL)
3006 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3007 break;
3009 case SHT_STRTAB:
3010 /* We assume that a section named .stab*str is a stabs
3011 string section. We look for a section with the same name
3012 but without the trailing ``str'', and set its sh_link
3013 field to point to this section. */
3014 if (CONST_STRNEQ (sec->name, ".stab")
3015 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3017 size_t len;
3018 char *alc;
3020 len = strlen (sec->name);
3021 alc = bfd_malloc (len - 2);
3022 if (alc == NULL)
3023 return FALSE;
3024 memcpy (alc, sec->name, len - 3);
3025 alc[len - 3] = '\0';
3026 s = bfd_get_section_by_name (abfd, alc);
3027 free (alc);
3028 if (s != NULL)
3030 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3032 /* This is a .stab section. */
3033 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3034 elf_section_data (s)->this_hdr.sh_entsize
3035 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3038 break;
3040 case SHT_DYNAMIC:
3041 case SHT_DYNSYM:
3042 case SHT_GNU_verneed:
3043 case SHT_GNU_verdef:
3044 /* sh_link is the section header index of the string table
3045 used for the dynamic entries, or the symbol table, or the
3046 version strings. */
3047 s = bfd_get_section_by_name (abfd, ".dynstr");
3048 if (s != NULL)
3049 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3050 break;
3052 case SHT_GNU_LIBLIST:
3053 /* sh_link is the section header index of the prelink library
3054 list used for the dynamic entries, or the symbol table, or
3055 the version strings. */
3056 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3057 ? ".dynstr" : ".gnu.libstr");
3058 if (s != NULL)
3059 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3060 break;
3062 case SHT_HASH:
3063 case SHT_GNU_HASH:
3064 case SHT_GNU_versym:
3065 /* sh_link is the section header index of the symbol table
3066 this hash table or version table is for. */
3067 s = bfd_get_section_by_name (abfd, ".dynsym");
3068 if (s != NULL)
3069 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3070 break;
3072 case SHT_GROUP:
3073 d->this_hdr.sh_link = t->symtab_section;
3077 for (secn = 1; secn < section_number; ++secn)
3078 if (i_shdrp[secn] == NULL)
3079 i_shdrp[secn] = i_shdrp[0];
3080 else
3081 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3082 i_shdrp[secn]->sh_name);
3083 return TRUE;
3086 /* Map symbol from it's internal number to the external number, moving
3087 all local symbols to be at the head of the list. */
3089 static bfd_boolean
3090 sym_is_global (bfd *abfd, asymbol *sym)
3092 /* If the backend has a special mapping, use it. */
3093 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3094 if (bed->elf_backend_sym_is_global)
3095 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3097 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3098 || bfd_is_und_section (bfd_get_section (sym))
3099 || bfd_is_com_section (bfd_get_section (sym)));
3102 /* Don't output section symbols for sections that are not going to be
3103 output. */
3105 static bfd_boolean
3106 ignore_section_sym (bfd *abfd, asymbol *sym)
3108 return ((sym->flags & BSF_SECTION_SYM) != 0
3109 && !(sym->section->owner == abfd
3110 || (sym->section->output_section->owner == abfd
3111 && sym->section->output_offset == 0)));
3114 static bfd_boolean
3115 elf_map_symbols (bfd *abfd)
3117 unsigned int symcount = bfd_get_symcount (abfd);
3118 asymbol **syms = bfd_get_outsymbols (abfd);
3119 asymbol **sect_syms;
3120 unsigned int num_locals = 0;
3121 unsigned int num_globals = 0;
3122 unsigned int num_locals2 = 0;
3123 unsigned int num_globals2 = 0;
3124 int max_index = 0;
3125 unsigned int idx;
3126 asection *asect;
3127 asymbol **new_syms;
3129 #ifdef DEBUG
3130 fprintf (stderr, "elf_map_symbols\n");
3131 fflush (stderr);
3132 #endif
3134 for (asect = abfd->sections; asect; asect = asect->next)
3136 if (max_index < asect->index)
3137 max_index = asect->index;
3140 max_index++;
3141 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3142 if (sect_syms == NULL)
3143 return FALSE;
3144 elf_section_syms (abfd) = sect_syms;
3145 elf_num_section_syms (abfd) = max_index;
3147 /* Init sect_syms entries for any section symbols we have already
3148 decided to output. */
3149 for (idx = 0; idx < symcount; idx++)
3151 asymbol *sym = syms[idx];
3153 if ((sym->flags & BSF_SECTION_SYM) != 0
3154 && sym->value == 0
3155 && !ignore_section_sym (abfd, sym))
3157 asection *sec = sym->section;
3159 if (sec->owner != abfd)
3160 sec = sec->output_section;
3162 sect_syms[sec->index] = syms[idx];
3166 /* Classify all of the symbols. */
3167 for (idx = 0; idx < symcount; idx++)
3169 if (ignore_section_sym (abfd, syms[idx]))
3170 continue;
3171 if (!sym_is_global (abfd, syms[idx]))
3172 num_locals++;
3173 else
3174 num_globals++;
3177 /* We will be adding a section symbol for each normal BFD section. Most
3178 sections will already have a section symbol in outsymbols, but
3179 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3180 at least in that case. */
3181 for (asect = abfd->sections; asect; asect = asect->next)
3183 if (sect_syms[asect->index] == NULL)
3185 if (!sym_is_global (abfd, asect->symbol))
3186 num_locals++;
3187 else
3188 num_globals++;
3192 /* Now sort the symbols so the local symbols are first. */
3193 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3195 if (new_syms == NULL)
3196 return FALSE;
3198 for (idx = 0; idx < symcount; idx++)
3200 asymbol *sym = syms[idx];
3201 unsigned int i;
3203 if (ignore_section_sym (abfd, sym))
3204 continue;
3205 if (!sym_is_global (abfd, sym))
3206 i = num_locals2++;
3207 else
3208 i = num_locals + num_globals2++;
3209 new_syms[i] = sym;
3210 sym->udata.i = i + 1;
3212 for (asect = abfd->sections; asect; asect = asect->next)
3214 if (sect_syms[asect->index] == NULL)
3216 asymbol *sym = asect->symbol;
3217 unsigned int i;
3219 sect_syms[asect->index] = sym;
3220 if (!sym_is_global (abfd, sym))
3221 i = num_locals2++;
3222 else
3223 i = num_locals + num_globals2++;
3224 new_syms[i] = sym;
3225 sym->udata.i = i + 1;
3229 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3231 elf_num_locals (abfd) = num_locals;
3232 elf_num_globals (abfd) = num_globals;
3233 return TRUE;
3236 /* Align to the maximum file alignment that could be required for any
3237 ELF data structure. */
3239 static inline file_ptr
3240 align_file_position (file_ptr off, int align)
3242 return (off + align - 1) & ~(align - 1);
3245 /* Assign a file position to a section, optionally aligning to the
3246 required section alignment. */
3248 file_ptr
3249 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3250 file_ptr offset,
3251 bfd_boolean align)
3253 if (align && i_shdrp->sh_addralign > 1)
3254 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3255 i_shdrp->sh_offset = offset;
3256 if (i_shdrp->bfd_section != NULL)
3257 i_shdrp->bfd_section->filepos = offset;
3258 if (i_shdrp->sh_type != SHT_NOBITS)
3259 offset += i_shdrp->sh_size;
3260 return offset;
3263 /* Compute the file positions we are going to put the sections at, and
3264 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3265 is not NULL, this is being called by the ELF backend linker. */
3267 bfd_boolean
3268 _bfd_elf_compute_section_file_positions (bfd *abfd,
3269 struct bfd_link_info *link_info)
3271 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3272 bfd_boolean failed;
3273 struct bfd_strtab_hash *strtab = NULL;
3274 Elf_Internal_Shdr *shstrtab_hdr;
3276 if (abfd->output_has_begun)
3277 return TRUE;
3279 /* Do any elf backend specific processing first. */
3280 if (bed->elf_backend_begin_write_processing)
3281 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3283 if (! prep_headers (abfd))
3284 return FALSE;
3286 /* Post process the headers if necessary. */
3287 if (bed->elf_backend_post_process_headers)
3288 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3290 failed = FALSE;
3291 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3292 if (failed)
3293 return FALSE;
3295 if (!assign_section_numbers (abfd, link_info))
3296 return FALSE;
3298 /* The backend linker builds symbol table information itself. */
3299 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3301 /* Non-zero if doing a relocatable link. */
3302 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3304 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3305 return FALSE;
3308 if (link_info == NULL)
3310 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3311 if (failed)
3312 return FALSE;
3315 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3316 /* sh_name was set in prep_headers. */
3317 shstrtab_hdr->sh_type = SHT_STRTAB;
3318 shstrtab_hdr->sh_flags = 0;
3319 shstrtab_hdr->sh_addr = 0;
3320 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3321 shstrtab_hdr->sh_entsize = 0;
3322 shstrtab_hdr->sh_link = 0;
3323 shstrtab_hdr->sh_info = 0;
3324 /* sh_offset is set in assign_file_positions_except_relocs. */
3325 shstrtab_hdr->sh_addralign = 1;
3327 if (!assign_file_positions_except_relocs (abfd, link_info))
3328 return FALSE;
3330 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3332 file_ptr off;
3333 Elf_Internal_Shdr *hdr;
3335 off = elf_tdata (abfd)->next_file_pos;
3337 hdr = &elf_tdata (abfd)->symtab_hdr;
3338 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3340 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3341 if (hdr->sh_size != 0)
3342 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3344 hdr = &elf_tdata (abfd)->strtab_hdr;
3345 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3347 elf_tdata (abfd)->next_file_pos = off;
3349 /* Now that we know where the .strtab section goes, write it
3350 out. */
3351 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3352 || ! _bfd_stringtab_emit (abfd, strtab))
3353 return FALSE;
3354 _bfd_stringtab_free (strtab);
3357 abfd->output_has_begun = TRUE;
3359 return TRUE;
3362 /* Make an initial estimate of the size of the program header. If we
3363 get the number wrong here, we'll redo section placement. */
3365 static bfd_size_type
3366 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3368 size_t segs;
3369 asection *s;
3370 const struct elf_backend_data *bed;
3372 /* Assume we will need exactly two PT_LOAD segments: one for text
3373 and one for data. */
3374 segs = 2;
3376 s = bfd_get_section_by_name (abfd, ".interp");
3377 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3379 /* If we have a loadable interpreter section, we need a
3380 PT_INTERP segment. In this case, assume we also need a
3381 PT_PHDR segment, although that may not be true for all
3382 targets. */
3383 segs += 2;
3386 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3388 /* We need a PT_DYNAMIC segment. */
3389 ++segs;
3392 if (info->relro)
3394 /* We need a PT_GNU_RELRO segment. */
3395 ++segs;
3398 if (elf_tdata (abfd)->eh_frame_hdr)
3400 /* We need a PT_GNU_EH_FRAME segment. */
3401 ++segs;
3404 if (elf_tdata (abfd)->stack_flags)
3406 /* We need a PT_GNU_STACK segment. */
3407 ++segs;
3410 for (s = abfd->sections; s != NULL; s = s->next)
3412 if ((s->flags & SEC_LOAD) != 0
3413 && CONST_STRNEQ (s->name, ".note"))
3415 /* We need a PT_NOTE segment. */
3416 ++segs;
3417 /* Try to create just one PT_NOTE segment
3418 for all adjacent loadable .note* sections.
3419 gABI requires that within a PT_NOTE segment
3420 (and also inside of each SHT_NOTE section)
3421 each note is padded to a multiple of 4 size,
3422 so we check whether the sections are correctly
3423 aligned. */
3424 if (s->alignment_power == 2)
3425 while (s->next != NULL
3426 && s->next->alignment_power == 2
3427 && (s->next->flags & SEC_LOAD) != 0
3428 && CONST_STRNEQ (s->next->name, ".note"))
3429 s = s->next;
3433 for (s = abfd->sections; s != NULL; s = s->next)
3435 if (s->flags & SEC_THREAD_LOCAL)
3437 /* We need a PT_TLS segment. */
3438 ++segs;
3439 break;
3443 /* Let the backend count up any program headers it might need. */
3444 bed = get_elf_backend_data (abfd);
3445 if (bed->elf_backend_additional_program_headers)
3447 int a;
3449 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3450 if (a == -1)
3451 abort ();
3452 segs += a;
3455 return segs * bed->s->sizeof_phdr;
3458 /* Find the segment that contains the output_section of section. */
3460 Elf_Internal_Phdr *
3461 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3463 struct elf_segment_map *m;
3464 Elf_Internal_Phdr *p;
3466 for (m = elf_tdata (abfd)->segment_map,
3467 p = elf_tdata (abfd)->phdr;
3468 m != NULL;
3469 m = m->next, p++)
3471 int i;
3473 for (i = m->count - 1; i >= 0; i--)
3474 if (m->sections[i] == section)
3475 return p;
3478 return NULL;
3481 /* Create a mapping from a set of sections to a program segment. */
3483 static struct elf_segment_map *
3484 make_mapping (bfd *abfd,
3485 asection **sections,
3486 unsigned int from,
3487 unsigned int to,
3488 bfd_boolean phdr)
3490 struct elf_segment_map *m;
3491 unsigned int i;
3492 asection **hdrpp;
3493 bfd_size_type amt;
3495 amt = sizeof (struct elf_segment_map);
3496 amt += (to - from - 1) * sizeof (asection *);
3497 m = bfd_zalloc (abfd, amt);
3498 if (m == NULL)
3499 return NULL;
3500 m->next = NULL;
3501 m->p_type = PT_LOAD;
3502 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3503 m->sections[i - from] = *hdrpp;
3504 m->count = to - from;
3506 if (from == 0 && phdr)
3508 /* Include the headers in the first PT_LOAD segment. */
3509 m->includes_filehdr = 1;
3510 m->includes_phdrs = 1;
3513 return m;
3516 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3517 on failure. */
3519 struct elf_segment_map *
3520 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3522 struct elf_segment_map *m;
3524 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3525 if (m == NULL)
3526 return NULL;
3527 m->next = NULL;
3528 m->p_type = PT_DYNAMIC;
3529 m->count = 1;
3530 m->sections[0] = dynsec;
3532 return m;
3535 /* Possibly add or remove segments from the segment map. */
3537 static bfd_boolean
3538 elf_modify_segment_map (bfd *abfd,
3539 struct bfd_link_info *info,
3540 bfd_boolean remove_empty_load)
3542 struct elf_segment_map **m;
3543 const struct elf_backend_data *bed;
3545 /* The placement algorithm assumes that non allocated sections are
3546 not in PT_LOAD segments. We ensure this here by removing such
3547 sections from the segment map. We also remove excluded
3548 sections. Finally, any PT_LOAD segment without sections is
3549 removed. */
3550 m = &elf_tdata (abfd)->segment_map;
3551 while (*m)
3553 unsigned int i, new_count;
3555 for (new_count = 0, i = 0; i < (*m)->count; i++)
3557 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3558 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3559 || (*m)->p_type != PT_LOAD))
3561 (*m)->sections[new_count] = (*m)->sections[i];
3562 new_count++;
3565 (*m)->count = new_count;
3567 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3568 *m = (*m)->next;
3569 else
3570 m = &(*m)->next;
3573 bed = get_elf_backend_data (abfd);
3574 if (bed->elf_backend_modify_segment_map != NULL)
3576 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3577 return FALSE;
3580 return TRUE;
3583 /* Set up a mapping from BFD sections to program segments. */
3585 bfd_boolean
3586 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3588 unsigned int count;
3589 struct elf_segment_map *m;
3590 asection **sections = NULL;
3591 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3592 bfd_boolean no_user_phdrs;
3594 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3595 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3597 asection *s;
3598 unsigned int i;
3599 struct elf_segment_map *mfirst;
3600 struct elf_segment_map **pm;
3601 asection *last_hdr;
3602 bfd_vma last_size;
3603 unsigned int phdr_index;
3604 bfd_vma maxpagesize;
3605 asection **hdrpp;
3606 bfd_boolean phdr_in_segment = TRUE;
3607 bfd_boolean writable;
3608 int tls_count = 0;
3609 asection *first_tls = NULL;
3610 asection *dynsec, *eh_frame_hdr;
3611 bfd_size_type amt;
3613 /* Select the allocated sections, and sort them. */
3615 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3616 if (sections == NULL)
3617 goto error_return;
3619 i = 0;
3620 for (s = abfd->sections; s != NULL; s = s->next)
3622 if ((s->flags & SEC_ALLOC) != 0)
3624 sections[i] = s;
3625 ++i;
3628 BFD_ASSERT (i <= bfd_count_sections (abfd));
3629 count = i;
3631 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3633 /* Build the mapping. */
3635 mfirst = NULL;
3636 pm = &mfirst;
3638 /* If we have a .interp section, then create a PT_PHDR segment for
3639 the program headers and a PT_INTERP segment for the .interp
3640 section. */
3641 s = bfd_get_section_by_name (abfd, ".interp");
3642 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3644 amt = sizeof (struct elf_segment_map);
3645 m = bfd_zalloc (abfd, amt);
3646 if (m == NULL)
3647 goto error_return;
3648 m->next = NULL;
3649 m->p_type = PT_PHDR;
3650 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3651 m->p_flags = PF_R | PF_X;
3652 m->p_flags_valid = 1;
3653 m->includes_phdrs = 1;
3655 *pm = m;
3656 pm = &m->next;
3658 amt = sizeof (struct elf_segment_map);
3659 m = bfd_zalloc (abfd, amt);
3660 if (m == NULL)
3661 goto error_return;
3662 m->next = NULL;
3663 m->p_type = PT_INTERP;
3664 m->count = 1;
3665 m->sections[0] = s;
3667 *pm = m;
3668 pm = &m->next;
3671 /* Look through the sections. We put sections in the same program
3672 segment when the start of the second section can be placed within
3673 a few bytes of the end of the first section. */
3674 last_hdr = NULL;
3675 last_size = 0;
3676 phdr_index = 0;
3677 maxpagesize = bed->maxpagesize;
3678 writable = FALSE;
3679 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3680 if (dynsec != NULL
3681 && (dynsec->flags & SEC_LOAD) == 0)
3682 dynsec = NULL;
3684 /* Deal with -Ttext or something similar such that the first section
3685 is not adjacent to the program headers. This is an
3686 approximation, since at this point we don't know exactly how many
3687 program headers we will need. */
3688 if (count > 0)
3690 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3692 if (phdr_size == (bfd_size_type) -1)
3693 phdr_size = get_program_header_size (abfd, info);
3694 if ((abfd->flags & D_PAGED) == 0
3695 || sections[0]->lma < phdr_size
3696 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3697 phdr_in_segment = FALSE;
3700 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3702 asection *hdr;
3703 bfd_boolean new_segment;
3705 hdr = *hdrpp;
3707 /* See if this section and the last one will fit in the same
3708 segment. */
3710 if (last_hdr == NULL)
3712 /* If we don't have a segment yet, then we don't need a new
3713 one (we build the last one after this loop). */
3714 new_segment = FALSE;
3716 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3718 /* If this section has a different relation between the
3719 virtual address and the load address, then we need a new
3720 segment. */
3721 new_segment = TRUE;
3723 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3724 < BFD_ALIGN (hdr->lma, maxpagesize))
3726 /* If putting this section in this segment would force us to
3727 skip a page in the segment, then we need a new segment. */
3728 new_segment = TRUE;
3730 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3731 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3733 /* We don't want to put a loadable section after a
3734 nonloadable section in the same segment.
3735 Consider .tbss sections as loadable for this purpose. */
3736 new_segment = TRUE;
3738 else if ((abfd->flags & D_PAGED) == 0)
3740 /* If the file is not demand paged, which means that we
3741 don't require the sections to be correctly aligned in the
3742 file, then there is no other reason for a new segment. */
3743 new_segment = FALSE;
3745 else if (! writable
3746 && (hdr->flags & SEC_READONLY) == 0
3747 && (((last_hdr->lma + last_size - 1)
3748 & ~(maxpagesize - 1))
3749 != (hdr->lma & ~(maxpagesize - 1))))
3751 /* We don't want to put a writable section in a read only
3752 segment, unless they are on the same page in memory
3753 anyhow. We already know that the last section does not
3754 bring us past the current section on the page, so the
3755 only case in which the new section is not on the same
3756 page as the previous section is when the previous section
3757 ends precisely on a page boundary. */
3758 new_segment = TRUE;
3760 else
3762 /* Otherwise, we can use the same segment. */
3763 new_segment = FALSE;
3766 /* Allow interested parties a chance to override our decision. */
3767 if (last_hdr && info->callbacks->override_segment_assignment)
3768 new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment);
3770 if (! new_segment)
3772 if ((hdr->flags & SEC_READONLY) == 0)
3773 writable = TRUE;
3774 last_hdr = hdr;
3775 /* .tbss sections effectively have zero size. */
3776 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3777 != SEC_THREAD_LOCAL)
3778 last_size = hdr->size;
3779 else
3780 last_size = 0;
3781 continue;
3784 /* We need a new program segment. We must create a new program
3785 header holding all the sections from phdr_index until hdr. */
3787 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3788 if (m == NULL)
3789 goto error_return;
3791 *pm = m;
3792 pm = &m->next;
3794 if ((hdr->flags & SEC_READONLY) == 0)
3795 writable = TRUE;
3796 else
3797 writable = FALSE;
3799 last_hdr = hdr;
3800 /* .tbss sections effectively have zero size. */
3801 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3802 last_size = hdr->size;
3803 else
3804 last_size = 0;
3805 phdr_index = i;
3806 phdr_in_segment = FALSE;
3809 /* Create a final PT_LOAD program segment. */
3810 if (last_hdr != NULL)
3812 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3813 if (m == NULL)
3814 goto error_return;
3816 *pm = m;
3817 pm = &m->next;
3820 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3821 if (dynsec != NULL)
3823 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3824 if (m == NULL)
3825 goto error_return;
3826 *pm = m;
3827 pm = &m->next;
3830 /* For each batch of consecutive loadable .note sections,
3831 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3832 because if we link together nonloadable .note sections and
3833 loadable .note sections, we will generate two .note sections
3834 in the output file. FIXME: Using names for section types is
3835 bogus anyhow. */
3836 for (s = abfd->sections; s != NULL; s = s->next)
3838 if ((s->flags & SEC_LOAD) != 0
3839 && CONST_STRNEQ (s->name, ".note"))
3841 asection *s2;
3842 unsigned count = 1;
3843 amt = sizeof (struct elf_segment_map);
3844 if (s->alignment_power == 2)
3845 for (s2 = s; s2->next != NULL; s2 = s2->next)
3847 if (s2->next->alignment_power == 2
3848 && (s2->next->flags & SEC_LOAD) != 0
3849 && CONST_STRNEQ (s2->next->name, ".note")
3850 && align_power (s2->vma + s2->size, 2)
3851 == s2->next->vma)
3852 count++;
3853 else
3854 break;
3856 amt += (count - 1) * sizeof (asection *);
3857 m = bfd_zalloc (abfd, amt);
3858 if (m == NULL)
3859 goto error_return;
3860 m->next = NULL;
3861 m->p_type = PT_NOTE;
3862 m->count = count;
3863 while (count > 1)
3865 m->sections[m->count - count--] = s;
3866 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3867 s = s->next;
3869 m->sections[m->count - 1] = s;
3870 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3871 *pm = m;
3872 pm = &m->next;
3874 if (s->flags & SEC_THREAD_LOCAL)
3876 if (! tls_count)
3877 first_tls = s;
3878 tls_count++;
3882 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3883 if (tls_count > 0)
3885 int i;
3887 amt = sizeof (struct elf_segment_map);
3888 amt += (tls_count - 1) * sizeof (asection *);
3889 m = bfd_zalloc (abfd, amt);
3890 if (m == NULL)
3891 goto error_return;
3892 m->next = NULL;
3893 m->p_type = PT_TLS;
3894 m->count = tls_count;
3895 /* Mandated PF_R. */
3896 m->p_flags = PF_R;
3897 m->p_flags_valid = 1;
3898 for (i = 0; i < tls_count; ++i)
3900 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3901 m->sections[i] = first_tls;
3902 first_tls = first_tls->next;
3905 *pm = m;
3906 pm = &m->next;
3909 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3910 segment. */
3911 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3912 if (eh_frame_hdr != NULL
3913 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3915 amt = sizeof (struct elf_segment_map);
3916 m = bfd_zalloc (abfd, amt);
3917 if (m == NULL)
3918 goto error_return;
3919 m->next = NULL;
3920 m->p_type = PT_GNU_EH_FRAME;
3921 m->count = 1;
3922 m->sections[0] = eh_frame_hdr->output_section;
3924 *pm = m;
3925 pm = &m->next;
3928 if (elf_tdata (abfd)->stack_flags)
3930 amt = sizeof (struct elf_segment_map);
3931 m = bfd_zalloc (abfd, amt);
3932 if (m == NULL)
3933 goto error_return;
3934 m->next = NULL;
3935 m->p_type = PT_GNU_STACK;
3936 m->p_flags = elf_tdata (abfd)->stack_flags;
3937 m->p_flags_valid = 1;
3939 *pm = m;
3940 pm = &m->next;
3943 if (info->relro)
3945 for (m = mfirst; m != NULL; m = m->next)
3947 if (m->p_type == PT_LOAD)
3949 asection *last = m->sections[m->count - 1];
3950 bfd_vma vaddr = m->sections[0]->vma;
3951 bfd_vma filesz = last->vma - vaddr + last->size;
3953 if (vaddr < info->relro_end
3954 && vaddr >= info->relro_start
3955 && (vaddr + filesz) >= info->relro_end)
3956 break;
3960 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3961 if (m != NULL)
3963 amt = sizeof (struct elf_segment_map);
3964 m = bfd_zalloc (abfd, amt);
3965 if (m == NULL)
3966 goto error_return;
3967 m->next = NULL;
3968 m->p_type = PT_GNU_RELRO;
3969 m->p_flags = PF_R;
3970 m->p_flags_valid = 1;
3972 *pm = m;
3973 pm = &m->next;
3977 free (sections);
3978 elf_tdata (abfd)->segment_map = mfirst;
3981 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
3982 return FALSE;
3984 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3985 ++count;
3986 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
3988 return TRUE;
3990 error_return:
3991 if (sections != NULL)
3992 free (sections);
3993 return FALSE;
3996 /* Sort sections by address. */
3998 static int
3999 elf_sort_sections (const void *arg1, const void *arg2)
4001 const asection *sec1 = *(const asection **) arg1;
4002 const asection *sec2 = *(const asection **) arg2;
4003 bfd_size_type size1, size2;
4005 /* Sort by LMA first, since this is the address used to
4006 place the section into a segment. */
4007 if (sec1->lma < sec2->lma)
4008 return -1;
4009 else if (sec1->lma > sec2->lma)
4010 return 1;
4012 /* Then sort by VMA. Normally the LMA and the VMA will be
4013 the same, and this will do nothing. */
4014 if (sec1->vma < sec2->vma)
4015 return -1;
4016 else if (sec1->vma > sec2->vma)
4017 return 1;
4019 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4021 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4023 if (TOEND (sec1))
4025 if (TOEND (sec2))
4027 /* If the indicies are the same, do not return 0
4028 here, but continue to try the next comparison. */
4029 if (sec1->target_index - sec2->target_index != 0)
4030 return sec1->target_index - sec2->target_index;
4032 else
4033 return 1;
4035 else if (TOEND (sec2))
4036 return -1;
4038 #undef TOEND
4040 /* Sort by size, to put zero sized sections
4041 before others at the same address. */
4043 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4044 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4046 if (size1 < size2)
4047 return -1;
4048 if (size1 > size2)
4049 return 1;
4051 return sec1->target_index - sec2->target_index;
4054 /* Ian Lance Taylor writes:
4056 We shouldn't be using % with a negative signed number. That's just
4057 not good. We have to make sure either that the number is not
4058 negative, or that the number has an unsigned type. When the types
4059 are all the same size they wind up as unsigned. When file_ptr is a
4060 larger signed type, the arithmetic winds up as signed long long,
4061 which is wrong.
4063 What we're trying to say here is something like ``increase OFF by
4064 the least amount that will cause it to be equal to the VMA modulo
4065 the page size.'' */
4066 /* In other words, something like:
4068 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4069 off_offset = off % bed->maxpagesize;
4070 if (vma_offset < off_offset)
4071 adjustment = vma_offset + bed->maxpagesize - off_offset;
4072 else
4073 adjustment = vma_offset - off_offset;
4075 which can can be collapsed into the expression below. */
4077 static file_ptr
4078 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4080 return ((vma - off) % maxpagesize);
4083 static void
4084 print_segment_map (const struct elf_segment_map *m)
4086 unsigned int j;
4087 const char *pt = get_segment_type (m->p_type);
4088 char buf[32];
4090 if (pt == NULL)
4092 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4093 sprintf (buf, "LOPROC+%7.7x",
4094 (unsigned int) (m->p_type - PT_LOPROC));
4095 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4096 sprintf (buf, "LOOS+%7.7x",
4097 (unsigned int) (m->p_type - PT_LOOS));
4098 else
4099 snprintf (buf, sizeof (buf), "%8.8x",
4100 (unsigned int) m->p_type);
4101 pt = buf;
4103 fprintf (stderr, "%s:", pt);
4104 for (j = 0; j < m->count; j++)
4105 fprintf (stderr, " %s", m->sections [j]->name);
4106 putc ('\n',stderr);
4109 /* Assign file positions to the sections based on the mapping from
4110 sections to segments. This function also sets up some fields in
4111 the file header. */
4113 static bfd_boolean
4114 assign_file_positions_for_load_sections (bfd *abfd,
4115 struct bfd_link_info *link_info)
4117 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4118 struct elf_segment_map *m;
4119 Elf_Internal_Phdr *phdrs;
4120 Elf_Internal_Phdr *p;
4121 file_ptr off;
4122 bfd_size_type maxpagesize;
4123 unsigned int alloc;
4124 unsigned int i, j;
4126 if (link_info == NULL
4127 && !elf_modify_segment_map (abfd, link_info, FALSE))
4128 return FALSE;
4130 alloc = 0;
4131 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4132 ++alloc;
4134 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4135 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4136 elf_elfheader (abfd)->e_phnum = alloc;
4138 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4139 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4140 else
4141 BFD_ASSERT (elf_tdata (abfd)->program_header_size
4142 >= alloc * bed->s->sizeof_phdr);
4144 if (alloc == 0)
4146 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4147 return TRUE;
4150 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
4151 elf_tdata (abfd)->phdr = phdrs;
4152 if (phdrs == NULL)
4153 return FALSE;
4155 maxpagesize = 1;
4156 if ((abfd->flags & D_PAGED) != 0)
4157 maxpagesize = bed->maxpagesize;
4159 off = bed->s->sizeof_ehdr;
4160 off += alloc * bed->s->sizeof_phdr;
4162 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4163 m != NULL;
4164 m = m->next, p++, j++)
4166 asection **secpp;
4167 bfd_vma off_adjust;
4168 bfd_boolean no_contents;
4170 /* If elf_segment_map is not from map_sections_to_segments, the
4171 sections may not be correctly ordered. NOTE: sorting should
4172 not be done to the PT_NOTE section of a corefile, which may
4173 contain several pseudo-sections artificially created by bfd.
4174 Sorting these pseudo-sections breaks things badly. */
4175 if (m->count > 1
4176 && !(elf_elfheader (abfd)->e_type == ET_CORE
4177 && m->p_type == PT_NOTE))
4178 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4179 elf_sort_sections);
4181 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4182 number of sections with contents contributing to both p_filesz
4183 and p_memsz, followed by a number of sections with no contents
4184 that just contribute to p_memsz. In this loop, OFF tracks next
4185 available file offset for PT_LOAD and PT_NOTE segments. */
4186 p->p_type = m->p_type;
4187 p->p_flags = m->p_flags;
4189 if (m->count == 0)
4190 p->p_vaddr = 0;
4191 else
4192 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4194 if (m->p_paddr_valid)
4195 p->p_paddr = m->p_paddr;
4196 else if (m->count == 0)
4197 p->p_paddr = 0;
4198 else
4199 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4201 if (p->p_type == PT_LOAD
4202 && (abfd->flags & D_PAGED) != 0)
4204 /* p_align in demand paged PT_LOAD segments effectively stores
4205 the maximum page size. When copying an executable with
4206 objcopy, we set m->p_align from the input file. Use this
4207 value for maxpagesize rather than bed->maxpagesize, which
4208 may be different. Note that we use maxpagesize for PT_TLS
4209 segment alignment later in this function, so we are relying
4210 on at least one PT_LOAD segment appearing before a PT_TLS
4211 segment. */
4212 if (m->p_align_valid)
4213 maxpagesize = m->p_align;
4215 p->p_align = maxpagesize;
4217 else if (m->p_align_valid)
4218 p->p_align = m->p_align;
4219 else if (m->count == 0)
4220 p->p_align = 1 << bed->s->log_file_align;
4221 else
4222 p->p_align = 0;
4224 no_contents = FALSE;
4225 off_adjust = 0;
4226 if (p->p_type == PT_LOAD
4227 && m->count > 0)
4229 bfd_size_type align;
4230 unsigned int align_power = 0;
4232 if (m->p_align_valid)
4233 align = p->p_align;
4234 else
4236 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4238 unsigned int secalign;
4240 secalign = bfd_get_section_alignment (abfd, *secpp);
4241 if (secalign > align_power)
4242 align_power = secalign;
4244 align = (bfd_size_type) 1 << align_power;
4245 if (align < maxpagesize)
4246 align = maxpagesize;
4249 for (i = 0; i < m->count; i++)
4250 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4251 /* If we aren't making room for this section, then
4252 it must be SHT_NOBITS regardless of what we've
4253 set via struct bfd_elf_special_section. */
4254 elf_section_type (m->sections[i]) = SHT_NOBITS;
4256 /* Find out whether this segment contains any loadable
4257 sections. If the first section isn't loadable, the same
4258 holds for any other sections. */
4259 i = 0;
4260 while (elf_section_type (m->sections[i]) == SHT_NOBITS)
4262 /* If a segment starts with .tbss, we need to look
4263 at the next section to decide whether the segment
4264 has any loadable sections. */
4265 if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
4266 || ++i >= m->count)
4268 no_contents = TRUE;
4269 break;
4273 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4274 off += off_adjust;
4275 if (no_contents)
4277 /* We shouldn't need to align the segment on disk since
4278 the segment doesn't need file space, but the gABI
4279 arguably requires the alignment and glibc ld.so
4280 checks it. So to comply with the alignment
4281 requirement but not waste file space, we adjust
4282 p_offset for just this segment. (OFF_ADJUST is
4283 subtracted from OFF later.) This may put p_offset
4284 past the end of file, but that shouldn't matter. */
4286 else
4287 off_adjust = 0;
4289 /* Make sure the .dynamic section is the first section in the
4290 PT_DYNAMIC segment. */
4291 else if (p->p_type == PT_DYNAMIC
4292 && m->count > 1
4293 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4295 _bfd_error_handler
4296 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4297 abfd);
4298 bfd_set_error (bfd_error_bad_value);
4299 return FALSE;
4301 /* Set the note section type to SHT_NOTE. */
4302 else if (p->p_type == PT_NOTE)
4303 for (i = 0; i < m->count; i++)
4304 elf_section_type (m->sections[i]) = SHT_NOTE;
4306 p->p_offset = 0;
4307 p->p_filesz = 0;
4308 p->p_memsz = 0;
4310 if (m->includes_filehdr)
4312 if (!m->p_flags_valid)
4313 p->p_flags |= PF_R;
4314 p->p_filesz = bed->s->sizeof_ehdr;
4315 p->p_memsz = bed->s->sizeof_ehdr;
4316 if (m->count > 0)
4318 BFD_ASSERT (p->p_type == PT_LOAD);
4320 if (p->p_vaddr < (bfd_vma) off)
4322 (*_bfd_error_handler)
4323 (_("%B: Not enough room for program headers, try linking with -N"),
4324 abfd);
4325 bfd_set_error (bfd_error_bad_value);
4326 return FALSE;
4329 p->p_vaddr -= off;
4330 if (!m->p_paddr_valid)
4331 p->p_paddr -= off;
4335 if (m->includes_phdrs)
4337 if (!m->p_flags_valid)
4338 p->p_flags |= PF_R;
4340 if (!m->includes_filehdr)
4342 p->p_offset = bed->s->sizeof_ehdr;
4344 if (m->count > 0)
4346 BFD_ASSERT (p->p_type == PT_LOAD);
4347 p->p_vaddr -= off - p->p_offset;
4348 if (!m->p_paddr_valid)
4349 p->p_paddr -= off - p->p_offset;
4353 p->p_filesz += alloc * bed->s->sizeof_phdr;
4354 p->p_memsz += alloc * bed->s->sizeof_phdr;
4357 if (p->p_type == PT_LOAD
4358 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4360 if (!m->includes_filehdr && !m->includes_phdrs)
4361 p->p_offset = off;
4362 else
4364 file_ptr adjust;
4366 adjust = off - (p->p_offset + p->p_filesz);
4367 if (!no_contents)
4368 p->p_filesz += adjust;
4369 p->p_memsz += adjust;
4373 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4374 maps. Set filepos for sections in PT_LOAD segments, and in
4375 core files, for sections in PT_NOTE segments.
4376 assign_file_positions_for_non_load_sections will set filepos
4377 for other sections and update p_filesz for other segments. */
4378 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4380 asection *sec;
4381 bfd_size_type align;
4382 Elf_Internal_Shdr *this_hdr;
4384 sec = *secpp;
4385 this_hdr = &elf_section_data (sec)->this_hdr;
4386 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4388 if ((p->p_type == PT_LOAD
4389 || p->p_type == PT_TLS)
4390 && (this_hdr->sh_type != SHT_NOBITS
4391 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4392 && ((this_hdr->sh_flags & SHF_TLS) == 0
4393 || p->p_type == PT_TLS))))
4395 bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
4397 if (adjust < 0)
4399 (*_bfd_error_handler)
4400 (_("%B: section %A vma 0x%lx overlaps previous sections"),
4401 abfd, sec, (unsigned long) sec->lma);
4402 adjust = 0;
4404 p->p_memsz += adjust;
4406 if (this_hdr->sh_type != SHT_NOBITS)
4408 off += adjust;
4409 p->p_filesz += adjust;
4413 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4415 /* The section at i == 0 is the one that actually contains
4416 everything. */
4417 if (i == 0)
4419 this_hdr->sh_offset = sec->filepos = off;
4420 off += this_hdr->sh_size;
4421 p->p_filesz = this_hdr->sh_size;
4422 p->p_memsz = 0;
4423 p->p_align = 1;
4425 else
4427 /* The rest are fake sections that shouldn't be written. */
4428 sec->filepos = 0;
4429 sec->size = 0;
4430 sec->flags = 0;
4431 continue;
4434 else
4436 if (p->p_type == PT_LOAD)
4438 this_hdr->sh_offset = sec->filepos = off;
4439 if (this_hdr->sh_type != SHT_NOBITS)
4440 off += this_hdr->sh_size;
4443 if (this_hdr->sh_type != SHT_NOBITS)
4445 p->p_filesz += this_hdr->sh_size;
4446 /* A load section without SHF_ALLOC is something like
4447 a note section in a PT_NOTE segment. These take
4448 file space but are not loaded into memory. */
4449 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4450 p->p_memsz += this_hdr->sh_size;
4452 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4454 if (p->p_type == PT_TLS)
4455 p->p_memsz += this_hdr->sh_size;
4457 /* .tbss is special. It doesn't contribute to p_memsz of
4458 normal segments. */
4459 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4460 p->p_memsz += this_hdr->sh_size;
4463 if (align > p->p_align
4464 && !m->p_align_valid
4465 && (p->p_type != PT_LOAD
4466 || (abfd->flags & D_PAGED) == 0))
4467 p->p_align = align;
4470 if (!m->p_flags_valid)
4472 p->p_flags |= PF_R;
4473 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4474 p->p_flags |= PF_X;
4475 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4476 p->p_flags |= PF_W;
4479 off -= off_adjust;
4481 /* Check that all sections are in a PT_LOAD segment.
4482 Don't check funky gdb generated core files. */
4483 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4484 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4486 Elf_Internal_Shdr *this_hdr;
4487 asection *sec;
4489 sec = *secpp;
4490 this_hdr = &(elf_section_data(sec)->this_hdr);
4491 if (this_hdr->sh_size != 0
4492 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4494 (*_bfd_error_handler)
4495 (_("%B: section `%A' can't be allocated in segment %d"),
4496 abfd, sec, j);
4497 print_segment_map (m);
4498 bfd_set_error (bfd_error_bad_value);
4499 return FALSE;
4504 elf_tdata (abfd)->next_file_pos = off;
4505 return TRUE;
4508 /* Assign file positions for the other sections. */
4510 static bfd_boolean
4511 assign_file_positions_for_non_load_sections (bfd *abfd,
4512 struct bfd_link_info *link_info)
4514 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4515 Elf_Internal_Shdr **i_shdrpp;
4516 Elf_Internal_Shdr **hdrpp;
4517 Elf_Internal_Phdr *phdrs;
4518 Elf_Internal_Phdr *p;
4519 struct elf_segment_map *m;
4520 bfd_vma filehdr_vaddr, filehdr_paddr;
4521 bfd_vma phdrs_vaddr, phdrs_paddr;
4522 file_ptr off;
4523 unsigned int num_sec;
4524 unsigned int i;
4525 unsigned int count;
4527 i_shdrpp = elf_elfsections (abfd);
4528 num_sec = elf_numsections (abfd);
4529 off = elf_tdata (abfd)->next_file_pos;
4530 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4532 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4533 Elf_Internal_Shdr *hdr;
4535 hdr = *hdrpp;
4536 if (hdr->bfd_section != NULL
4537 && (hdr->bfd_section->filepos != 0
4538 || (hdr->sh_type == SHT_NOBITS
4539 && hdr->contents == NULL)))
4540 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4541 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4543 if (hdr->sh_size != 0)
4544 ((*_bfd_error_handler)
4545 (_("%B: warning: allocated section `%s' not in segment"),
4546 abfd,
4547 (hdr->bfd_section == NULL
4548 ? "*unknown*"
4549 : hdr->bfd_section->name)));
4550 /* We don't need to page align empty sections. */
4551 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4552 off += vma_page_aligned_bias (hdr->sh_addr, off,
4553 bed->maxpagesize);
4554 else
4555 off += vma_page_aligned_bias (hdr->sh_addr, off,
4556 hdr->sh_addralign);
4557 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4558 FALSE);
4560 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4561 && hdr->bfd_section == NULL)
4562 || hdr == i_shdrpp[tdata->symtab_section]
4563 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4564 || hdr == i_shdrpp[tdata->strtab_section])
4565 hdr->sh_offset = -1;
4566 else
4567 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4570 /* Now that we have set the section file positions, we can set up
4571 the file positions for the non PT_LOAD segments. */
4572 count = 0;
4573 filehdr_vaddr = 0;
4574 filehdr_paddr = 0;
4575 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4576 phdrs_paddr = 0;
4577 phdrs = elf_tdata (abfd)->phdr;
4578 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4579 m != NULL;
4580 m = m->next, p++)
4582 ++count;
4583 if (p->p_type != PT_LOAD)
4584 continue;
4586 if (m->includes_filehdr)
4588 filehdr_vaddr = p->p_vaddr;
4589 filehdr_paddr = p->p_paddr;
4591 if (m->includes_phdrs)
4593 phdrs_vaddr = p->p_vaddr;
4594 phdrs_paddr = p->p_paddr;
4595 if (m->includes_filehdr)
4597 phdrs_vaddr += bed->s->sizeof_ehdr;
4598 phdrs_paddr += bed->s->sizeof_ehdr;
4603 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4604 m != NULL;
4605 m = m->next, p++)
4607 if (m->count != 0)
4609 if (p->p_type != PT_LOAD
4610 && (p->p_type != PT_NOTE
4611 || bfd_get_format (abfd) != bfd_core))
4613 Elf_Internal_Shdr *hdr;
4614 asection *sect;
4616 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4618 sect = m->sections[m->count - 1];
4619 hdr = &elf_section_data (sect)->this_hdr;
4620 p->p_filesz = sect->filepos - m->sections[0]->filepos;
4621 if (hdr->sh_type != SHT_NOBITS)
4622 p->p_filesz += hdr->sh_size;
4624 if (p->p_type == PT_GNU_RELRO)
4626 /* When we get here, we are copying executable
4627 or shared library. But we need to use the same
4628 linker logic. */
4629 Elf_Internal_Phdr *lp;
4631 for (lp = phdrs; lp < phdrs + count; ++lp)
4633 if (lp->p_type == PT_LOAD
4634 && lp->p_paddr == p->p_paddr)
4635 break;
4638 if (lp < phdrs + count)
4640 /* We should use p_size if it is valid since it
4641 may contain the first few bytes of the next
4642 SEC_ALLOC section. */
4643 if (m->p_size_valid)
4644 p->p_filesz = m->p_size;
4645 else
4646 abort ();
4647 p->p_vaddr = lp->p_vaddr;
4648 p->p_offset = lp->p_offset;
4649 p->p_memsz = p->p_filesz;
4650 p->p_align = 1;
4652 else
4653 abort ();
4655 else
4656 p->p_offset = m->sections[0]->filepos;
4659 else
4661 if (m->includes_filehdr)
4663 p->p_vaddr = filehdr_vaddr;
4664 if (! m->p_paddr_valid)
4665 p->p_paddr = filehdr_paddr;
4667 else if (m->includes_phdrs)
4669 p->p_vaddr = phdrs_vaddr;
4670 if (! m->p_paddr_valid)
4671 p->p_paddr = phdrs_paddr;
4673 else if (p->p_type == PT_GNU_RELRO)
4675 Elf_Internal_Phdr *lp;
4677 for (lp = phdrs; lp < phdrs + count; ++lp)
4679 if (lp->p_type == PT_LOAD
4680 && lp->p_vaddr <= link_info->relro_end
4681 && lp->p_vaddr >= link_info->relro_start
4682 && (lp->p_vaddr + lp->p_filesz
4683 >= link_info->relro_end))
4684 break;
4687 if (lp < phdrs + count
4688 && link_info->relro_end > lp->p_vaddr)
4690 p->p_vaddr = lp->p_vaddr;
4691 p->p_paddr = lp->p_paddr;
4692 p->p_offset = lp->p_offset;
4693 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4694 p->p_memsz = p->p_filesz;
4695 p->p_align = 1;
4696 p->p_flags = (lp->p_flags & ~PF_W);
4698 else
4700 memset (p, 0, sizeof *p);
4701 p->p_type = PT_NULL;
4707 elf_tdata (abfd)->next_file_pos = off;
4709 return TRUE;
4712 /* Work out the file positions of all the sections. This is called by
4713 _bfd_elf_compute_section_file_positions. All the section sizes and
4714 VMAs must be known before this is called.
4716 Reloc sections come in two flavours: Those processed specially as
4717 "side-channel" data attached to a section to which they apply, and
4718 those that bfd doesn't process as relocations. The latter sort are
4719 stored in a normal bfd section by bfd_section_from_shdr. We don't
4720 consider the former sort here, unless they form part of the loadable
4721 image. Reloc sections not assigned here will be handled later by
4722 assign_file_positions_for_relocs.
4724 We also don't set the positions of the .symtab and .strtab here. */
4726 static bfd_boolean
4727 assign_file_positions_except_relocs (bfd *abfd,
4728 struct bfd_link_info *link_info)
4730 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4731 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4732 file_ptr off;
4733 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4735 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4736 && bfd_get_format (abfd) != bfd_core)
4738 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4739 unsigned int num_sec = elf_numsections (abfd);
4740 Elf_Internal_Shdr **hdrpp;
4741 unsigned int i;
4743 /* Start after the ELF header. */
4744 off = i_ehdrp->e_ehsize;
4746 /* We are not creating an executable, which means that we are
4747 not creating a program header, and that the actual order of
4748 the sections in the file is unimportant. */
4749 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4751 Elf_Internal_Shdr *hdr;
4753 hdr = *hdrpp;
4754 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4755 && hdr->bfd_section == NULL)
4756 || i == tdata->symtab_section
4757 || i == tdata->symtab_shndx_section
4758 || i == tdata->strtab_section)
4760 hdr->sh_offset = -1;
4762 else
4763 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4766 else
4768 unsigned int alloc;
4770 /* Assign file positions for the loaded sections based on the
4771 assignment of sections to segments. */
4772 if (!assign_file_positions_for_load_sections (abfd, link_info))
4773 return FALSE;
4775 /* And for non-load sections. */
4776 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4777 return FALSE;
4779 if (bed->elf_backend_modify_program_headers != NULL)
4781 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4782 return FALSE;
4785 /* Write out the program headers. */
4786 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4787 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4788 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4789 return FALSE;
4791 off = tdata->next_file_pos;
4794 /* Place the section headers. */
4795 off = align_file_position (off, 1 << bed->s->log_file_align);
4796 i_ehdrp->e_shoff = off;
4797 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4799 tdata->next_file_pos = off;
4801 return TRUE;
4804 static bfd_boolean
4805 prep_headers (bfd *abfd)
4807 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4808 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4809 struct elf_strtab_hash *shstrtab;
4810 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4812 i_ehdrp = elf_elfheader (abfd);
4814 shstrtab = _bfd_elf_strtab_init ();
4815 if (shstrtab == NULL)
4816 return FALSE;
4818 elf_shstrtab (abfd) = shstrtab;
4820 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4821 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4822 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4823 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4825 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4826 i_ehdrp->e_ident[EI_DATA] =
4827 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4828 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4830 if ((abfd->flags & DYNAMIC) != 0)
4831 i_ehdrp->e_type = ET_DYN;
4832 else if ((abfd->flags & EXEC_P) != 0)
4833 i_ehdrp->e_type = ET_EXEC;
4834 else if (bfd_get_format (abfd) == bfd_core)
4835 i_ehdrp->e_type = ET_CORE;
4836 else
4837 i_ehdrp->e_type = ET_REL;
4839 switch (bfd_get_arch (abfd))
4841 case bfd_arch_unknown:
4842 i_ehdrp->e_machine = EM_NONE;
4843 break;
4845 /* There used to be a long list of cases here, each one setting
4846 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4847 in the corresponding bfd definition. To avoid duplication,
4848 the switch was removed. Machines that need special handling
4849 can generally do it in elf_backend_final_write_processing(),
4850 unless they need the information earlier than the final write.
4851 Such need can generally be supplied by replacing the tests for
4852 e_machine with the conditions used to determine it. */
4853 default:
4854 i_ehdrp->e_machine = bed->elf_machine_code;
4857 i_ehdrp->e_version = bed->s->ev_current;
4858 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4860 /* No program header, for now. */
4861 i_ehdrp->e_phoff = 0;
4862 i_ehdrp->e_phentsize = 0;
4863 i_ehdrp->e_phnum = 0;
4865 /* Each bfd section is section header entry. */
4866 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4867 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4869 /* If we're building an executable, we'll need a program header table. */
4870 if (abfd->flags & EXEC_P)
4871 /* It all happens later. */
4873 else
4875 i_ehdrp->e_phentsize = 0;
4876 i_phdrp = 0;
4877 i_ehdrp->e_phoff = 0;
4880 elf_tdata (abfd)->symtab_hdr.sh_name =
4881 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4882 elf_tdata (abfd)->strtab_hdr.sh_name =
4883 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4884 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4885 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4886 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4887 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4888 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4889 return FALSE;
4891 return TRUE;
4894 /* Assign file positions for all the reloc sections which are not part
4895 of the loadable file image. */
4897 void
4898 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4900 file_ptr off;
4901 unsigned int i, num_sec;
4902 Elf_Internal_Shdr **shdrpp;
4904 off = elf_tdata (abfd)->next_file_pos;
4906 num_sec = elf_numsections (abfd);
4907 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4909 Elf_Internal_Shdr *shdrp;
4911 shdrp = *shdrpp;
4912 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4913 && shdrp->sh_offset == -1)
4914 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4917 elf_tdata (abfd)->next_file_pos = off;
4920 bfd_boolean
4921 _bfd_elf_write_object_contents (bfd *abfd)
4923 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4924 Elf_Internal_Ehdr *i_ehdrp;
4925 Elf_Internal_Shdr **i_shdrp;
4926 bfd_boolean failed;
4927 unsigned int count, num_sec;
4929 if (! abfd->output_has_begun
4930 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4931 return FALSE;
4933 i_shdrp = elf_elfsections (abfd);
4934 i_ehdrp = elf_elfheader (abfd);
4936 failed = FALSE;
4937 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4938 if (failed)
4939 return FALSE;
4941 _bfd_elf_assign_file_positions_for_relocs (abfd);
4943 /* After writing the headers, we need to write the sections too... */
4944 num_sec = elf_numsections (abfd);
4945 for (count = 1; count < num_sec; count++)
4947 if (bed->elf_backend_section_processing)
4948 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4949 if (i_shdrp[count]->contents)
4951 bfd_size_type amt = i_shdrp[count]->sh_size;
4953 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4954 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4955 return FALSE;
4959 /* Write out the section header names. */
4960 if (elf_shstrtab (abfd) != NULL
4961 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4962 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4963 return FALSE;
4965 if (bed->elf_backend_final_write_processing)
4966 (*bed->elf_backend_final_write_processing) (abfd,
4967 elf_tdata (abfd)->linker);
4969 if (!bed->s->write_shdrs_and_ehdr (abfd))
4970 return FALSE;
4972 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4973 if (elf_tdata (abfd)->after_write_object_contents)
4974 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
4976 return TRUE;
4979 bfd_boolean
4980 _bfd_elf_write_corefile_contents (bfd *abfd)
4982 /* Hopefully this can be done just like an object file. */
4983 return _bfd_elf_write_object_contents (abfd);
4986 /* Given a section, search the header to find them. */
4988 unsigned int
4989 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4991 const struct elf_backend_data *bed;
4992 unsigned int index;
4994 if (elf_section_data (asect) != NULL
4995 && elf_section_data (asect)->this_idx != 0)
4996 return elf_section_data (asect)->this_idx;
4998 if (bfd_is_abs_section (asect))
4999 index = SHN_ABS;
5000 else if (bfd_is_com_section (asect))
5001 index = SHN_COMMON;
5002 else if (bfd_is_und_section (asect))
5003 index = SHN_UNDEF;
5004 else
5005 index = SHN_BAD;
5007 bed = get_elf_backend_data (abfd);
5008 if (bed->elf_backend_section_from_bfd_section)
5010 int retval = index;
5012 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5013 return retval;
5016 if (index == SHN_BAD)
5017 bfd_set_error (bfd_error_nonrepresentable_section);
5019 return index;
5022 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5023 on error. */
5026 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5028 asymbol *asym_ptr = *asym_ptr_ptr;
5029 int idx;
5030 flagword flags = asym_ptr->flags;
5032 /* When gas creates relocations against local labels, it creates its
5033 own symbol for the section, but does put the symbol into the
5034 symbol chain, so udata is 0. When the linker is generating
5035 relocatable output, this section symbol may be for one of the
5036 input sections rather than the output section. */
5037 if (asym_ptr->udata.i == 0
5038 && (flags & BSF_SECTION_SYM)
5039 && asym_ptr->section)
5041 asection *sec;
5042 int indx;
5044 sec = asym_ptr->section;
5045 if (sec->owner != abfd && sec->output_section != NULL)
5046 sec = sec->output_section;
5047 if (sec->owner == abfd
5048 && (indx = sec->index) < elf_num_section_syms (abfd)
5049 && elf_section_syms (abfd)[indx] != NULL)
5050 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5053 idx = asym_ptr->udata.i;
5055 if (idx == 0)
5057 /* This case can occur when using --strip-symbol on a symbol
5058 which is used in a relocation entry. */
5059 (*_bfd_error_handler)
5060 (_("%B: symbol `%s' required but not present"),
5061 abfd, bfd_asymbol_name (asym_ptr));
5062 bfd_set_error (bfd_error_no_symbols);
5063 return -1;
5066 #if DEBUG & 4
5068 fprintf (stderr,
5069 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5070 (long) asym_ptr, asym_ptr->name, idx, flags,
5071 elf_symbol_flags (flags));
5072 fflush (stderr);
5074 #endif
5076 return idx;
5079 /* Rewrite program header information. */
5081 static bfd_boolean
5082 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5084 Elf_Internal_Ehdr *iehdr;
5085 struct elf_segment_map *map;
5086 struct elf_segment_map *map_first;
5087 struct elf_segment_map **pointer_to_map;
5088 Elf_Internal_Phdr *segment;
5089 asection *section;
5090 unsigned int i;
5091 unsigned int num_segments;
5092 bfd_boolean phdr_included = FALSE;
5093 bfd_boolean p_paddr_valid;
5094 bfd_vma maxpagesize;
5095 struct elf_segment_map *phdr_adjust_seg = NULL;
5096 unsigned int phdr_adjust_num = 0;
5097 const struct elf_backend_data *bed;
5099 bed = get_elf_backend_data (ibfd);
5100 iehdr = elf_elfheader (ibfd);
5102 map_first = NULL;
5103 pointer_to_map = &map_first;
5105 num_segments = elf_elfheader (ibfd)->e_phnum;
5106 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5108 /* Returns the end address of the segment + 1. */
5109 #define SEGMENT_END(segment, start) \
5110 (start + (segment->p_memsz > segment->p_filesz \
5111 ? segment->p_memsz : segment->p_filesz))
5113 #define SECTION_SIZE(section, segment) \
5114 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5115 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5116 ? section->size : 0)
5118 /* Returns TRUE if the given section is contained within
5119 the given segment. VMA addresses are compared. */
5120 #define IS_CONTAINED_BY_VMA(section, segment) \
5121 (section->vma >= segment->p_vaddr \
5122 && (section->vma + SECTION_SIZE (section, segment) \
5123 <= (SEGMENT_END (segment, segment->p_vaddr))))
5125 /* Returns TRUE if the given section is contained within
5126 the given segment. LMA addresses are compared. */
5127 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5128 (section->lma >= base \
5129 && (section->lma + SECTION_SIZE (section, segment) \
5130 <= SEGMENT_END (segment, base)))
5132 /* Handle PT_NOTE segment. */
5133 #define IS_NOTE(p, s) \
5134 (p->p_type == PT_NOTE \
5135 && elf_section_type (s) == SHT_NOTE \
5136 && (bfd_vma) s->filepos >= p->p_offset \
5137 && ((bfd_vma) s->filepos + s->size \
5138 <= p->p_offset + p->p_filesz))
5140 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5141 etc. */
5142 #define IS_COREFILE_NOTE(p, s) \
5143 (IS_NOTE (p, s) \
5144 && bfd_get_format (ibfd) == bfd_core \
5145 && s->vma == 0 \
5146 && s->lma == 0)
5148 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5149 linker, which generates a PT_INTERP section with p_vaddr and
5150 p_memsz set to 0. */
5151 #define IS_SOLARIS_PT_INTERP(p, s) \
5152 (p->p_vaddr == 0 \
5153 && p->p_paddr == 0 \
5154 && p->p_memsz == 0 \
5155 && p->p_filesz > 0 \
5156 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5157 && s->size > 0 \
5158 && (bfd_vma) s->filepos >= p->p_offset \
5159 && ((bfd_vma) s->filepos + s->size \
5160 <= p->p_offset + p->p_filesz))
5162 /* Decide if the given section should be included in the given segment.
5163 A section will be included if:
5164 1. It is within the address space of the segment -- we use the LMA
5165 if that is set for the segment and the VMA otherwise,
5166 2. It is an allocated section or a NOTE section in a PT_NOTE
5167 segment.
5168 3. There is an output section associated with it,
5169 4. The section has not already been allocated to a previous segment.
5170 5. PT_GNU_STACK segments do not include any sections.
5171 6. PT_TLS segment includes only SHF_TLS sections.
5172 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5173 8. PT_DYNAMIC should not contain empty sections at the beginning
5174 (with the possible exception of .dynamic). */
5175 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5176 ((((segment->p_paddr \
5177 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5178 : IS_CONTAINED_BY_VMA (section, segment)) \
5179 && (section->flags & SEC_ALLOC) != 0) \
5180 || IS_NOTE (segment, section)) \
5181 && segment->p_type != PT_GNU_STACK \
5182 && (segment->p_type != PT_TLS \
5183 || (section->flags & SEC_THREAD_LOCAL)) \
5184 && (segment->p_type == PT_LOAD \
5185 || segment->p_type == PT_TLS \
5186 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5187 && (segment->p_type != PT_DYNAMIC \
5188 || SECTION_SIZE (section, segment) > 0 \
5189 || (segment->p_paddr \
5190 ? segment->p_paddr != section->lma \
5191 : segment->p_vaddr != section->vma) \
5192 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5193 == 0)) \
5194 && !section->segment_mark)
5196 /* If the output section of a section in the input segment is NULL,
5197 it is removed from the corresponding output segment. */
5198 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5199 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5200 && section->output_section != NULL)
5202 /* Returns TRUE iff seg1 starts after the end of seg2. */
5203 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5204 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5206 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5207 their VMA address ranges and their LMA address ranges overlap.
5208 It is possible to have overlapping VMA ranges without overlapping LMA
5209 ranges. RedBoot images for example can have both .data and .bss mapped
5210 to the same VMA range, but with the .data section mapped to a different
5211 LMA. */
5212 #define SEGMENT_OVERLAPS(seg1, seg2) \
5213 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5214 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5215 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5216 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5218 /* Initialise the segment mark field. */
5219 for (section = ibfd->sections; section != NULL; section = section->next)
5220 section->segment_mark = FALSE;
5222 /* The Solaris linker creates program headers in which all the
5223 p_paddr fields are zero. When we try to objcopy or strip such a
5224 file, we get confused. Check for this case, and if we find it
5225 don't set the p_paddr_valid fields. */
5226 p_paddr_valid = FALSE;
5227 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5228 i < num_segments;
5229 i++, segment++)
5230 if (segment->p_paddr != 0)
5232 p_paddr_valid = TRUE;
5233 break;
5236 /* Scan through the segments specified in the program header
5237 of the input BFD. For this first scan we look for overlaps
5238 in the loadable segments. These can be created by weird
5239 parameters to objcopy. Also, fix some solaris weirdness. */
5240 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5241 i < num_segments;
5242 i++, segment++)
5244 unsigned int j;
5245 Elf_Internal_Phdr *segment2;
5247 if (segment->p_type == PT_INTERP)
5248 for (section = ibfd->sections; section; section = section->next)
5249 if (IS_SOLARIS_PT_INTERP (segment, section))
5251 /* Mininal change so that the normal section to segment
5252 assignment code will work. */
5253 segment->p_vaddr = section->vma;
5254 break;
5257 if (segment->p_type != PT_LOAD)
5259 /* Remove PT_GNU_RELRO segment. */
5260 if (segment->p_type == PT_GNU_RELRO)
5261 segment->p_type = PT_NULL;
5262 continue;
5265 /* Determine if this segment overlaps any previous segments. */
5266 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5268 bfd_signed_vma extra_length;
5270 if (segment2->p_type != PT_LOAD
5271 || !SEGMENT_OVERLAPS (segment, segment2))
5272 continue;
5274 /* Merge the two segments together. */
5275 if (segment2->p_vaddr < segment->p_vaddr)
5277 /* Extend SEGMENT2 to include SEGMENT and then delete
5278 SEGMENT. */
5279 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5280 - SEGMENT_END (segment2, segment2->p_vaddr));
5282 if (extra_length > 0)
5284 segment2->p_memsz += extra_length;
5285 segment2->p_filesz += extra_length;
5288 segment->p_type = PT_NULL;
5290 /* Since we have deleted P we must restart the outer loop. */
5291 i = 0;
5292 segment = elf_tdata (ibfd)->phdr;
5293 break;
5295 else
5297 /* Extend SEGMENT to include SEGMENT2 and then delete
5298 SEGMENT2. */
5299 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5300 - SEGMENT_END (segment, segment->p_vaddr));
5302 if (extra_length > 0)
5304 segment->p_memsz += extra_length;
5305 segment->p_filesz += extra_length;
5308 segment2->p_type = PT_NULL;
5313 /* The second scan attempts to assign sections to segments. */
5314 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5315 i < num_segments;
5316 i++, segment++)
5318 unsigned int section_count;
5319 asection **sections;
5320 asection *output_section;
5321 unsigned int isec;
5322 bfd_vma matching_lma;
5323 bfd_vma suggested_lma;
5324 unsigned int j;
5325 bfd_size_type amt;
5326 asection *first_section;
5327 bfd_boolean first_matching_lma;
5328 bfd_boolean first_suggested_lma;
5330 if (segment->p_type == PT_NULL)
5331 continue;
5333 first_section = NULL;
5334 /* Compute how many sections might be placed into this segment. */
5335 for (section = ibfd->sections, section_count = 0;
5336 section != NULL;
5337 section = section->next)
5339 /* Find the first section in the input segment, which may be
5340 removed from the corresponding output segment. */
5341 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5343 if (first_section == NULL)
5344 first_section = section;
5345 if (section->output_section != NULL)
5346 ++section_count;
5350 /* Allocate a segment map big enough to contain
5351 all of the sections we have selected. */
5352 amt = sizeof (struct elf_segment_map);
5353 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5354 map = bfd_zalloc (obfd, amt);
5355 if (map == NULL)
5356 return FALSE;
5358 /* Initialise the fields of the segment map. Default to
5359 using the physical address of the segment in the input BFD. */
5360 map->next = NULL;
5361 map->p_type = segment->p_type;
5362 map->p_flags = segment->p_flags;
5363 map->p_flags_valid = 1;
5365 /* If the first section in the input segment is removed, there is
5366 no need to preserve segment physical address in the corresponding
5367 output segment. */
5368 if (!first_section || first_section->output_section != NULL)
5370 map->p_paddr = segment->p_paddr;
5371 map->p_paddr_valid = p_paddr_valid;
5374 /* Determine if this segment contains the ELF file header
5375 and if it contains the program headers themselves. */
5376 map->includes_filehdr = (segment->p_offset == 0
5377 && segment->p_filesz >= iehdr->e_ehsize);
5378 map->includes_phdrs = 0;
5380 if (!phdr_included || segment->p_type != PT_LOAD)
5382 map->includes_phdrs =
5383 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5384 && (segment->p_offset + segment->p_filesz
5385 >= ((bfd_vma) iehdr->e_phoff
5386 + iehdr->e_phnum * iehdr->e_phentsize)));
5388 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5389 phdr_included = TRUE;
5392 if (section_count == 0)
5394 /* Special segments, such as the PT_PHDR segment, may contain
5395 no sections, but ordinary, loadable segments should contain
5396 something. They are allowed by the ELF spec however, so only
5397 a warning is produced. */
5398 if (segment->p_type == PT_LOAD)
5399 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5400 " detected, is this intentional ?\n"),
5401 ibfd);
5403 map->count = 0;
5404 *pointer_to_map = map;
5405 pointer_to_map = &map->next;
5407 continue;
5410 /* Now scan the sections in the input BFD again and attempt
5411 to add their corresponding output sections to the segment map.
5412 The problem here is how to handle an output section which has
5413 been moved (ie had its LMA changed). There are four possibilities:
5415 1. None of the sections have been moved.
5416 In this case we can continue to use the segment LMA from the
5417 input BFD.
5419 2. All of the sections have been moved by the same amount.
5420 In this case we can change the segment's LMA to match the LMA
5421 of the first section.
5423 3. Some of the sections have been moved, others have not.
5424 In this case those sections which have not been moved can be
5425 placed in the current segment which will have to have its size,
5426 and possibly its LMA changed, and a new segment or segments will
5427 have to be created to contain the other sections.
5429 4. The sections have been moved, but not by the same amount.
5430 In this case we can change the segment's LMA to match the LMA
5431 of the first section and we will have to create a new segment
5432 or segments to contain the other sections.
5434 In order to save time, we allocate an array to hold the section
5435 pointers that we are interested in. As these sections get assigned
5436 to a segment, they are removed from this array. */
5438 sections = bfd_malloc2 (section_count, sizeof (asection *));
5439 if (sections == NULL)
5440 return FALSE;
5442 /* Step One: Scan for segment vs section LMA conflicts.
5443 Also add the sections to the section array allocated above.
5444 Also add the sections to the current segment. In the common
5445 case, where the sections have not been moved, this means that
5446 we have completely filled the segment, and there is nothing
5447 more to do. */
5448 isec = 0;
5449 matching_lma = 0;
5450 suggested_lma = 0;
5451 first_matching_lma = TRUE;
5452 first_suggested_lma = TRUE;
5454 for (section = ibfd->sections;
5455 section != NULL;
5456 section = section->next)
5457 if (section == first_section)
5458 break;
5460 for (j = 0; section != NULL; section = section->next)
5462 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5464 output_section = section->output_section;
5466 sections[j++] = section;
5468 /* The Solaris native linker always sets p_paddr to 0.
5469 We try to catch that case here, and set it to the
5470 correct value. Note - some backends require that
5471 p_paddr be left as zero. */
5472 if (!p_paddr_valid
5473 && segment->p_vaddr != 0
5474 && !bed->want_p_paddr_set_to_zero
5475 && isec == 0
5476 && output_section->lma != 0
5477 && output_section->vma == (segment->p_vaddr
5478 + (map->includes_filehdr
5479 ? iehdr->e_ehsize
5480 : 0)
5481 + (map->includes_phdrs
5482 ? (iehdr->e_phnum
5483 * iehdr->e_phentsize)
5484 : 0)))
5485 map->p_paddr = segment->p_vaddr;
5487 /* Match up the physical address of the segment with the
5488 LMA address of the output section. */
5489 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5490 || IS_COREFILE_NOTE (segment, section)
5491 || (bed->want_p_paddr_set_to_zero
5492 && IS_CONTAINED_BY_VMA (output_section, segment)))
5494 if (first_matching_lma || output_section->lma < matching_lma)
5496 matching_lma = output_section->lma;
5497 first_matching_lma = FALSE;
5500 /* We assume that if the section fits within the segment
5501 then it does not overlap any other section within that
5502 segment. */
5503 map->sections[isec++] = output_section;
5505 else if (first_suggested_lma)
5507 suggested_lma = output_section->lma;
5508 first_suggested_lma = FALSE;
5511 if (j == section_count)
5512 break;
5516 BFD_ASSERT (j == section_count);
5518 /* Step Two: Adjust the physical address of the current segment,
5519 if necessary. */
5520 if (isec == section_count)
5522 /* All of the sections fitted within the segment as currently
5523 specified. This is the default case. Add the segment to
5524 the list of built segments and carry on to process the next
5525 program header in the input BFD. */
5526 map->count = section_count;
5527 *pointer_to_map = map;
5528 pointer_to_map = &map->next;
5530 if (p_paddr_valid
5531 && !bed->want_p_paddr_set_to_zero
5532 && matching_lma != map->p_paddr
5533 && !map->includes_filehdr
5534 && !map->includes_phdrs)
5535 /* There is some padding before the first section in the
5536 segment. So, we must account for that in the output
5537 segment's vma. */
5538 map->p_vaddr_offset = matching_lma - map->p_paddr;
5540 free (sections);
5541 continue;
5543 else
5545 if (!first_matching_lma)
5547 /* At least one section fits inside the current segment.
5548 Keep it, but modify its physical address to match the
5549 LMA of the first section that fitted. */
5550 map->p_paddr = matching_lma;
5552 else
5554 /* None of the sections fitted inside the current segment.
5555 Change the current segment's physical address to match
5556 the LMA of the first section. */
5557 map->p_paddr = suggested_lma;
5560 /* Offset the segment physical address from the lma
5561 to allow for space taken up by elf headers. */
5562 if (map->includes_filehdr)
5563 map->p_paddr -= iehdr->e_ehsize;
5565 if (map->includes_phdrs)
5567 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5569 /* iehdr->e_phnum is just an estimate of the number
5570 of program headers that we will need. Make a note
5571 here of the number we used and the segment we chose
5572 to hold these headers, so that we can adjust the
5573 offset when we know the correct value. */
5574 phdr_adjust_num = iehdr->e_phnum;
5575 phdr_adjust_seg = map;
5579 /* Step Three: Loop over the sections again, this time assigning
5580 those that fit to the current segment and removing them from the
5581 sections array; but making sure not to leave large gaps. Once all
5582 possible sections have been assigned to the current segment it is
5583 added to the list of built segments and if sections still remain
5584 to be assigned, a new segment is constructed before repeating
5585 the loop. */
5586 isec = 0;
5589 map->count = 0;
5590 suggested_lma = 0;
5591 first_suggested_lma = TRUE;
5593 /* Fill the current segment with sections that fit. */
5594 for (j = 0; j < section_count; j++)
5596 section = sections[j];
5598 if (section == NULL)
5599 continue;
5601 output_section = section->output_section;
5603 BFD_ASSERT (output_section != NULL);
5605 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5606 || IS_COREFILE_NOTE (segment, section))
5608 if (map->count == 0)
5610 /* If the first section in a segment does not start at
5611 the beginning of the segment, then something is
5612 wrong. */
5613 if (output_section->lma
5614 != (map->p_paddr
5615 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5616 + (map->includes_phdrs
5617 ? iehdr->e_phnum * iehdr->e_phentsize
5618 : 0)))
5619 abort ();
5621 else
5623 asection *prev_sec;
5625 prev_sec = map->sections[map->count - 1];
5627 /* If the gap between the end of the previous section
5628 and the start of this section is more than
5629 maxpagesize then we need to start a new segment. */
5630 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5631 maxpagesize)
5632 < BFD_ALIGN (output_section->lma, maxpagesize))
5633 || (prev_sec->lma + prev_sec->size
5634 > output_section->lma))
5636 if (first_suggested_lma)
5638 suggested_lma = output_section->lma;
5639 first_suggested_lma = FALSE;
5642 continue;
5646 map->sections[map->count++] = output_section;
5647 ++isec;
5648 sections[j] = NULL;
5649 section->segment_mark = TRUE;
5651 else if (first_suggested_lma)
5653 suggested_lma = output_section->lma;
5654 first_suggested_lma = FALSE;
5658 BFD_ASSERT (map->count > 0);
5660 /* Add the current segment to the list of built segments. */
5661 *pointer_to_map = map;
5662 pointer_to_map = &map->next;
5664 if (isec < section_count)
5666 /* We still have not allocated all of the sections to
5667 segments. Create a new segment here, initialise it
5668 and carry on looping. */
5669 amt = sizeof (struct elf_segment_map);
5670 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5671 map = bfd_alloc (obfd, amt);
5672 if (map == NULL)
5674 free (sections);
5675 return FALSE;
5678 /* Initialise the fields of the segment map. Set the physical
5679 physical address to the LMA of the first section that has
5680 not yet been assigned. */
5681 map->next = NULL;
5682 map->p_type = segment->p_type;
5683 map->p_flags = segment->p_flags;
5684 map->p_flags_valid = 1;
5685 map->p_paddr = suggested_lma;
5686 map->p_paddr_valid = p_paddr_valid;
5687 map->includes_filehdr = 0;
5688 map->includes_phdrs = 0;
5691 while (isec < section_count);
5693 free (sections);
5696 elf_tdata (obfd)->segment_map = map_first;
5698 /* If we had to estimate the number of program headers that were
5699 going to be needed, then check our estimate now and adjust
5700 the offset if necessary. */
5701 if (phdr_adjust_seg != NULL)
5703 unsigned int count;
5705 for (count = 0, map = map_first; map != NULL; map = map->next)
5706 count++;
5708 if (count > phdr_adjust_num)
5709 phdr_adjust_seg->p_paddr
5710 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5713 #undef SEGMENT_END
5714 #undef SECTION_SIZE
5715 #undef IS_CONTAINED_BY_VMA
5716 #undef IS_CONTAINED_BY_LMA
5717 #undef IS_NOTE
5718 #undef IS_COREFILE_NOTE
5719 #undef IS_SOLARIS_PT_INTERP
5720 #undef IS_SECTION_IN_INPUT_SEGMENT
5721 #undef INCLUDE_SECTION_IN_SEGMENT
5722 #undef SEGMENT_AFTER_SEGMENT
5723 #undef SEGMENT_OVERLAPS
5724 return TRUE;
5727 /* Copy ELF program header information. */
5729 static bfd_boolean
5730 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5732 Elf_Internal_Ehdr *iehdr;
5733 struct elf_segment_map *map;
5734 struct elf_segment_map *map_first;
5735 struct elf_segment_map **pointer_to_map;
5736 Elf_Internal_Phdr *segment;
5737 unsigned int i;
5738 unsigned int num_segments;
5739 bfd_boolean phdr_included = FALSE;
5740 bfd_boolean p_paddr_valid;
5742 iehdr = elf_elfheader (ibfd);
5744 map_first = NULL;
5745 pointer_to_map = &map_first;
5747 /* If all the segment p_paddr fields are zero, don't set
5748 map->p_paddr_valid. */
5749 p_paddr_valid = FALSE;
5750 num_segments = elf_elfheader (ibfd)->e_phnum;
5751 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5752 i < num_segments;
5753 i++, segment++)
5754 if (segment->p_paddr != 0)
5756 p_paddr_valid = TRUE;
5757 break;
5760 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5761 i < num_segments;
5762 i++, segment++)
5764 asection *section;
5765 unsigned int section_count;
5766 bfd_size_type amt;
5767 Elf_Internal_Shdr *this_hdr;
5768 asection *first_section = NULL;
5769 asection *lowest_section = NULL;
5771 /* Compute how many sections are in this segment. */
5772 for (section = ibfd->sections, section_count = 0;
5773 section != NULL;
5774 section = section->next)
5776 this_hdr = &(elf_section_data(section)->this_hdr);
5777 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5779 if (!first_section)
5780 first_section = lowest_section = section;
5781 if (section->lma < lowest_section->lma)
5782 lowest_section = section;
5783 section_count++;
5787 /* Allocate a segment map big enough to contain
5788 all of the sections we have selected. */
5789 amt = sizeof (struct elf_segment_map);
5790 if (section_count != 0)
5791 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5792 map = bfd_zalloc (obfd, amt);
5793 if (map == NULL)
5794 return FALSE;
5796 /* Initialize the fields of the output segment map with the
5797 input segment. */
5798 map->next = NULL;
5799 map->p_type = segment->p_type;
5800 map->p_flags = segment->p_flags;
5801 map->p_flags_valid = 1;
5802 map->p_paddr = segment->p_paddr;
5803 map->p_paddr_valid = p_paddr_valid;
5804 map->p_align = segment->p_align;
5805 map->p_align_valid = 1;
5806 map->p_vaddr_offset = 0;
5808 if (map->p_type == PT_GNU_RELRO
5809 && segment->p_filesz == segment->p_memsz)
5811 /* The PT_GNU_RELRO segment may contain the first a few
5812 bytes in the .got.plt section even if the whole .got.plt
5813 section isn't in the PT_GNU_RELRO segment. We won't
5814 change the size of the PT_GNU_RELRO segment. */
5815 map->p_size = segment->p_filesz;
5816 map->p_size_valid = 1;
5819 /* Determine if this segment contains the ELF file header
5820 and if it contains the program headers themselves. */
5821 map->includes_filehdr = (segment->p_offset == 0
5822 && segment->p_filesz >= iehdr->e_ehsize);
5824 map->includes_phdrs = 0;
5825 if (! phdr_included || segment->p_type != PT_LOAD)
5827 map->includes_phdrs =
5828 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5829 && (segment->p_offset + segment->p_filesz
5830 >= ((bfd_vma) iehdr->e_phoff
5831 + iehdr->e_phnum * iehdr->e_phentsize)));
5833 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5834 phdr_included = TRUE;
5837 if (!map->includes_phdrs
5838 && !map->includes_filehdr
5839 && map->p_paddr_valid)
5840 /* There is some other padding before the first section. */
5841 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5842 - segment->p_paddr);
5844 if (section_count != 0)
5846 unsigned int isec = 0;
5848 for (section = first_section;
5849 section != NULL;
5850 section = section->next)
5852 this_hdr = &(elf_section_data(section)->this_hdr);
5853 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5855 map->sections[isec++] = section->output_section;
5856 if (isec == section_count)
5857 break;
5862 map->count = section_count;
5863 *pointer_to_map = map;
5864 pointer_to_map = &map->next;
5867 elf_tdata (obfd)->segment_map = map_first;
5868 return TRUE;
5871 /* Copy private BFD data. This copies or rewrites ELF program header
5872 information. */
5874 static bfd_boolean
5875 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5877 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5878 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5879 return TRUE;
5881 if (elf_tdata (ibfd)->phdr == NULL)
5882 return TRUE;
5884 if (ibfd->xvec == obfd->xvec)
5886 /* Check to see if any sections in the input BFD
5887 covered by ELF program header have changed. */
5888 Elf_Internal_Phdr *segment;
5889 asection *section, *osec;
5890 unsigned int i, num_segments;
5891 Elf_Internal_Shdr *this_hdr;
5892 const struct elf_backend_data *bed;
5894 bed = get_elf_backend_data (ibfd);
5896 /* Regenerate the segment map if p_paddr is set to 0. */
5897 if (bed->want_p_paddr_set_to_zero)
5898 goto rewrite;
5900 /* Initialize the segment mark field. */
5901 for (section = obfd->sections; section != NULL;
5902 section = section->next)
5903 section->segment_mark = FALSE;
5905 num_segments = elf_elfheader (ibfd)->e_phnum;
5906 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5907 i < num_segments;
5908 i++, segment++)
5910 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5911 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5912 which severly confuses things, so always regenerate the segment
5913 map in this case. */
5914 if (segment->p_paddr == 0
5915 && segment->p_memsz == 0
5916 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5917 goto rewrite;
5919 for (section = ibfd->sections;
5920 section != NULL; section = section->next)
5922 /* We mark the output section so that we know it comes
5923 from the input BFD. */
5924 osec = section->output_section;
5925 if (osec)
5926 osec->segment_mark = TRUE;
5928 /* Check if this section is covered by the segment. */
5929 this_hdr = &(elf_section_data(section)->this_hdr);
5930 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5932 /* FIXME: Check if its output section is changed or
5933 removed. What else do we need to check? */
5934 if (osec == NULL
5935 || section->flags != osec->flags
5936 || section->lma != osec->lma
5937 || section->vma != osec->vma
5938 || section->size != osec->size
5939 || section->rawsize != osec->rawsize
5940 || section->alignment_power != osec->alignment_power)
5941 goto rewrite;
5946 /* Check to see if any output section do not come from the
5947 input BFD. */
5948 for (section = obfd->sections; section != NULL;
5949 section = section->next)
5951 if (section->segment_mark == FALSE)
5952 goto rewrite;
5953 else
5954 section->segment_mark = FALSE;
5957 return copy_elf_program_header (ibfd, obfd);
5960 rewrite:
5961 return rewrite_elf_program_header (ibfd, obfd);
5964 /* Initialize private output section information from input section. */
5966 bfd_boolean
5967 _bfd_elf_init_private_section_data (bfd *ibfd,
5968 asection *isec,
5969 bfd *obfd,
5970 asection *osec,
5971 struct bfd_link_info *link_info)
5974 Elf_Internal_Shdr *ihdr, *ohdr;
5975 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
5977 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5978 || obfd->xvec->flavour != bfd_target_elf_flavour)
5979 return TRUE;
5981 /* Don't copy the output ELF section type from input if the
5982 output BFD section flags have been set to something different.
5983 elf_fake_sections will set ELF section type based on BFD
5984 section flags. */
5985 if (elf_section_type (osec) == SHT_NULL
5986 && (osec->flags == isec->flags || !osec->flags))
5987 elf_section_type (osec) = elf_section_type (isec);
5989 /* FIXME: Is this correct for all OS/PROC specific flags? */
5990 elf_section_flags (osec) |= (elf_section_flags (isec)
5991 & (SHF_MASKOS | SHF_MASKPROC));
5993 /* Set things up for objcopy and relocatable link. The output
5994 SHT_GROUP section will have its elf_next_in_group pointing back
5995 to the input group members. Ignore linker created group section.
5996 See elfNN_ia64_object_p in elfxx-ia64.c. */
5997 if (need_group)
5999 if (elf_sec_group (isec) == NULL
6000 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6002 if (elf_section_flags (isec) & SHF_GROUP)
6003 elf_section_flags (osec) |= SHF_GROUP;
6004 elf_next_in_group (osec) = elf_next_in_group (isec);
6005 elf_group_name (osec) = elf_group_name (isec);
6009 ihdr = &elf_section_data (isec)->this_hdr;
6011 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6012 don't use the output section of the linked-to section since it
6013 may be NULL at this point. */
6014 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6016 ohdr = &elf_section_data (osec)->this_hdr;
6017 ohdr->sh_flags |= SHF_LINK_ORDER;
6018 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6021 osec->use_rela_p = isec->use_rela_p;
6023 return TRUE;
6026 /* Copy private section information. This copies over the entsize
6027 field, and sometimes the info field. */
6029 bfd_boolean
6030 _bfd_elf_copy_private_section_data (bfd *ibfd,
6031 asection *isec,
6032 bfd *obfd,
6033 asection *osec)
6035 Elf_Internal_Shdr *ihdr, *ohdr;
6037 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6038 || obfd->xvec->flavour != bfd_target_elf_flavour)
6039 return TRUE;
6041 ihdr = &elf_section_data (isec)->this_hdr;
6042 ohdr = &elf_section_data (osec)->this_hdr;
6044 ohdr->sh_entsize = ihdr->sh_entsize;
6046 if (ihdr->sh_type == SHT_SYMTAB
6047 || ihdr->sh_type == SHT_DYNSYM
6048 || ihdr->sh_type == SHT_GNU_verneed
6049 || ihdr->sh_type == SHT_GNU_verdef)
6050 ohdr->sh_info = ihdr->sh_info;
6052 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6053 NULL);
6056 /* Copy private header information. */
6058 bfd_boolean
6059 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6061 asection *isec;
6063 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6064 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6065 return TRUE;
6067 /* Copy over private BFD data if it has not already been copied.
6068 This must be done here, rather than in the copy_private_bfd_data
6069 entry point, because the latter is called after the section
6070 contents have been set, which means that the program headers have
6071 already been worked out. */
6072 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6074 if (! copy_private_bfd_data (ibfd, obfd))
6075 return FALSE;
6078 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6079 but this might be wrong if we deleted the group section. */
6080 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6081 if (elf_section_type (isec) == SHT_GROUP
6082 && isec->output_section == NULL)
6084 asection *first = elf_next_in_group (isec);
6085 asection *s = first;
6086 while (s != NULL)
6088 if (s->output_section != NULL)
6090 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6091 elf_group_name (s->output_section) = NULL;
6093 s = elf_next_in_group (s);
6094 if (s == first)
6095 break;
6099 return TRUE;
6102 /* Copy private symbol information. If this symbol is in a section
6103 which we did not map into a BFD section, try to map the section
6104 index correctly. We use special macro definitions for the mapped
6105 section indices; these definitions are interpreted by the
6106 swap_out_syms function. */
6108 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6109 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6110 #define MAP_STRTAB (SHN_HIOS + 3)
6111 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6112 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6114 bfd_boolean
6115 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
6116 asymbol *isymarg,
6117 bfd *obfd,
6118 asymbol *osymarg)
6120 elf_symbol_type *isym, *osym;
6122 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6123 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6124 return TRUE;
6126 isym = elf_symbol_from (ibfd, isymarg);
6127 osym = elf_symbol_from (obfd, osymarg);
6129 if (isym != NULL
6130 && isym->internal_elf_sym.st_shndx != 0
6131 && osym != NULL
6132 && bfd_is_abs_section (isym->symbol.section))
6134 unsigned int shndx;
6136 shndx = isym->internal_elf_sym.st_shndx;
6137 if (shndx == elf_onesymtab (ibfd))
6138 shndx = MAP_ONESYMTAB;
6139 else if (shndx == elf_dynsymtab (ibfd))
6140 shndx = MAP_DYNSYMTAB;
6141 else if (shndx == elf_tdata (ibfd)->strtab_section)
6142 shndx = MAP_STRTAB;
6143 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6144 shndx = MAP_SHSTRTAB;
6145 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6146 shndx = MAP_SYM_SHNDX;
6147 osym->internal_elf_sym.st_shndx = shndx;
6150 return TRUE;
6153 /* Swap out the symbols. */
6155 static bfd_boolean
6156 swap_out_syms (bfd *abfd,
6157 struct bfd_strtab_hash **sttp,
6158 int relocatable_p)
6160 const struct elf_backend_data *bed;
6161 int symcount;
6162 asymbol **syms;
6163 struct bfd_strtab_hash *stt;
6164 Elf_Internal_Shdr *symtab_hdr;
6165 Elf_Internal_Shdr *symtab_shndx_hdr;
6166 Elf_Internal_Shdr *symstrtab_hdr;
6167 bfd_byte *outbound_syms;
6168 bfd_byte *outbound_shndx;
6169 int idx;
6170 bfd_size_type amt;
6171 bfd_boolean name_local_sections;
6173 if (!elf_map_symbols (abfd))
6174 return FALSE;
6176 /* Dump out the symtabs. */
6177 stt = _bfd_elf_stringtab_init ();
6178 if (stt == NULL)
6179 return FALSE;
6181 bed = get_elf_backend_data (abfd);
6182 symcount = bfd_get_symcount (abfd);
6183 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6184 symtab_hdr->sh_type = SHT_SYMTAB;
6185 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6186 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6187 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6188 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6190 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6191 symstrtab_hdr->sh_type = SHT_STRTAB;
6193 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
6194 if (outbound_syms == NULL)
6196 _bfd_stringtab_free (stt);
6197 return FALSE;
6199 symtab_hdr->contents = outbound_syms;
6201 outbound_shndx = NULL;
6202 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6203 if (symtab_shndx_hdr->sh_name != 0)
6205 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6206 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6207 sizeof (Elf_External_Sym_Shndx));
6208 if (outbound_shndx == NULL)
6210 _bfd_stringtab_free (stt);
6211 return FALSE;
6214 symtab_shndx_hdr->contents = outbound_shndx;
6215 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6216 symtab_shndx_hdr->sh_size = amt;
6217 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6218 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6221 /* Now generate the data (for "contents"). */
6223 /* Fill in zeroth symbol and swap it out. */
6224 Elf_Internal_Sym sym;
6225 sym.st_name = 0;
6226 sym.st_value = 0;
6227 sym.st_size = 0;
6228 sym.st_info = 0;
6229 sym.st_other = 0;
6230 sym.st_shndx = SHN_UNDEF;
6231 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6232 outbound_syms += bed->s->sizeof_sym;
6233 if (outbound_shndx != NULL)
6234 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6237 name_local_sections
6238 = (bed->elf_backend_name_local_section_symbols
6239 && bed->elf_backend_name_local_section_symbols (abfd));
6241 syms = bfd_get_outsymbols (abfd);
6242 for (idx = 0; idx < symcount; idx++)
6244 Elf_Internal_Sym sym;
6245 bfd_vma value = syms[idx]->value;
6246 elf_symbol_type *type_ptr;
6247 flagword flags = syms[idx]->flags;
6248 int type;
6250 if (!name_local_sections
6251 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6253 /* Local section symbols have no name. */
6254 sym.st_name = 0;
6256 else
6258 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6259 syms[idx]->name,
6260 TRUE, FALSE);
6261 if (sym.st_name == (unsigned long) -1)
6263 _bfd_stringtab_free (stt);
6264 return FALSE;
6268 type_ptr = elf_symbol_from (abfd, syms[idx]);
6270 if ((flags & BSF_SECTION_SYM) == 0
6271 && bfd_is_com_section (syms[idx]->section))
6273 /* ELF common symbols put the alignment into the `value' field,
6274 and the size into the `size' field. This is backwards from
6275 how BFD handles it, so reverse it here. */
6276 sym.st_size = value;
6277 if (type_ptr == NULL
6278 || type_ptr->internal_elf_sym.st_value == 0)
6279 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6280 else
6281 sym.st_value = type_ptr->internal_elf_sym.st_value;
6282 sym.st_shndx = _bfd_elf_section_from_bfd_section
6283 (abfd, syms[idx]->section);
6285 else
6287 asection *sec = syms[idx]->section;
6288 unsigned int shndx;
6290 if (sec->output_section)
6292 value += sec->output_offset;
6293 sec = sec->output_section;
6296 /* Don't add in the section vma for relocatable output. */
6297 if (! relocatable_p)
6298 value += sec->vma;
6299 sym.st_value = value;
6300 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6302 if (bfd_is_abs_section (sec)
6303 && type_ptr != NULL
6304 && type_ptr->internal_elf_sym.st_shndx != 0)
6306 /* This symbol is in a real ELF section which we did
6307 not create as a BFD section. Undo the mapping done
6308 by copy_private_symbol_data. */
6309 shndx = type_ptr->internal_elf_sym.st_shndx;
6310 switch (shndx)
6312 case MAP_ONESYMTAB:
6313 shndx = elf_onesymtab (abfd);
6314 break;
6315 case MAP_DYNSYMTAB:
6316 shndx = elf_dynsymtab (abfd);
6317 break;
6318 case MAP_STRTAB:
6319 shndx = elf_tdata (abfd)->strtab_section;
6320 break;
6321 case MAP_SHSTRTAB:
6322 shndx = elf_tdata (abfd)->shstrtab_section;
6323 break;
6324 case MAP_SYM_SHNDX:
6325 shndx = elf_tdata (abfd)->symtab_shndx_section;
6326 break;
6327 default:
6328 break;
6331 else
6333 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6335 if (shndx == SHN_BAD)
6337 asection *sec2;
6339 /* Writing this would be a hell of a lot easier if
6340 we had some decent documentation on bfd, and
6341 knew what to expect of the library, and what to
6342 demand of applications. For example, it
6343 appears that `objcopy' might not set the
6344 section of a symbol to be a section that is
6345 actually in the output file. */
6346 sec2 = bfd_get_section_by_name (abfd, sec->name);
6347 if (sec2 == NULL)
6349 _bfd_error_handler (_("\
6350 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6351 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6352 sec->name);
6353 bfd_set_error (bfd_error_invalid_operation);
6354 _bfd_stringtab_free (stt);
6355 return FALSE;
6358 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6359 BFD_ASSERT (shndx != SHN_BAD);
6363 sym.st_shndx = shndx;
6366 if ((flags & BSF_THREAD_LOCAL) != 0)
6367 type = STT_TLS;
6368 else if ((flags & BSF_FUNCTION) != 0)
6369 type = STT_FUNC;
6370 else if ((flags & BSF_OBJECT) != 0)
6371 type = STT_OBJECT;
6372 else if ((flags & BSF_RELC) != 0)
6373 type = STT_RELC;
6374 else if ((flags & BSF_SRELC) != 0)
6375 type = STT_SRELC;
6376 else
6377 type = STT_NOTYPE;
6379 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6380 type = STT_TLS;
6382 /* Processor-specific types. */
6383 if (type_ptr != NULL
6384 && bed->elf_backend_get_symbol_type)
6385 type = ((*bed->elf_backend_get_symbol_type)
6386 (&type_ptr->internal_elf_sym, type));
6388 if (flags & BSF_SECTION_SYM)
6390 if (flags & BSF_GLOBAL)
6391 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6392 else
6393 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6395 else if (bfd_is_com_section (syms[idx]->section))
6397 #ifdef USE_STT_COMMON
6398 if (type == STT_OBJECT)
6399 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6400 else
6401 #else
6402 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6403 #endif
6405 else if (bfd_is_und_section (syms[idx]->section))
6406 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6407 ? STB_WEAK
6408 : STB_GLOBAL),
6409 type);
6410 else if (flags & BSF_FILE)
6411 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6412 else
6414 int bind = STB_LOCAL;
6416 if (flags & BSF_LOCAL)
6417 bind = STB_LOCAL;
6418 else if (flags & BSF_WEAK)
6419 bind = STB_WEAK;
6420 else if (flags & BSF_GLOBAL)
6421 bind = STB_GLOBAL;
6423 sym.st_info = ELF_ST_INFO (bind, type);
6426 if (type_ptr != NULL)
6427 sym.st_other = type_ptr->internal_elf_sym.st_other;
6428 else
6429 sym.st_other = 0;
6431 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6432 outbound_syms += bed->s->sizeof_sym;
6433 if (outbound_shndx != NULL)
6434 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6437 *sttp = stt;
6438 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6439 symstrtab_hdr->sh_type = SHT_STRTAB;
6441 symstrtab_hdr->sh_flags = 0;
6442 symstrtab_hdr->sh_addr = 0;
6443 symstrtab_hdr->sh_entsize = 0;
6444 symstrtab_hdr->sh_link = 0;
6445 symstrtab_hdr->sh_info = 0;
6446 symstrtab_hdr->sh_addralign = 1;
6448 return TRUE;
6451 /* Return the number of bytes required to hold the symtab vector.
6453 Note that we base it on the count plus 1, since we will null terminate
6454 the vector allocated based on this size. However, the ELF symbol table
6455 always has a dummy entry as symbol #0, so it ends up even. */
6457 long
6458 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6460 long symcount;
6461 long symtab_size;
6462 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6464 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6465 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6466 if (symcount > 0)
6467 symtab_size -= sizeof (asymbol *);
6469 return symtab_size;
6472 long
6473 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6475 long symcount;
6476 long symtab_size;
6477 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6479 if (elf_dynsymtab (abfd) == 0)
6481 bfd_set_error (bfd_error_invalid_operation);
6482 return -1;
6485 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6486 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6487 if (symcount > 0)
6488 symtab_size -= sizeof (asymbol *);
6490 return symtab_size;
6493 long
6494 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6495 sec_ptr asect)
6497 return (asect->reloc_count + 1) * sizeof (arelent *);
6500 /* Canonicalize the relocs. */
6502 long
6503 _bfd_elf_canonicalize_reloc (bfd *abfd,
6504 sec_ptr section,
6505 arelent **relptr,
6506 asymbol **symbols)
6508 arelent *tblptr;
6509 unsigned int i;
6510 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6512 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6513 return -1;
6515 tblptr = section->relocation;
6516 for (i = 0; i < section->reloc_count; i++)
6517 *relptr++ = tblptr++;
6519 *relptr = NULL;
6521 return section->reloc_count;
6524 long
6525 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6527 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6528 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6530 if (symcount >= 0)
6531 bfd_get_symcount (abfd) = symcount;
6532 return symcount;
6535 long
6536 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6537 asymbol **allocation)
6539 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6540 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6542 if (symcount >= 0)
6543 bfd_get_dynamic_symcount (abfd) = symcount;
6544 return symcount;
6547 /* Return the size required for the dynamic reloc entries. Any loadable
6548 section that was actually installed in the BFD, and has type SHT_REL
6549 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6550 dynamic reloc section. */
6552 long
6553 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6555 long ret;
6556 asection *s;
6558 if (elf_dynsymtab (abfd) == 0)
6560 bfd_set_error (bfd_error_invalid_operation);
6561 return -1;
6564 ret = sizeof (arelent *);
6565 for (s = abfd->sections; s != NULL; s = s->next)
6566 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6567 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6568 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6569 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6570 * sizeof (arelent *));
6572 return ret;
6575 /* Canonicalize the dynamic relocation entries. Note that we return the
6576 dynamic relocations as a single block, although they are actually
6577 associated with particular sections; the interface, which was
6578 designed for SunOS style shared libraries, expects that there is only
6579 one set of dynamic relocs. Any loadable section that was actually
6580 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6581 dynamic symbol table, is considered to be a dynamic reloc section. */
6583 long
6584 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6585 arelent **storage,
6586 asymbol **syms)
6588 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6589 asection *s;
6590 long ret;
6592 if (elf_dynsymtab (abfd) == 0)
6594 bfd_set_error (bfd_error_invalid_operation);
6595 return -1;
6598 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6599 ret = 0;
6600 for (s = abfd->sections; s != NULL; s = s->next)
6602 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6603 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6604 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6606 arelent *p;
6607 long count, i;
6609 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6610 return -1;
6611 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6612 p = s->relocation;
6613 for (i = 0; i < count; i++)
6614 *storage++ = p++;
6615 ret += count;
6619 *storage = NULL;
6621 return ret;
6624 /* Read in the version information. */
6626 bfd_boolean
6627 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6629 bfd_byte *contents = NULL;
6630 unsigned int freeidx = 0;
6632 if (elf_dynverref (abfd) != 0)
6634 Elf_Internal_Shdr *hdr;
6635 Elf_External_Verneed *everneed;
6636 Elf_Internal_Verneed *iverneed;
6637 unsigned int i;
6638 bfd_byte *contents_end;
6640 hdr = &elf_tdata (abfd)->dynverref_hdr;
6642 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6643 sizeof (Elf_Internal_Verneed));
6644 if (elf_tdata (abfd)->verref == NULL)
6645 goto error_return;
6647 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6649 contents = bfd_malloc (hdr->sh_size);
6650 if (contents == NULL)
6652 error_return_verref:
6653 elf_tdata (abfd)->verref = NULL;
6654 elf_tdata (abfd)->cverrefs = 0;
6655 goto error_return;
6657 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6658 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6659 goto error_return_verref;
6661 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6662 goto error_return_verref;
6664 BFD_ASSERT (sizeof (Elf_External_Verneed)
6665 == sizeof (Elf_External_Vernaux));
6666 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6667 everneed = (Elf_External_Verneed *) contents;
6668 iverneed = elf_tdata (abfd)->verref;
6669 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6671 Elf_External_Vernaux *evernaux;
6672 Elf_Internal_Vernaux *ivernaux;
6673 unsigned int j;
6675 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6677 iverneed->vn_bfd = abfd;
6679 iverneed->vn_filename =
6680 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6681 iverneed->vn_file);
6682 if (iverneed->vn_filename == NULL)
6683 goto error_return_verref;
6685 if (iverneed->vn_cnt == 0)
6686 iverneed->vn_auxptr = NULL;
6687 else
6689 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6690 sizeof (Elf_Internal_Vernaux));
6691 if (iverneed->vn_auxptr == NULL)
6692 goto error_return_verref;
6695 if (iverneed->vn_aux
6696 > (size_t) (contents_end - (bfd_byte *) everneed))
6697 goto error_return_verref;
6699 evernaux = ((Elf_External_Vernaux *)
6700 ((bfd_byte *) everneed + iverneed->vn_aux));
6701 ivernaux = iverneed->vn_auxptr;
6702 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6704 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6706 ivernaux->vna_nodename =
6707 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6708 ivernaux->vna_name);
6709 if (ivernaux->vna_nodename == NULL)
6710 goto error_return_verref;
6712 if (j + 1 < iverneed->vn_cnt)
6713 ivernaux->vna_nextptr = ivernaux + 1;
6714 else
6715 ivernaux->vna_nextptr = NULL;
6717 if (ivernaux->vna_next
6718 > (size_t) (contents_end - (bfd_byte *) evernaux))
6719 goto error_return_verref;
6721 evernaux = ((Elf_External_Vernaux *)
6722 ((bfd_byte *) evernaux + ivernaux->vna_next));
6724 if (ivernaux->vna_other > freeidx)
6725 freeidx = ivernaux->vna_other;
6728 if (i + 1 < hdr->sh_info)
6729 iverneed->vn_nextref = iverneed + 1;
6730 else
6731 iverneed->vn_nextref = NULL;
6733 if (iverneed->vn_next
6734 > (size_t) (contents_end - (bfd_byte *) everneed))
6735 goto error_return_verref;
6737 everneed = ((Elf_External_Verneed *)
6738 ((bfd_byte *) everneed + iverneed->vn_next));
6741 free (contents);
6742 contents = NULL;
6745 if (elf_dynverdef (abfd) != 0)
6747 Elf_Internal_Shdr *hdr;
6748 Elf_External_Verdef *everdef;
6749 Elf_Internal_Verdef *iverdef;
6750 Elf_Internal_Verdef *iverdefarr;
6751 Elf_Internal_Verdef iverdefmem;
6752 unsigned int i;
6753 unsigned int maxidx;
6754 bfd_byte *contents_end_def, *contents_end_aux;
6756 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6758 contents = bfd_malloc (hdr->sh_size);
6759 if (contents == NULL)
6760 goto error_return;
6761 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6762 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6763 goto error_return;
6765 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6766 goto error_return;
6768 BFD_ASSERT (sizeof (Elf_External_Verdef)
6769 >= sizeof (Elf_External_Verdaux));
6770 contents_end_def = contents + hdr->sh_size
6771 - sizeof (Elf_External_Verdef);
6772 contents_end_aux = contents + hdr->sh_size
6773 - sizeof (Elf_External_Verdaux);
6775 /* We know the number of entries in the section but not the maximum
6776 index. Therefore we have to run through all entries and find
6777 the maximum. */
6778 everdef = (Elf_External_Verdef *) contents;
6779 maxidx = 0;
6780 for (i = 0; i < hdr->sh_info; ++i)
6782 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6784 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6785 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6787 if (iverdefmem.vd_next
6788 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6789 goto error_return;
6791 everdef = ((Elf_External_Verdef *)
6792 ((bfd_byte *) everdef + iverdefmem.vd_next));
6795 if (default_imported_symver)
6797 if (freeidx > maxidx)
6798 maxidx = ++freeidx;
6799 else
6800 freeidx = ++maxidx;
6802 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6803 sizeof (Elf_Internal_Verdef));
6804 if (elf_tdata (abfd)->verdef == NULL)
6805 goto error_return;
6807 elf_tdata (abfd)->cverdefs = maxidx;
6809 everdef = (Elf_External_Verdef *) contents;
6810 iverdefarr = elf_tdata (abfd)->verdef;
6811 for (i = 0; i < hdr->sh_info; i++)
6813 Elf_External_Verdaux *everdaux;
6814 Elf_Internal_Verdaux *iverdaux;
6815 unsigned int j;
6817 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6819 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6821 error_return_verdef:
6822 elf_tdata (abfd)->verdef = NULL;
6823 elf_tdata (abfd)->cverdefs = 0;
6824 goto error_return;
6827 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6828 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6830 iverdef->vd_bfd = abfd;
6832 if (iverdef->vd_cnt == 0)
6833 iverdef->vd_auxptr = NULL;
6834 else
6836 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6837 sizeof (Elf_Internal_Verdaux));
6838 if (iverdef->vd_auxptr == NULL)
6839 goto error_return_verdef;
6842 if (iverdef->vd_aux
6843 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6844 goto error_return_verdef;
6846 everdaux = ((Elf_External_Verdaux *)
6847 ((bfd_byte *) everdef + iverdef->vd_aux));
6848 iverdaux = iverdef->vd_auxptr;
6849 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6851 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6853 iverdaux->vda_nodename =
6854 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6855 iverdaux->vda_name);
6856 if (iverdaux->vda_nodename == NULL)
6857 goto error_return_verdef;
6859 if (j + 1 < iverdef->vd_cnt)
6860 iverdaux->vda_nextptr = iverdaux + 1;
6861 else
6862 iverdaux->vda_nextptr = NULL;
6864 if (iverdaux->vda_next
6865 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6866 goto error_return_verdef;
6868 everdaux = ((Elf_External_Verdaux *)
6869 ((bfd_byte *) everdaux + iverdaux->vda_next));
6872 if (iverdef->vd_cnt)
6873 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6875 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6876 iverdef->vd_nextdef = iverdef + 1;
6877 else
6878 iverdef->vd_nextdef = NULL;
6880 everdef = ((Elf_External_Verdef *)
6881 ((bfd_byte *) everdef + iverdef->vd_next));
6884 free (contents);
6885 contents = NULL;
6887 else if (default_imported_symver)
6889 if (freeidx < 3)
6890 freeidx = 3;
6891 else
6892 freeidx++;
6894 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6895 sizeof (Elf_Internal_Verdef));
6896 if (elf_tdata (abfd)->verdef == NULL)
6897 goto error_return;
6899 elf_tdata (abfd)->cverdefs = freeidx;
6902 /* Create a default version based on the soname. */
6903 if (default_imported_symver)
6905 Elf_Internal_Verdef *iverdef;
6906 Elf_Internal_Verdaux *iverdaux;
6908 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6910 iverdef->vd_version = VER_DEF_CURRENT;
6911 iverdef->vd_flags = 0;
6912 iverdef->vd_ndx = freeidx;
6913 iverdef->vd_cnt = 1;
6915 iverdef->vd_bfd = abfd;
6917 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6918 if (iverdef->vd_nodename == NULL)
6919 goto error_return_verdef;
6920 iverdef->vd_nextdef = NULL;
6921 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6922 if (iverdef->vd_auxptr == NULL)
6923 goto error_return_verdef;
6925 iverdaux = iverdef->vd_auxptr;
6926 iverdaux->vda_nodename = iverdef->vd_nodename;
6927 iverdaux->vda_nextptr = NULL;
6930 return TRUE;
6932 error_return:
6933 if (contents != NULL)
6934 free (contents);
6935 return FALSE;
6938 asymbol *
6939 _bfd_elf_make_empty_symbol (bfd *abfd)
6941 elf_symbol_type *newsym;
6942 bfd_size_type amt = sizeof (elf_symbol_type);
6944 newsym = bfd_zalloc (abfd, amt);
6945 if (!newsym)
6946 return NULL;
6947 else
6949 newsym->symbol.the_bfd = abfd;
6950 return &newsym->symbol;
6954 void
6955 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6956 asymbol *symbol,
6957 symbol_info *ret)
6959 bfd_symbol_info (symbol, ret);
6962 /* Return whether a symbol name implies a local symbol. Most targets
6963 use this function for the is_local_label_name entry point, but some
6964 override it. */
6966 bfd_boolean
6967 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6968 const char *name)
6970 /* Normal local symbols start with ``.L''. */
6971 if (name[0] == '.' && name[1] == 'L')
6972 return TRUE;
6974 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6975 DWARF debugging symbols starting with ``..''. */
6976 if (name[0] == '.' && name[1] == '.')
6977 return TRUE;
6979 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6980 emitting DWARF debugging output. I suspect this is actually a
6981 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6982 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6983 underscore to be emitted on some ELF targets). For ease of use,
6984 we treat such symbols as local. */
6985 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6986 return TRUE;
6988 return FALSE;
6991 alent *
6992 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6993 asymbol *symbol ATTRIBUTE_UNUSED)
6995 abort ();
6996 return NULL;
6999 bfd_boolean
7000 _bfd_elf_set_arch_mach (bfd *abfd,
7001 enum bfd_architecture arch,
7002 unsigned long machine)
7004 /* If this isn't the right architecture for this backend, and this
7005 isn't the generic backend, fail. */
7006 if (arch != get_elf_backend_data (abfd)->arch
7007 && arch != bfd_arch_unknown
7008 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7009 return FALSE;
7011 return bfd_default_set_arch_mach (abfd, arch, machine);
7014 /* Find the function to a particular section and offset,
7015 for error reporting. */
7017 static bfd_boolean
7018 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7019 asection *section,
7020 asymbol **symbols,
7021 bfd_vma offset,
7022 const char **filename_ptr,
7023 const char **functionname_ptr)
7025 const char *filename;
7026 asymbol *func, *file;
7027 bfd_vma low_func;
7028 asymbol **p;
7029 /* ??? Given multiple file symbols, it is impossible to reliably
7030 choose the right file name for global symbols. File symbols are
7031 local symbols, and thus all file symbols must sort before any
7032 global symbols. The ELF spec may be interpreted to say that a
7033 file symbol must sort before other local symbols, but currently
7034 ld -r doesn't do this. So, for ld -r output, it is possible to
7035 make a better choice of file name for local symbols by ignoring
7036 file symbols appearing after a given local symbol. */
7037 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7039 filename = NULL;
7040 func = NULL;
7041 file = NULL;
7042 low_func = 0;
7043 state = nothing_seen;
7045 for (p = symbols; *p != NULL; p++)
7047 elf_symbol_type *q;
7049 q = (elf_symbol_type *) *p;
7051 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7053 default:
7054 break;
7055 case STT_FILE:
7056 file = &q->symbol;
7057 if (state == symbol_seen)
7058 state = file_after_symbol_seen;
7059 continue;
7060 case STT_NOTYPE:
7061 case STT_FUNC:
7062 if (bfd_get_section (&q->symbol) == section
7063 && q->symbol.value >= low_func
7064 && q->symbol.value <= offset)
7066 func = (asymbol *) q;
7067 low_func = q->symbol.value;
7068 filename = NULL;
7069 if (file != NULL
7070 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7071 || state != file_after_symbol_seen))
7072 filename = bfd_asymbol_name (file);
7074 break;
7076 if (state == nothing_seen)
7077 state = symbol_seen;
7080 if (func == NULL)
7081 return FALSE;
7083 if (filename_ptr)
7084 *filename_ptr = filename;
7085 if (functionname_ptr)
7086 *functionname_ptr = bfd_asymbol_name (func);
7088 return TRUE;
7091 /* Find the nearest line to a particular section and offset,
7092 for error reporting. */
7094 bfd_boolean
7095 _bfd_elf_find_nearest_line (bfd *abfd,
7096 asection *section,
7097 asymbol **symbols,
7098 bfd_vma offset,
7099 const char **filename_ptr,
7100 const char **functionname_ptr,
7101 unsigned int *line_ptr)
7103 bfd_boolean found;
7105 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7106 filename_ptr, functionname_ptr,
7107 line_ptr))
7109 if (!*functionname_ptr)
7110 elf_find_function (abfd, section, symbols, offset,
7111 *filename_ptr ? NULL : filename_ptr,
7112 functionname_ptr);
7114 return TRUE;
7117 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7118 filename_ptr, functionname_ptr,
7119 line_ptr, 0,
7120 &elf_tdata (abfd)->dwarf2_find_line_info))
7122 if (!*functionname_ptr)
7123 elf_find_function (abfd, section, symbols, offset,
7124 *filename_ptr ? NULL : filename_ptr,
7125 functionname_ptr);
7127 return TRUE;
7130 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7131 &found, filename_ptr,
7132 functionname_ptr, line_ptr,
7133 &elf_tdata (abfd)->line_info))
7134 return FALSE;
7135 if (found && (*functionname_ptr || *line_ptr))
7136 return TRUE;
7138 if (symbols == NULL)
7139 return FALSE;
7141 if (! elf_find_function (abfd, section, symbols, offset,
7142 filename_ptr, functionname_ptr))
7143 return FALSE;
7145 *line_ptr = 0;
7146 return TRUE;
7149 /* Find the line for a symbol. */
7151 bfd_boolean
7152 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7153 const char **filename_ptr, unsigned int *line_ptr)
7155 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7156 filename_ptr, line_ptr, 0,
7157 &elf_tdata (abfd)->dwarf2_find_line_info);
7160 /* After a call to bfd_find_nearest_line, successive calls to
7161 bfd_find_inliner_info can be used to get source information about
7162 each level of function inlining that terminated at the address
7163 passed to bfd_find_nearest_line. Currently this is only supported
7164 for DWARF2 with appropriate DWARF3 extensions. */
7166 bfd_boolean
7167 _bfd_elf_find_inliner_info (bfd *abfd,
7168 const char **filename_ptr,
7169 const char **functionname_ptr,
7170 unsigned int *line_ptr)
7172 bfd_boolean found;
7173 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7174 functionname_ptr, line_ptr,
7175 & elf_tdata (abfd)->dwarf2_find_line_info);
7176 return found;
7180 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7182 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7183 int ret = bed->s->sizeof_ehdr;
7185 if (!info->relocatable)
7187 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7189 if (phdr_size == (bfd_size_type) -1)
7191 struct elf_segment_map *m;
7193 phdr_size = 0;
7194 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7195 phdr_size += bed->s->sizeof_phdr;
7197 if (phdr_size == 0)
7198 phdr_size = get_program_header_size (abfd, info);
7201 elf_tdata (abfd)->program_header_size = phdr_size;
7202 ret += phdr_size;
7205 return ret;
7208 bfd_boolean
7209 _bfd_elf_set_section_contents (bfd *abfd,
7210 sec_ptr section,
7211 const void *location,
7212 file_ptr offset,
7213 bfd_size_type count)
7215 Elf_Internal_Shdr *hdr;
7216 bfd_signed_vma pos;
7218 if (! abfd->output_has_begun
7219 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7220 return FALSE;
7222 hdr = &elf_section_data (section)->this_hdr;
7223 pos = hdr->sh_offset + offset;
7224 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7225 || bfd_bwrite (location, count, abfd) != count)
7226 return FALSE;
7228 return TRUE;
7231 void
7232 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7233 arelent *cache_ptr ATTRIBUTE_UNUSED,
7234 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7236 abort ();
7239 /* Try to convert a non-ELF reloc into an ELF one. */
7241 bfd_boolean
7242 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7244 /* Check whether we really have an ELF howto. */
7246 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7248 bfd_reloc_code_real_type code;
7249 reloc_howto_type *howto;
7251 /* Alien reloc: Try to determine its type to replace it with an
7252 equivalent ELF reloc. */
7254 if (areloc->howto->pc_relative)
7256 switch (areloc->howto->bitsize)
7258 case 8:
7259 code = BFD_RELOC_8_PCREL;
7260 break;
7261 case 12:
7262 code = BFD_RELOC_12_PCREL;
7263 break;
7264 case 16:
7265 code = BFD_RELOC_16_PCREL;
7266 break;
7267 case 24:
7268 code = BFD_RELOC_24_PCREL;
7269 break;
7270 case 32:
7271 code = BFD_RELOC_32_PCREL;
7272 break;
7273 case 64:
7274 code = BFD_RELOC_64_PCREL;
7275 break;
7276 default:
7277 goto fail;
7280 howto = bfd_reloc_type_lookup (abfd, code);
7282 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7284 if (howto->pcrel_offset)
7285 areloc->addend += areloc->address;
7286 else
7287 areloc->addend -= areloc->address; /* addend is unsigned!! */
7290 else
7292 switch (areloc->howto->bitsize)
7294 case 8:
7295 code = BFD_RELOC_8;
7296 break;
7297 case 14:
7298 code = BFD_RELOC_14;
7299 break;
7300 case 16:
7301 code = BFD_RELOC_16;
7302 break;
7303 case 26:
7304 code = BFD_RELOC_26;
7305 break;
7306 case 32:
7307 code = BFD_RELOC_32;
7308 break;
7309 case 64:
7310 code = BFD_RELOC_64;
7311 break;
7312 default:
7313 goto fail;
7316 howto = bfd_reloc_type_lookup (abfd, code);
7319 if (howto)
7320 areloc->howto = howto;
7321 else
7322 goto fail;
7325 return TRUE;
7327 fail:
7328 (*_bfd_error_handler)
7329 (_("%B: unsupported relocation type %s"),
7330 abfd, areloc->howto->name);
7331 bfd_set_error (bfd_error_bad_value);
7332 return FALSE;
7335 bfd_boolean
7336 _bfd_elf_close_and_cleanup (bfd *abfd)
7338 if (bfd_get_format (abfd) == bfd_object)
7340 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7341 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7342 _bfd_dwarf2_cleanup_debug_info (abfd);
7345 return _bfd_generic_close_and_cleanup (abfd);
7348 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7349 in the relocation's offset. Thus we cannot allow any sort of sanity
7350 range-checking to interfere. There is nothing else to do in processing
7351 this reloc. */
7353 bfd_reloc_status_type
7354 _bfd_elf_rel_vtable_reloc_fn
7355 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7356 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7357 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7358 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7360 return bfd_reloc_ok;
7363 /* Elf core file support. Much of this only works on native
7364 toolchains, since we rely on knowing the
7365 machine-dependent procfs structure in order to pick
7366 out details about the corefile. */
7368 #ifdef HAVE_SYS_PROCFS_H
7369 # include <sys/procfs.h>
7370 #endif
7372 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7374 static int
7375 elfcore_make_pid (bfd *abfd)
7377 return ((elf_tdata (abfd)->core_lwpid << 16)
7378 + (elf_tdata (abfd)->core_pid));
7381 /* If there isn't a section called NAME, make one, using
7382 data from SECT. Note, this function will generate a
7383 reference to NAME, so you shouldn't deallocate or
7384 overwrite it. */
7386 static bfd_boolean
7387 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7389 asection *sect2;
7391 if (bfd_get_section_by_name (abfd, name) != NULL)
7392 return TRUE;
7394 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7395 if (sect2 == NULL)
7396 return FALSE;
7398 sect2->size = sect->size;
7399 sect2->filepos = sect->filepos;
7400 sect2->alignment_power = sect->alignment_power;
7401 return TRUE;
7404 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7405 actually creates up to two pseudosections:
7406 - For the single-threaded case, a section named NAME, unless
7407 such a section already exists.
7408 - For the multi-threaded case, a section named "NAME/PID", where
7409 PID is elfcore_make_pid (abfd).
7410 Both pseudosections have identical contents. */
7411 bfd_boolean
7412 _bfd_elfcore_make_pseudosection (bfd *abfd,
7413 char *name,
7414 size_t size,
7415 ufile_ptr filepos)
7417 char buf[100];
7418 char *threaded_name;
7419 size_t len;
7420 asection *sect;
7422 /* Build the section name. */
7424 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7425 len = strlen (buf) + 1;
7426 threaded_name = bfd_alloc (abfd, len);
7427 if (threaded_name == NULL)
7428 return FALSE;
7429 memcpy (threaded_name, buf, len);
7431 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7432 SEC_HAS_CONTENTS);
7433 if (sect == NULL)
7434 return FALSE;
7435 sect->size = size;
7436 sect->filepos = filepos;
7437 sect->alignment_power = 2;
7439 return elfcore_maybe_make_sect (abfd, name, sect);
7442 /* prstatus_t exists on:
7443 solaris 2.5+
7444 linux 2.[01] + glibc
7445 unixware 4.2
7448 #if defined (HAVE_PRSTATUS_T)
7450 static bfd_boolean
7451 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7453 size_t size;
7454 int offset;
7456 if (note->descsz == sizeof (prstatus_t))
7458 prstatus_t prstat;
7460 size = sizeof (prstat.pr_reg);
7461 offset = offsetof (prstatus_t, pr_reg);
7462 memcpy (&prstat, note->descdata, sizeof (prstat));
7464 /* Do not overwrite the core signal if it
7465 has already been set by another thread. */
7466 if (elf_tdata (abfd)->core_signal == 0)
7467 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7468 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7470 /* pr_who exists on:
7471 solaris 2.5+
7472 unixware 4.2
7473 pr_who doesn't exist on:
7474 linux 2.[01]
7476 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7477 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7478 #endif
7480 #if defined (HAVE_PRSTATUS32_T)
7481 else if (note->descsz == sizeof (prstatus32_t))
7483 /* 64-bit host, 32-bit corefile */
7484 prstatus32_t prstat;
7486 size = sizeof (prstat.pr_reg);
7487 offset = offsetof (prstatus32_t, pr_reg);
7488 memcpy (&prstat, note->descdata, sizeof (prstat));
7490 /* Do not overwrite the core signal if it
7491 has already been set by another thread. */
7492 if (elf_tdata (abfd)->core_signal == 0)
7493 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7494 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7496 /* pr_who exists on:
7497 solaris 2.5+
7498 unixware 4.2
7499 pr_who doesn't exist on:
7500 linux 2.[01]
7502 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7503 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7504 #endif
7506 #endif /* HAVE_PRSTATUS32_T */
7507 else
7509 /* Fail - we don't know how to handle any other
7510 note size (ie. data object type). */
7511 return TRUE;
7514 /* Make a ".reg/999" section and a ".reg" section. */
7515 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7516 size, note->descpos + offset);
7518 #endif /* defined (HAVE_PRSTATUS_T) */
7520 /* Create a pseudosection containing the exact contents of NOTE. */
7521 static bfd_boolean
7522 elfcore_make_note_pseudosection (bfd *abfd,
7523 char *name,
7524 Elf_Internal_Note *note)
7526 return _bfd_elfcore_make_pseudosection (abfd, name,
7527 note->descsz, note->descpos);
7530 /* There isn't a consistent prfpregset_t across platforms,
7531 but it doesn't matter, because we don't have to pick this
7532 data structure apart. */
7534 static bfd_boolean
7535 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7537 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7540 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7541 type of NT_PRXFPREG. Just include the whole note's contents
7542 literally. */
7544 static bfd_boolean
7545 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7547 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7550 static bfd_boolean
7551 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7553 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7557 #if defined (HAVE_PRPSINFO_T)
7558 typedef prpsinfo_t elfcore_psinfo_t;
7559 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7560 typedef prpsinfo32_t elfcore_psinfo32_t;
7561 #endif
7562 #endif
7564 #if defined (HAVE_PSINFO_T)
7565 typedef psinfo_t elfcore_psinfo_t;
7566 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7567 typedef psinfo32_t elfcore_psinfo32_t;
7568 #endif
7569 #endif
7571 /* return a malloc'ed copy of a string at START which is at
7572 most MAX bytes long, possibly without a terminating '\0'.
7573 the copy will always have a terminating '\0'. */
7575 char *
7576 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7578 char *dups;
7579 char *end = memchr (start, '\0', max);
7580 size_t len;
7582 if (end == NULL)
7583 len = max;
7584 else
7585 len = end - start;
7587 dups = bfd_alloc (abfd, len + 1);
7588 if (dups == NULL)
7589 return NULL;
7591 memcpy (dups, start, len);
7592 dups[len] = '\0';
7594 return dups;
7597 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7598 static bfd_boolean
7599 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7601 if (note->descsz == sizeof (elfcore_psinfo_t))
7603 elfcore_psinfo_t psinfo;
7605 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7607 elf_tdata (abfd)->core_program
7608 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7609 sizeof (psinfo.pr_fname));
7611 elf_tdata (abfd)->core_command
7612 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7613 sizeof (psinfo.pr_psargs));
7615 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7616 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7618 /* 64-bit host, 32-bit corefile */
7619 elfcore_psinfo32_t psinfo;
7621 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7623 elf_tdata (abfd)->core_program
7624 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7625 sizeof (psinfo.pr_fname));
7627 elf_tdata (abfd)->core_command
7628 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7629 sizeof (psinfo.pr_psargs));
7631 #endif
7633 else
7635 /* Fail - we don't know how to handle any other
7636 note size (ie. data object type). */
7637 return TRUE;
7640 /* Note that for some reason, a spurious space is tacked
7641 onto the end of the args in some (at least one anyway)
7642 implementations, so strip it off if it exists. */
7645 char *command = elf_tdata (abfd)->core_command;
7646 int n = strlen (command);
7648 if (0 < n && command[n - 1] == ' ')
7649 command[n - 1] = '\0';
7652 return TRUE;
7654 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7656 #if defined (HAVE_PSTATUS_T)
7657 static bfd_boolean
7658 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7660 if (note->descsz == sizeof (pstatus_t)
7661 #if defined (HAVE_PXSTATUS_T)
7662 || note->descsz == sizeof (pxstatus_t)
7663 #endif
7666 pstatus_t pstat;
7668 memcpy (&pstat, note->descdata, sizeof (pstat));
7670 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7672 #if defined (HAVE_PSTATUS32_T)
7673 else if (note->descsz == sizeof (pstatus32_t))
7675 /* 64-bit host, 32-bit corefile */
7676 pstatus32_t pstat;
7678 memcpy (&pstat, note->descdata, sizeof (pstat));
7680 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7682 #endif
7683 /* Could grab some more details from the "representative"
7684 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7685 NT_LWPSTATUS note, presumably. */
7687 return TRUE;
7689 #endif /* defined (HAVE_PSTATUS_T) */
7691 #if defined (HAVE_LWPSTATUS_T)
7692 static bfd_boolean
7693 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7695 lwpstatus_t lwpstat;
7696 char buf[100];
7697 char *name;
7698 size_t len;
7699 asection *sect;
7701 if (note->descsz != sizeof (lwpstat)
7702 #if defined (HAVE_LWPXSTATUS_T)
7703 && note->descsz != sizeof (lwpxstatus_t)
7704 #endif
7706 return TRUE;
7708 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7710 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7711 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7713 /* Make a ".reg/999" section. */
7715 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7716 len = strlen (buf) + 1;
7717 name = bfd_alloc (abfd, len);
7718 if (name == NULL)
7719 return FALSE;
7720 memcpy (name, buf, len);
7722 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7723 if (sect == NULL)
7724 return FALSE;
7726 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7727 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7728 sect->filepos = note->descpos
7729 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7730 #endif
7732 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7733 sect->size = sizeof (lwpstat.pr_reg);
7734 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7735 #endif
7737 sect->alignment_power = 2;
7739 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7740 return FALSE;
7742 /* Make a ".reg2/999" section */
7744 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7745 len = strlen (buf) + 1;
7746 name = bfd_alloc (abfd, len);
7747 if (name == NULL)
7748 return FALSE;
7749 memcpy (name, buf, len);
7751 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7752 if (sect == NULL)
7753 return FALSE;
7755 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7756 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7757 sect->filepos = note->descpos
7758 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7759 #endif
7761 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7762 sect->size = sizeof (lwpstat.pr_fpreg);
7763 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7764 #endif
7766 sect->alignment_power = 2;
7768 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7770 #endif /* defined (HAVE_LWPSTATUS_T) */
7772 static bfd_boolean
7773 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7775 char buf[30];
7776 char *name;
7777 size_t len;
7778 asection *sect;
7779 int type;
7780 int is_active_thread;
7781 bfd_vma base_addr;
7783 if (note->descsz < 728)
7784 return TRUE;
7786 if (! CONST_STRNEQ (note->namedata, "win32"))
7787 return TRUE;
7789 type = bfd_get_32 (abfd, note->descdata);
7791 switch (type)
7793 case 1 /* NOTE_INFO_PROCESS */:
7794 /* FIXME: need to add ->core_command. */
7795 /* process_info.pid */
7796 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7797 /* process_info.signal */
7798 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7799 break;
7801 case 2 /* NOTE_INFO_THREAD */:
7802 /* Make a ".reg/999" section. */
7803 /* thread_info.tid */
7804 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7806 len = strlen (buf) + 1;
7807 name = bfd_alloc (abfd, len);
7808 if (name == NULL)
7809 return FALSE;
7811 memcpy (name, buf, len);
7813 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7814 if (sect == NULL)
7815 return FALSE;
7817 /* sizeof (thread_info.thread_context) */
7818 sect->size = 716;
7819 /* offsetof (thread_info.thread_context) */
7820 sect->filepos = note->descpos + 12;
7821 sect->alignment_power = 2;
7823 /* thread_info.is_active_thread */
7824 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7826 if (is_active_thread)
7827 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7828 return FALSE;
7829 break;
7831 case 3 /* NOTE_INFO_MODULE */:
7832 /* Make a ".module/xxxxxxxx" section. */
7833 /* module_info.base_address */
7834 base_addr = bfd_get_32 (abfd, note->descdata + 4);
7835 sprintf (buf, ".module/%08lx", (long) base_addr);
7837 len = strlen (buf) + 1;
7838 name = bfd_alloc (abfd, len);
7839 if (name == NULL)
7840 return FALSE;
7842 memcpy (name, buf, len);
7844 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7846 if (sect == NULL)
7847 return FALSE;
7849 sect->size = note->descsz;
7850 sect->filepos = note->descpos;
7851 sect->alignment_power = 2;
7852 break;
7854 default:
7855 return TRUE;
7858 return TRUE;
7861 static bfd_boolean
7862 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7864 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7866 switch (note->type)
7868 default:
7869 return TRUE;
7871 case NT_PRSTATUS:
7872 if (bed->elf_backend_grok_prstatus)
7873 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7874 return TRUE;
7875 #if defined (HAVE_PRSTATUS_T)
7876 return elfcore_grok_prstatus (abfd, note);
7877 #else
7878 return TRUE;
7879 #endif
7881 #if defined (HAVE_PSTATUS_T)
7882 case NT_PSTATUS:
7883 return elfcore_grok_pstatus (abfd, note);
7884 #endif
7886 #if defined (HAVE_LWPSTATUS_T)
7887 case NT_LWPSTATUS:
7888 return elfcore_grok_lwpstatus (abfd, note);
7889 #endif
7891 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7892 return elfcore_grok_prfpreg (abfd, note);
7894 case NT_WIN32PSTATUS:
7895 return elfcore_grok_win32pstatus (abfd, note);
7897 case NT_PRXFPREG: /* Linux SSE extension */
7898 if (note->namesz == 6
7899 && strcmp (note->namedata, "LINUX") == 0)
7900 return elfcore_grok_prxfpreg (abfd, note);
7901 else
7902 return TRUE;
7904 case NT_PPC_VMX:
7905 if (note->namesz == 6
7906 && strcmp (note->namedata, "LINUX") == 0)
7907 return elfcore_grok_ppc_vmx (abfd, note);
7908 else
7909 return TRUE;
7911 case NT_PRPSINFO:
7912 case NT_PSINFO:
7913 if (bed->elf_backend_grok_psinfo)
7914 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7915 return TRUE;
7916 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7917 return elfcore_grok_psinfo (abfd, note);
7918 #else
7919 return TRUE;
7920 #endif
7922 case NT_AUXV:
7924 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7925 SEC_HAS_CONTENTS);
7927 if (sect == NULL)
7928 return FALSE;
7929 sect->size = note->descsz;
7930 sect->filepos = note->descpos;
7931 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7933 return TRUE;
7938 static bfd_boolean
7939 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
7941 elf_tdata (abfd)->build_id_size = note->descsz;
7942 elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz);
7943 if (elf_tdata (abfd)->build_id == NULL)
7944 return FALSE;
7946 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
7948 return TRUE;
7951 static bfd_boolean
7952 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
7954 switch (note->type)
7956 default:
7957 return TRUE;
7959 case NT_GNU_BUILD_ID:
7960 return elfobj_grok_gnu_build_id (abfd, note);
7964 static bfd_boolean
7965 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7967 char *cp;
7969 cp = strchr (note->namedata, '@');
7970 if (cp != NULL)
7972 *lwpidp = atoi(cp + 1);
7973 return TRUE;
7975 return FALSE;
7978 static bfd_boolean
7979 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
7981 /* Signal number at offset 0x08. */
7982 elf_tdata (abfd)->core_signal
7983 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7985 /* Process ID at offset 0x50. */
7986 elf_tdata (abfd)->core_pid
7987 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7989 /* Command name at 0x7c (max 32 bytes, including nul). */
7990 elf_tdata (abfd)->core_command
7991 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7993 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7994 note);
7997 static bfd_boolean
7998 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8000 int lwp;
8002 if (elfcore_netbsd_get_lwpid (note, &lwp))
8003 elf_tdata (abfd)->core_lwpid = lwp;
8005 if (note->type == NT_NETBSDCORE_PROCINFO)
8007 /* NetBSD-specific core "procinfo". Note that we expect to
8008 find this note before any of the others, which is fine,
8009 since the kernel writes this note out first when it
8010 creates a core file. */
8012 return elfcore_grok_netbsd_procinfo (abfd, note);
8015 /* As of Jan 2002 there are no other machine-independent notes
8016 defined for NetBSD core files. If the note type is less
8017 than the start of the machine-dependent note types, we don't
8018 understand it. */
8020 if (note->type < NT_NETBSDCORE_FIRSTMACH)
8021 return TRUE;
8024 switch (bfd_get_arch (abfd))
8026 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8027 PT_GETFPREGS == mach+2. */
8029 case bfd_arch_alpha:
8030 case bfd_arch_sparc:
8031 switch (note->type)
8033 case NT_NETBSDCORE_FIRSTMACH+0:
8034 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8036 case NT_NETBSDCORE_FIRSTMACH+2:
8037 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8039 default:
8040 return TRUE;
8043 /* On all other arch's, PT_GETREGS == mach+1 and
8044 PT_GETFPREGS == mach+3. */
8046 default:
8047 switch (note->type)
8049 case NT_NETBSDCORE_FIRSTMACH+1:
8050 return elfcore_make_note_pseudosection (abfd, ".reg", note);
8052 case NT_NETBSDCORE_FIRSTMACH+3:
8053 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8055 default:
8056 return TRUE;
8059 /* NOTREACHED */
8062 static bfd_boolean
8063 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8065 void *ddata = note->descdata;
8066 char buf[100];
8067 char *name;
8068 asection *sect;
8069 short sig;
8070 unsigned flags;
8072 /* nto_procfs_status 'pid' field is at offset 0. */
8073 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8075 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8076 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8078 /* nto_procfs_status 'flags' field is at offset 8. */
8079 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8081 /* nto_procfs_status 'what' field is at offset 14. */
8082 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8084 elf_tdata (abfd)->core_signal = sig;
8085 elf_tdata (abfd)->core_lwpid = *tid;
8088 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8089 do not come from signals so we make sure we set the current
8090 thread just in case. */
8091 if (flags & 0x00000080)
8092 elf_tdata (abfd)->core_lwpid = *tid;
8094 /* Make a ".qnx_core_status/%d" section. */
8095 sprintf (buf, ".qnx_core_status/%ld", *tid);
8097 name = bfd_alloc (abfd, strlen (buf) + 1);
8098 if (name == NULL)
8099 return FALSE;
8100 strcpy (name, buf);
8102 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8103 if (sect == NULL)
8104 return FALSE;
8106 sect->size = note->descsz;
8107 sect->filepos = note->descpos;
8108 sect->alignment_power = 2;
8110 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8113 static bfd_boolean
8114 elfcore_grok_nto_regs (bfd *abfd,
8115 Elf_Internal_Note *note,
8116 long tid,
8117 char *base)
8119 char buf[100];
8120 char *name;
8121 asection *sect;
8123 /* Make a "(base)/%d" section. */
8124 sprintf (buf, "%s/%ld", base, tid);
8126 name = bfd_alloc (abfd, strlen (buf) + 1);
8127 if (name == NULL)
8128 return FALSE;
8129 strcpy (name, buf);
8131 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8132 if (sect == NULL)
8133 return FALSE;
8135 sect->size = note->descsz;
8136 sect->filepos = note->descpos;
8137 sect->alignment_power = 2;
8139 /* This is the current thread. */
8140 if (elf_tdata (abfd)->core_lwpid == tid)
8141 return elfcore_maybe_make_sect (abfd, base, sect);
8143 return TRUE;
8146 #define BFD_QNT_CORE_INFO 7
8147 #define BFD_QNT_CORE_STATUS 8
8148 #define BFD_QNT_CORE_GREG 9
8149 #define BFD_QNT_CORE_FPREG 10
8151 static bfd_boolean
8152 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8154 /* Every GREG section has a STATUS section before it. Store the
8155 tid from the previous call to pass down to the next gregs
8156 function. */
8157 static long tid = 1;
8159 switch (note->type)
8161 case BFD_QNT_CORE_INFO:
8162 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8163 case BFD_QNT_CORE_STATUS:
8164 return elfcore_grok_nto_status (abfd, note, &tid);
8165 case BFD_QNT_CORE_GREG:
8166 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8167 case BFD_QNT_CORE_FPREG:
8168 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8169 default:
8170 return TRUE;
8174 static bfd_boolean
8175 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8177 char *name;
8178 asection *sect;
8179 size_t len;
8181 /* Use note name as section name. */
8182 len = note->namesz;
8183 name = bfd_alloc (abfd, len);
8184 if (name == NULL)
8185 return FALSE;
8186 memcpy (name, note->namedata, len);
8187 name[len - 1] = '\0';
8189 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8190 if (sect == NULL)
8191 return FALSE;
8193 sect->size = note->descsz;
8194 sect->filepos = note->descpos;
8195 sect->alignment_power = 1;
8197 return TRUE;
8200 /* Function: elfcore_write_note
8202 Inputs:
8203 buffer to hold note, and current size of buffer
8204 name of note
8205 type of note
8206 data for note
8207 size of data for note
8209 Writes note to end of buffer. ELF64 notes are written exactly as
8210 for ELF32, despite the current (as of 2006) ELF gabi specifying
8211 that they ought to have 8-byte namesz and descsz field, and have
8212 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8214 Return:
8215 Pointer to realloc'd buffer, *BUFSIZ updated. */
8217 char *
8218 elfcore_write_note (bfd *abfd,
8219 char *buf,
8220 int *bufsiz,
8221 const char *name,
8222 int type,
8223 const void *input,
8224 int size)
8226 Elf_External_Note *xnp;
8227 size_t namesz;
8228 size_t newspace;
8229 char *dest;
8231 namesz = 0;
8232 if (name != NULL)
8233 namesz = strlen (name) + 1;
8235 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8237 buf = realloc (buf, *bufsiz + newspace);
8238 if (buf == NULL)
8239 return buf;
8240 dest = buf + *bufsiz;
8241 *bufsiz += newspace;
8242 xnp = (Elf_External_Note *) dest;
8243 H_PUT_32 (abfd, namesz, xnp->namesz);
8244 H_PUT_32 (abfd, size, xnp->descsz);
8245 H_PUT_32 (abfd, type, xnp->type);
8246 dest = xnp->name;
8247 if (name != NULL)
8249 memcpy (dest, name, namesz);
8250 dest += namesz;
8251 while (namesz & 3)
8253 *dest++ = '\0';
8254 ++namesz;
8257 memcpy (dest, input, size);
8258 dest += size;
8259 while (size & 3)
8261 *dest++ = '\0';
8262 ++size;
8264 return buf;
8267 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8268 char *
8269 elfcore_write_prpsinfo (bfd *abfd,
8270 char *buf,
8271 int *bufsiz,
8272 const char *fname,
8273 const char *psargs)
8275 const char *note_name = "CORE";
8276 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8278 if (bed->elf_backend_write_core_note != NULL)
8280 char *ret;
8281 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8282 NT_PRPSINFO, fname, psargs);
8283 if (ret != NULL)
8284 return ret;
8287 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8288 if (bed->s->elfclass == ELFCLASS32)
8290 #if defined (HAVE_PSINFO32_T)
8291 psinfo32_t data;
8292 int note_type = NT_PSINFO;
8293 #else
8294 prpsinfo32_t data;
8295 int note_type = NT_PRPSINFO;
8296 #endif
8298 memset (&data, 0, sizeof (data));
8299 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8300 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8301 return elfcore_write_note (abfd, buf, bufsiz,
8302 note_name, note_type, &data, sizeof (data));
8304 else
8305 #endif
8307 #if defined (HAVE_PSINFO_T)
8308 psinfo_t data;
8309 int note_type = NT_PSINFO;
8310 #else
8311 prpsinfo_t data;
8312 int note_type = NT_PRPSINFO;
8313 #endif
8315 memset (&data, 0, sizeof (data));
8316 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8317 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8318 return elfcore_write_note (abfd, buf, bufsiz,
8319 note_name, note_type, &data, sizeof (data));
8322 #endif /* PSINFO_T or PRPSINFO_T */
8324 #if defined (HAVE_PRSTATUS_T)
8325 char *
8326 elfcore_write_prstatus (bfd *abfd,
8327 char *buf,
8328 int *bufsiz,
8329 long pid,
8330 int cursig,
8331 const void *gregs)
8333 const char *note_name = "CORE";
8334 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8336 if (bed->elf_backend_write_core_note != NULL)
8338 char *ret;
8339 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8340 NT_PRSTATUS,
8341 pid, cursig, gregs);
8342 if (ret != NULL)
8343 return ret;
8346 #if defined (HAVE_PRSTATUS32_T)
8347 if (bed->s->elfclass == ELFCLASS32)
8349 prstatus32_t prstat;
8351 memset (&prstat, 0, sizeof (prstat));
8352 prstat.pr_pid = pid;
8353 prstat.pr_cursig = cursig;
8354 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8355 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8356 NT_PRSTATUS, &prstat, sizeof (prstat));
8358 else
8359 #endif
8361 prstatus_t prstat;
8363 memset (&prstat, 0, sizeof (prstat));
8364 prstat.pr_pid = pid;
8365 prstat.pr_cursig = cursig;
8366 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8367 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8368 NT_PRSTATUS, &prstat, sizeof (prstat));
8371 #endif /* HAVE_PRSTATUS_T */
8373 #if defined (HAVE_LWPSTATUS_T)
8374 char *
8375 elfcore_write_lwpstatus (bfd *abfd,
8376 char *buf,
8377 int *bufsiz,
8378 long pid,
8379 int cursig,
8380 const void *gregs)
8382 lwpstatus_t lwpstat;
8383 const char *note_name = "CORE";
8385 memset (&lwpstat, 0, sizeof (lwpstat));
8386 lwpstat.pr_lwpid = pid >> 16;
8387 lwpstat.pr_cursig = cursig;
8388 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8389 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8390 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8391 #if !defined(gregs)
8392 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8393 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8394 #else
8395 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8396 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8397 #endif
8398 #endif
8399 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8400 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8402 #endif /* HAVE_LWPSTATUS_T */
8404 #if defined (HAVE_PSTATUS_T)
8405 char *
8406 elfcore_write_pstatus (bfd *abfd,
8407 char *buf,
8408 int *bufsiz,
8409 long pid,
8410 int cursig ATTRIBUTE_UNUSED,
8411 const void *gregs ATTRIBUTE_UNUSED)
8413 const char *note_name = "CORE";
8414 #if defined (HAVE_PSTATUS32_T)
8415 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8417 if (bed->s->elfclass == ELFCLASS32)
8419 pstatus32_t pstat;
8421 memset (&pstat, 0, sizeof (pstat));
8422 pstat.pr_pid = pid & 0xffff;
8423 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8424 NT_PSTATUS, &pstat, sizeof (pstat));
8425 return buf;
8427 else
8428 #endif
8430 pstatus_t pstat;
8432 memset (&pstat, 0, sizeof (pstat));
8433 pstat.pr_pid = pid & 0xffff;
8434 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8435 NT_PSTATUS, &pstat, sizeof (pstat));
8436 return buf;
8439 #endif /* HAVE_PSTATUS_T */
8441 char *
8442 elfcore_write_prfpreg (bfd *abfd,
8443 char *buf,
8444 int *bufsiz,
8445 const void *fpregs,
8446 int size)
8448 const char *note_name = "CORE";
8449 return elfcore_write_note (abfd, buf, bufsiz,
8450 note_name, NT_FPREGSET, fpregs, size);
8453 char *
8454 elfcore_write_prxfpreg (bfd *abfd,
8455 char *buf,
8456 int *bufsiz,
8457 const void *xfpregs,
8458 int size)
8460 char *note_name = "LINUX";
8461 return elfcore_write_note (abfd, buf, bufsiz,
8462 note_name, NT_PRXFPREG, xfpregs, size);
8465 char *
8466 elfcore_write_ppc_vmx (bfd *abfd,
8467 char *buf,
8468 int *bufsiz,
8469 const void *ppc_vmx,
8470 int size)
8472 char *note_name = "LINUX";
8473 return elfcore_write_note (abfd, buf, bufsiz,
8474 note_name, NT_PPC_VMX, ppc_vmx, size);
8477 char *
8478 elfcore_write_register_note (bfd *abfd,
8479 char *buf,
8480 int *bufsiz,
8481 const char *section,
8482 const void *data,
8483 int size)
8485 if (strcmp (section, ".reg2") == 0)
8486 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8487 if (strcmp (section, ".reg-xfp") == 0)
8488 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8489 if (strcmp (section, ".reg-ppc-vmx") == 0)
8490 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8491 return NULL;
8494 static bfd_boolean
8495 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8497 char *p;
8499 p = buf;
8500 while (p < buf + size)
8502 /* FIXME: bad alignment assumption. */
8503 Elf_External_Note *xnp = (Elf_External_Note *) p;
8504 Elf_Internal_Note in;
8506 in.type = H_GET_32 (abfd, xnp->type);
8508 in.namesz = H_GET_32 (abfd, xnp->namesz);
8509 in.namedata = xnp->name;
8511 in.descsz = H_GET_32 (abfd, xnp->descsz);
8512 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8513 in.descpos = offset + (in.descdata - buf);
8515 switch (bfd_get_format (abfd))
8517 default:
8518 return TRUE;
8520 case bfd_core:
8521 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8523 if (! elfcore_grok_netbsd_note (abfd, &in))
8524 return FALSE;
8526 else if (CONST_STRNEQ (in.namedata, "QNX"))
8528 if (! elfcore_grok_nto_note (abfd, &in))
8529 return FALSE;
8531 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8533 if (! elfcore_grok_spu_note (abfd, &in))
8534 return FALSE;
8536 else
8538 if (! elfcore_grok_note (abfd, &in))
8539 return FALSE;
8541 break;
8543 case bfd_object:
8544 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8546 if (! elfobj_grok_gnu_note (abfd, &in))
8547 return FALSE;
8549 break;
8552 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8555 return TRUE;
8558 static bfd_boolean
8559 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8561 char *buf;
8563 if (size <= 0)
8564 return TRUE;
8566 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8567 return FALSE;
8569 buf = bfd_malloc (size);
8570 if (buf == NULL)
8571 return FALSE;
8573 if (bfd_bread (buf, size, abfd) != size
8574 || !elf_parse_notes (abfd, buf, size, offset))
8576 free (buf);
8577 return FALSE;
8580 free (buf);
8581 return TRUE;
8584 /* Providing external access to the ELF program header table. */
8586 /* Return an upper bound on the number of bytes required to store a
8587 copy of ABFD's program header table entries. Return -1 if an error
8588 occurs; bfd_get_error will return an appropriate code. */
8590 long
8591 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8593 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8595 bfd_set_error (bfd_error_wrong_format);
8596 return -1;
8599 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8602 /* Copy ABFD's program header table entries to *PHDRS. The entries
8603 will be stored as an array of Elf_Internal_Phdr structures, as
8604 defined in include/elf/internal.h. To find out how large the
8605 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8607 Return the number of program header table entries read, or -1 if an
8608 error occurs; bfd_get_error will return an appropriate code. */
8611 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8613 int num_phdrs;
8615 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8617 bfd_set_error (bfd_error_wrong_format);
8618 return -1;
8621 num_phdrs = elf_elfheader (abfd)->e_phnum;
8622 memcpy (phdrs, elf_tdata (abfd)->phdr,
8623 num_phdrs * sizeof (Elf_Internal_Phdr));
8625 return num_phdrs;
8628 enum elf_reloc_type_class
8629 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8631 return reloc_class_normal;
8634 /* For RELA architectures, return the relocation value for a
8635 relocation against a local symbol. */
8637 bfd_vma
8638 _bfd_elf_rela_local_sym (bfd *abfd,
8639 Elf_Internal_Sym *sym,
8640 asection **psec,
8641 Elf_Internal_Rela *rel)
8643 asection *sec = *psec;
8644 bfd_vma relocation;
8646 relocation = (sec->output_section->vma
8647 + sec->output_offset
8648 + sym->st_value);
8649 if ((sec->flags & SEC_MERGE)
8650 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8651 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8653 rel->r_addend =
8654 _bfd_merged_section_offset (abfd, psec,
8655 elf_section_data (sec)->sec_info,
8656 sym->st_value + rel->r_addend);
8657 if (sec != *psec)
8659 /* If we have changed the section, and our original section is
8660 marked with SEC_EXCLUDE, it means that the original
8661 SEC_MERGE section has been completely subsumed in some
8662 other SEC_MERGE section. In this case, we need to leave
8663 some info around for --emit-relocs. */
8664 if ((sec->flags & SEC_EXCLUDE) != 0)
8665 sec->kept_section = *psec;
8666 sec = *psec;
8668 rel->r_addend -= relocation;
8669 rel->r_addend += sec->output_section->vma + sec->output_offset;
8671 return relocation;
8674 bfd_vma
8675 _bfd_elf_rel_local_sym (bfd *abfd,
8676 Elf_Internal_Sym *sym,
8677 asection **psec,
8678 bfd_vma addend)
8680 asection *sec = *psec;
8682 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8683 return sym->st_value + addend;
8685 return _bfd_merged_section_offset (abfd, psec,
8686 elf_section_data (sec)->sec_info,
8687 sym->st_value + addend);
8690 bfd_vma
8691 _bfd_elf_section_offset (bfd *abfd,
8692 struct bfd_link_info *info,
8693 asection *sec,
8694 bfd_vma offset)
8696 switch (sec->sec_info_type)
8698 case ELF_INFO_TYPE_STABS:
8699 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8700 offset);
8701 case ELF_INFO_TYPE_EH_FRAME:
8702 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8703 default:
8704 return offset;
8708 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8709 reconstruct an ELF file by reading the segments out of remote memory
8710 based on the ELF file header at EHDR_VMA and the ELF program headers it
8711 points to. If not null, *LOADBASEP is filled in with the difference
8712 between the VMAs from which the segments were read, and the VMAs the
8713 file headers (and hence BFD's idea of each section's VMA) put them at.
8715 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8716 remote memory at target address VMA into the local buffer at MYADDR; it
8717 should return zero on success or an `errno' code on failure. TEMPL must
8718 be a BFD for an ELF target with the word size and byte order found in
8719 the remote memory. */
8721 bfd *
8722 bfd_elf_bfd_from_remote_memory
8723 (bfd *templ,
8724 bfd_vma ehdr_vma,
8725 bfd_vma *loadbasep,
8726 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8728 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8729 (templ, ehdr_vma, loadbasep, target_read_memory);
8732 long
8733 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8734 long symcount ATTRIBUTE_UNUSED,
8735 asymbol **syms ATTRIBUTE_UNUSED,
8736 long dynsymcount,
8737 asymbol **dynsyms,
8738 asymbol **ret)
8740 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8741 asection *relplt;
8742 asymbol *s;
8743 const char *relplt_name;
8744 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8745 arelent *p;
8746 long count, i, n;
8747 size_t size;
8748 Elf_Internal_Shdr *hdr;
8749 char *names;
8750 asection *plt;
8752 *ret = NULL;
8754 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8755 return 0;
8757 if (dynsymcount <= 0)
8758 return 0;
8760 if (!bed->plt_sym_val)
8761 return 0;
8763 relplt_name = bed->relplt_name;
8764 if (relplt_name == NULL)
8765 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
8766 relplt = bfd_get_section_by_name (abfd, relplt_name);
8767 if (relplt == NULL)
8768 return 0;
8770 hdr = &elf_section_data (relplt)->this_hdr;
8771 if (hdr->sh_link != elf_dynsymtab (abfd)
8772 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8773 return 0;
8775 plt = bfd_get_section_by_name (abfd, ".plt");
8776 if (plt == NULL)
8777 return 0;
8779 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8780 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8781 return -1;
8783 count = relplt->size / hdr->sh_entsize;
8784 size = count * sizeof (asymbol);
8785 p = relplt->relocation;
8786 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8787 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8789 s = *ret = bfd_malloc (size);
8790 if (s == NULL)
8791 return -1;
8793 names = (char *) (s + count);
8794 p = relplt->relocation;
8795 n = 0;
8796 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8798 size_t len;
8799 bfd_vma addr;
8801 addr = bed->plt_sym_val (i, plt, p);
8802 if (addr == (bfd_vma) -1)
8803 continue;
8805 *s = **p->sym_ptr_ptr;
8806 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8807 we are defining a symbol, ensure one of them is set. */
8808 if ((s->flags & BSF_LOCAL) == 0)
8809 s->flags |= BSF_GLOBAL;
8810 s->flags |= BSF_SYNTHETIC;
8811 s->section = plt;
8812 s->value = addr - plt->vma;
8813 s->name = names;
8814 s->udata.p = NULL;
8815 len = strlen ((*p->sym_ptr_ptr)->name);
8816 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8817 names += len;
8818 memcpy (names, "@plt", sizeof ("@plt"));
8819 names += sizeof ("@plt");
8820 ++s, ++n;
8823 return n;
8826 /* It is only used by x86-64 so far. */
8827 asection _bfd_elf_large_com_section
8828 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
8829 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
8831 void
8832 _bfd_elf_set_osabi (bfd * abfd,
8833 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8835 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8837 i_ehdrp = elf_elfheader (abfd);
8839 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8843 /* Return TRUE for ELF symbol types that represent functions.
8844 This is the default version of this function, which is sufficient for
8845 most targets. It returns true if TYPE is STT_FUNC. */
8847 bfd_boolean
8848 _bfd_elf_is_function_type (unsigned int type)
8850 return (type == STT_FUNC);