SEGMENT_OVERLAPS: Check that LMAs overlap as well.
[binutils.git] / bfd / elf.c
blob9637ee6f5178e0fee6d71dc792303c55197d4049
1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* SECTION
23 ELF backends
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet. */
33 /* For sparc64-cross-sparc32. */
34 #define _SYSCALL32
35 #include "bfd.h"
36 #include "sysdep.h"
37 #include "bfdlink.h"
38 #include "libbfd.h"
39 #define ARCH_SIZE 0
40 #include "elf-bfd.h"
41 #include "libiberty.h"
43 static INLINE struct elf_segment_map *make_mapping
44 PARAMS ((bfd *, asection **, unsigned int, unsigned int, bfd_boolean));
45 static bfd_boolean map_sections_to_segments
46 PARAMS ((bfd *));
47 static int elf_sort_sections
48 PARAMS ((const PTR, const PTR));
49 static bfd_boolean assign_file_positions_for_segments
50 PARAMS ((bfd *));
51 static bfd_boolean assign_file_positions_except_relocs
52 PARAMS ((bfd *));
53 static bfd_boolean prep_headers
54 PARAMS ((bfd *));
55 static bfd_boolean swap_out_syms
56 PARAMS ((bfd *, struct bfd_strtab_hash **, int));
57 static bfd_boolean copy_private_bfd_data
58 PARAMS ((bfd *, bfd *));
59 static char *elf_read
60 PARAMS ((bfd *, file_ptr, bfd_size_type));
61 static const char *group_signature
62 PARAMS ((bfd *, Elf_Internal_Shdr *));
63 static bfd_boolean setup_group
64 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
65 static void merge_sections_remove_hook
66 PARAMS ((bfd *, asection *));
67 static void elf_fake_sections
68 PARAMS ((bfd *, asection *, PTR));
69 static bfd_boolean assign_section_numbers
70 PARAMS ((bfd *));
71 static INLINE int sym_is_global
72 PARAMS ((bfd *, asymbol *));
73 static bfd_boolean elf_map_symbols
74 PARAMS ((bfd *));
75 static bfd_size_type get_program_header_size
76 PARAMS ((bfd *));
77 static bfd_boolean elfcore_read_notes
78 PARAMS ((bfd *, file_ptr, bfd_size_type));
79 static bfd_boolean elf_find_function
80 PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **,
81 const char **));
82 static int elfcore_make_pid
83 PARAMS ((bfd *));
84 static bfd_boolean elfcore_maybe_make_sect
85 PARAMS ((bfd *, char *, asection *));
86 static bfd_boolean elfcore_make_note_pseudosection
87 PARAMS ((bfd *, char *, Elf_Internal_Note *));
88 static bfd_boolean elfcore_grok_prfpreg
89 PARAMS ((bfd *, Elf_Internal_Note *));
90 static bfd_boolean elfcore_grok_prxfpreg
91 PARAMS ((bfd *, Elf_Internal_Note *));
92 static bfd_boolean elfcore_grok_note
93 PARAMS ((bfd *, Elf_Internal_Note *));
94 static bfd_boolean elfcore_netbsd_get_lwpid
95 PARAMS ((Elf_Internal_Note *, int *));
96 static bfd_boolean elfcore_grok_netbsd_procinfo
97 PARAMS ((bfd *, Elf_Internal_Note *));
98 static bfd_boolean elfcore_grok_netbsd_note
99 PARAMS ((bfd *, Elf_Internal_Note *));
101 /* Swap version information in and out. The version information is
102 currently size independent. If that ever changes, this code will
103 need to move into elfcode.h. */
105 /* Swap in a Verdef structure. */
107 void
108 _bfd_elf_swap_verdef_in (abfd, src, dst)
109 bfd *abfd;
110 const Elf_External_Verdef *src;
111 Elf_Internal_Verdef *dst;
113 dst->vd_version = H_GET_16 (abfd, src->vd_version);
114 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
115 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
116 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
117 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
118 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
119 dst->vd_next = H_GET_32 (abfd, src->vd_next);
122 /* Swap out a Verdef structure. */
124 void
125 _bfd_elf_swap_verdef_out (abfd, src, dst)
126 bfd *abfd;
127 const Elf_Internal_Verdef *src;
128 Elf_External_Verdef *dst;
130 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
131 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
132 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
133 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
134 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
135 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
136 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
139 /* Swap in a Verdaux structure. */
141 void
142 _bfd_elf_swap_verdaux_in (abfd, src, dst)
143 bfd *abfd;
144 const Elf_External_Verdaux *src;
145 Elf_Internal_Verdaux *dst;
147 dst->vda_name = H_GET_32 (abfd, src->vda_name);
148 dst->vda_next = H_GET_32 (abfd, src->vda_next);
151 /* Swap out a Verdaux structure. */
153 void
154 _bfd_elf_swap_verdaux_out (abfd, src, dst)
155 bfd *abfd;
156 const Elf_Internal_Verdaux *src;
157 Elf_External_Verdaux *dst;
159 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
160 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
163 /* Swap in a Verneed structure. */
165 void
166 _bfd_elf_swap_verneed_in (abfd, src, dst)
167 bfd *abfd;
168 const Elf_External_Verneed *src;
169 Elf_Internal_Verneed *dst;
171 dst->vn_version = H_GET_16 (abfd, src->vn_version);
172 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
173 dst->vn_file = H_GET_32 (abfd, src->vn_file);
174 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
175 dst->vn_next = H_GET_32 (abfd, src->vn_next);
178 /* Swap out a Verneed structure. */
180 void
181 _bfd_elf_swap_verneed_out (abfd, src, dst)
182 bfd *abfd;
183 const Elf_Internal_Verneed *src;
184 Elf_External_Verneed *dst;
186 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
187 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
188 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
189 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
190 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
193 /* Swap in a Vernaux structure. */
195 void
196 _bfd_elf_swap_vernaux_in (abfd, src, dst)
197 bfd *abfd;
198 const Elf_External_Vernaux *src;
199 Elf_Internal_Vernaux *dst;
201 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
202 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
203 dst->vna_other = H_GET_16 (abfd, src->vna_other);
204 dst->vna_name = H_GET_32 (abfd, src->vna_name);
205 dst->vna_next = H_GET_32 (abfd, src->vna_next);
208 /* Swap out a Vernaux structure. */
210 void
211 _bfd_elf_swap_vernaux_out (abfd, src, dst)
212 bfd *abfd;
213 const Elf_Internal_Vernaux *src;
214 Elf_External_Vernaux *dst;
216 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
217 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
218 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
219 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
220 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
223 /* Swap in a Versym structure. */
225 void
226 _bfd_elf_swap_versym_in (abfd, src, dst)
227 bfd *abfd;
228 const Elf_External_Versym *src;
229 Elf_Internal_Versym *dst;
231 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
234 /* Swap out a Versym structure. */
236 void
237 _bfd_elf_swap_versym_out (abfd, src, dst)
238 bfd *abfd;
239 const Elf_Internal_Versym *src;
240 Elf_External_Versym *dst;
242 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
245 /* Standard ELF hash function. Do not change this function; you will
246 cause invalid hash tables to be generated. */
248 unsigned long
249 bfd_elf_hash (namearg)
250 const char *namearg;
252 const unsigned char *name = (const unsigned char *) namearg;
253 unsigned long h = 0;
254 unsigned long g;
255 int ch;
257 while ((ch = *name++) != '\0')
259 h = (h << 4) + ch;
260 if ((g = (h & 0xf0000000)) != 0)
262 h ^= g >> 24;
263 /* The ELF ABI says `h &= ~g', but this is equivalent in
264 this case and on some machines one insn instead of two. */
265 h ^= g;
268 return h;
271 /* Read a specified number of bytes at a specified offset in an ELF
272 file, into a newly allocated buffer, and return a pointer to the
273 buffer. */
275 static char *
276 elf_read (abfd, offset, size)
277 bfd *abfd;
278 file_ptr offset;
279 bfd_size_type size;
281 char *buf;
283 if ((buf = bfd_alloc (abfd, size)) == NULL)
284 return NULL;
285 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
286 return NULL;
287 if (bfd_bread ((PTR) buf, size, abfd) != size)
289 if (bfd_get_error () != bfd_error_system_call)
290 bfd_set_error (bfd_error_file_truncated);
291 return NULL;
293 return buf;
296 bfd_boolean
297 bfd_elf_mkobject (abfd)
298 bfd *abfd;
300 /* This just does initialization. */
301 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
302 bfd_size_type amt = sizeof (struct elf_obj_tdata);
303 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
304 if (elf_tdata (abfd) == 0)
305 return FALSE;
306 /* Since everything is done at close time, do we need any
307 initialization? */
309 return TRUE;
312 bfd_boolean
313 bfd_elf_mkcorefile (abfd)
314 bfd *abfd;
316 /* I think this can be done just like an object file. */
317 return bfd_elf_mkobject (abfd);
320 char *
321 bfd_elf_get_str_section (abfd, shindex)
322 bfd *abfd;
323 unsigned int shindex;
325 Elf_Internal_Shdr **i_shdrp;
326 char *shstrtab = NULL;
327 file_ptr offset;
328 bfd_size_type shstrtabsize;
330 i_shdrp = elf_elfsections (abfd);
331 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
332 return 0;
334 shstrtab = (char *) i_shdrp[shindex]->contents;
335 if (shstrtab == NULL)
337 /* No cached one, attempt to read, and cache what we read. */
338 offset = i_shdrp[shindex]->sh_offset;
339 shstrtabsize = i_shdrp[shindex]->sh_size;
340 shstrtab = elf_read (abfd, offset, shstrtabsize);
341 i_shdrp[shindex]->contents = (PTR) shstrtab;
343 return shstrtab;
346 char *
347 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
348 bfd *abfd;
349 unsigned int shindex;
350 unsigned int strindex;
352 Elf_Internal_Shdr *hdr;
354 if (strindex == 0)
355 return "";
357 hdr = elf_elfsections (abfd)[shindex];
359 if (hdr->contents == NULL
360 && bfd_elf_get_str_section (abfd, shindex) == NULL)
361 return NULL;
363 if (strindex >= hdr->sh_size)
365 (*_bfd_error_handler)
366 (_("%s: invalid string offset %u >= %lu for section `%s'"),
367 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
368 ((shindex == elf_elfheader(abfd)->e_shstrndx
369 && strindex == hdr->sh_name)
370 ? ".shstrtab"
371 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
372 return "";
375 return ((char *) hdr->contents) + strindex;
378 /* Read and convert symbols to internal format.
379 SYMCOUNT specifies the number of symbols to read, starting from
380 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
381 are non-NULL, they are used to store the internal symbols, external
382 symbols, and symbol section index extensions, respectively. */
384 Elf_Internal_Sym *
385 bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, symoffset,
386 intsym_buf, extsym_buf, extshndx_buf)
387 bfd *ibfd;
388 Elf_Internal_Shdr *symtab_hdr;
389 size_t symcount;
390 size_t symoffset;
391 Elf_Internal_Sym *intsym_buf;
392 PTR extsym_buf;
393 Elf_External_Sym_Shndx *extshndx_buf;
395 Elf_Internal_Shdr *shndx_hdr;
396 PTR alloc_ext;
397 const bfd_byte *esym;
398 Elf_External_Sym_Shndx *alloc_extshndx;
399 Elf_External_Sym_Shndx *shndx;
400 Elf_Internal_Sym *isym;
401 Elf_Internal_Sym *isymend;
402 struct elf_backend_data *bed;
403 size_t extsym_size;
404 bfd_size_type amt;
405 file_ptr pos;
407 if (symcount == 0)
408 return intsym_buf;
410 /* Normal syms might have section extension entries. */
411 shndx_hdr = NULL;
412 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
413 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
415 /* Read the symbols. */
416 alloc_ext = NULL;
417 alloc_extshndx = NULL;
418 bed = get_elf_backend_data (ibfd);
419 extsym_size = bed->s->sizeof_sym;
420 amt = symcount * extsym_size;
421 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
422 if (extsym_buf == NULL)
424 alloc_ext = bfd_malloc (amt);
425 extsym_buf = alloc_ext;
427 if (extsym_buf == NULL
428 || bfd_seek (ibfd, pos, SEEK_SET) != 0
429 || bfd_bread (extsym_buf, amt, ibfd) != amt)
431 intsym_buf = NULL;
432 goto out;
435 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
436 extshndx_buf = NULL;
437 else
439 amt = symcount * sizeof (Elf_External_Sym_Shndx);
440 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
441 if (extshndx_buf == NULL)
443 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
444 extshndx_buf = alloc_extshndx;
446 if (extshndx_buf == NULL
447 || bfd_seek (ibfd, pos, SEEK_SET) != 0
448 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
450 intsym_buf = NULL;
451 goto out;
455 if (intsym_buf == NULL)
457 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
458 intsym_buf = (Elf_Internal_Sym *) bfd_malloc (amt);
459 if (intsym_buf == NULL)
460 goto out;
463 /* Convert the symbols to internal form. */
464 isymend = intsym_buf + symcount;
465 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
466 isym < isymend;
467 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
468 (*bed->s->swap_symbol_in) (ibfd, esym, (const PTR) shndx, isym);
470 out:
471 if (alloc_ext != NULL)
472 free (alloc_ext);
473 if (alloc_extshndx != NULL)
474 free (alloc_extshndx);
476 return intsym_buf;
479 /* Look up a symbol name. */
480 const char *
481 bfd_elf_local_sym_name (abfd, isym)
482 bfd *abfd;
483 Elf_Internal_Sym *isym;
485 unsigned int iname = isym->st_name;
486 unsigned int shindex = elf_tdata (abfd)->symtab_hdr.sh_link;
487 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION)
489 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
490 shindex = elf_elfheader (abfd)->e_shstrndx;
493 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
496 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
497 sections. The first element is the flags, the rest are section
498 pointers. */
500 typedef union elf_internal_group {
501 Elf_Internal_Shdr *shdr;
502 unsigned int flags;
503 } Elf_Internal_Group;
505 /* Return the name of the group signature symbol. Why isn't the
506 signature just a string? */
508 static const char *
509 group_signature (abfd, ghdr)
510 bfd *abfd;
511 Elf_Internal_Shdr *ghdr;
513 Elf_Internal_Shdr *hdr;
514 unsigned char esym[sizeof (Elf64_External_Sym)];
515 Elf_External_Sym_Shndx eshndx;
516 Elf_Internal_Sym isym;
518 /* First we need to ensure the symbol table is available. */
519 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
520 return NULL;
522 /* Go read the symbol. */
523 hdr = &elf_tdata (abfd)->symtab_hdr;
524 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
525 &isym, esym, &eshndx) == NULL)
526 return NULL;
528 return bfd_elf_local_sym_name (abfd, &isym);
531 /* Set next_in_group list pointer, and group name for NEWSECT. */
533 static bfd_boolean
534 setup_group (abfd, hdr, newsect)
535 bfd *abfd;
536 Elf_Internal_Shdr *hdr;
537 asection *newsect;
539 unsigned int num_group = elf_tdata (abfd)->num_group;
541 /* If num_group is zero, read in all SHT_GROUP sections. The count
542 is set to -1 if there are no SHT_GROUP sections. */
543 if (num_group == 0)
545 unsigned int i, shnum;
547 /* First count the number of groups. If we have a SHT_GROUP
548 section with just a flag word (ie. sh_size is 4), ignore it. */
549 shnum = elf_numsections (abfd);
550 num_group = 0;
551 for (i = 0; i < shnum; i++)
553 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
554 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
555 num_group += 1;
558 if (num_group == 0)
559 num_group = (unsigned) -1;
560 elf_tdata (abfd)->num_group = num_group;
562 if (num_group > 0)
564 /* We keep a list of elf section headers for group sections,
565 so we can find them quickly. */
566 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
567 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
568 if (elf_tdata (abfd)->group_sect_ptr == NULL)
569 return FALSE;
571 num_group = 0;
572 for (i = 0; i < shnum; i++)
574 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
575 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
577 unsigned char *src;
578 Elf_Internal_Group *dest;
580 /* Add to list of sections. */
581 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
582 num_group += 1;
584 /* Read the raw contents. */
585 BFD_ASSERT (sizeof (*dest) >= 4);
586 amt = shdr->sh_size * sizeof (*dest) / 4;
587 shdr->contents = bfd_alloc (abfd, amt);
588 if (shdr->contents == NULL
589 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
590 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
591 != shdr->sh_size))
592 return FALSE;
594 /* Translate raw contents, a flag word followed by an
595 array of elf section indices all in target byte order,
596 to the flag word followed by an array of elf section
597 pointers. */
598 src = shdr->contents + shdr->sh_size;
599 dest = (Elf_Internal_Group *) (shdr->contents + amt);
600 while (1)
602 unsigned int idx;
604 src -= 4;
605 --dest;
606 idx = H_GET_32 (abfd, src);
607 if (src == shdr->contents)
609 dest->flags = idx;
610 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
611 shdr->bfd_section->flags
612 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
613 break;
615 if (idx >= shnum)
617 ((*_bfd_error_handler)
618 (_("%s: invalid SHT_GROUP entry"),
619 bfd_archive_filename (abfd)));
620 idx = 0;
622 dest->shdr = elf_elfsections (abfd)[idx];
629 if (num_group != (unsigned) -1)
631 unsigned int i;
633 for (i = 0; i < num_group; i++)
635 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
636 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
637 unsigned int n_elt = shdr->sh_size / 4;
639 /* Look through this group's sections to see if current
640 section is a member. */
641 while (--n_elt != 0)
642 if ((++idx)->shdr == hdr)
644 asection *s = NULL;
646 /* We are a member of this group. Go looking through
647 other members to see if any others are linked via
648 next_in_group. */
649 idx = (Elf_Internal_Group *) shdr->contents;
650 n_elt = shdr->sh_size / 4;
651 while (--n_elt != 0)
652 if ((s = (++idx)->shdr->bfd_section) != NULL
653 && elf_next_in_group (s) != NULL)
654 break;
655 if (n_elt != 0)
657 /* Snarf the group name from other member, and
658 insert current section in circular list. */
659 elf_group_name (newsect) = elf_group_name (s);
660 elf_next_in_group (newsect) = elf_next_in_group (s);
661 elf_next_in_group (s) = newsect;
663 else
665 const char *gname;
667 gname = group_signature (abfd, shdr);
668 if (gname == NULL)
669 return FALSE;
670 elf_group_name (newsect) = gname;
672 /* Start a circular list with one element. */
673 elf_next_in_group (newsect) = newsect;
676 /* If the group section has been created, point to the
677 new member. */
678 if (shdr->bfd_section != NULL)
679 elf_next_in_group (shdr->bfd_section) = newsect;
681 i = num_group - 1;
682 break;
687 if (elf_group_name (newsect) == NULL)
689 (*_bfd_error_handler) (_("%s: no group info for section %s"),
690 bfd_archive_filename (abfd), newsect->name);
692 return TRUE;
695 bfd_boolean
696 bfd_elf_discard_group (abfd, group)
697 bfd *abfd ATTRIBUTE_UNUSED;
698 asection *group;
700 asection *first = elf_next_in_group (group);
701 asection *s = first;
703 while (s != NULL)
705 s->output_section = bfd_abs_section_ptr;
706 s = elf_next_in_group (s);
707 /* These lists are circular. */
708 if (s == first)
709 break;
711 return TRUE;
714 /* Make a BFD section from an ELF section. We store a pointer to the
715 BFD section in the bfd_section field of the header. */
717 bfd_boolean
718 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
719 bfd *abfd;
720 Elf_Internal_Shdr *hdr;
721 const char *name;
723 asection *newsect;
724 flagword flags;
725 struct elf_backend_data *bed;
727 if (hdr->bfd_section != NULL)
729 BFD_ASSERT (strcmp (name,
730 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
731 return TRUE;
734 newsect = bfd_make_section_anyway (abfd, name);
735 if (newsect == NULL)
736 return FALSE;
738 newsect->filepos = hdr->sh_offset;
740 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
741 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
742 || ! bfd_set_section_alignment (abfd, newsect,
743 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
744 return FALSE;
746 flags = SEC_NO_FLAGS;
747 if (hdr->sh_type != SHT_NOBITS)
748 flags |= SEC_HAS_CONTENTS;
749 if (hdr->sh_type == SHT_GROUP)
750 flags |= SEC_GROUP | SEC_EXCLUDE;
751 if ((hdr->sh_flags & SHF_ALLOC) != 0)
753 flags |= SEC_ALLOC;
754 if (hdr->sh_type != SHT_NOBITS)
755 flags |= SEC_LOAD;
757 if ((hdr->sh_flags & SHF_WRITE) == 0)
758 flags |= SEC_READONLY;
759 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
760 flags |= SEC_CODE;
761 else if ((flags & SEC_LOAD) != 0)
762 flags |= SEC_DATA;
763 if ((hdr->sh_flags & SHF_MERGE) != 0)
765 flags |= SEC_MERGE;
766 newsect->entsize = hdr->sh_entsize;
767 if ((hdr->sh_flags & SHF_STRINGS) != 0)
768 flags |= SEC_STRINGS;
770 if (hdr->sh_flags & SHF_GROUP)
771 if (!setup_group (abfd, hdr, newsect))
772 return FALSE;
773 if ((hdr->sh_flags & SHF_TLS) != 0)
774 flags |= SEC_THREAD_LOCAL;
776 /* The debugging sections appear to be recognized only by name, not
777 any sort of flag. */
779 static const char *debug_sec_names [] =
781 ".debug",
782 ".gnu.linkonce.wi.",
783 ".line",
784 ".stab"
786 int i;
788 for (i = ARRAY_SIZE (debug_sec_names); i--;)
789 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
790 break;
792 if (i >= 0)
793 flags |= SEC_DEBUGGING;
796 /* As a GNU extension, if the name begins with .gnu.linkonce, we
797 only link a single copy of the section. This is used to support
798 g++. g++ will emit each template expansion in its own section.
799 The symbols will be defined as weak, so that multiple definitions
800 are permitted. The GNU linker extension is to actually discard
801 all but one of the sections. */
802 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
803 && elf_next_in_group (newsect) == NULL)
804 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
806 bed = get_elf_backend_data (abfd);
807 if (bed->elf_backend_section_flags)
808 if (! bed->elf_backend_section_flags (&flags, hdr))
809 return FALSE;
811 if (! bfd_set_section_flags (abfd, newsect, flags))
812 return FALSE;
814 if ((flags & SEC_ALLOC) != 0)
816 Elf_Internal_Phdr *phdr;
817 unsigned int i;
819 /* Look through the phdrs to see if we need to adjust the lma.
820 If all the p_paddr fields are zero, we ignore them, since
821 some ELF linkers produce such output. */
822 phdr = elf_tdata (abfd)->phdr;
823 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
825 if (phdr->p_paddr != 0)
826 break;
828 if (i < elf_elfheader (abfd)->e_phnum)
830 phdr = elf_tdata (abfd)->phdr;
831 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
833 /* This section is part of this segment if its file
834 offset plus size lies within the segment's memory
835 span and, if the section is loaded, the extent of the
836 loaded data lies within the extent of the segment.
838 Note - we used to check the p_paddr field as well, and
839 refuse to set the LMA if it was 0. This is wrong
840 though, as a perfectly valid initialised segment can
841 have a p_paddr of zero. Some architectures, eg ARM,
842 place special significance on the address 0 and
843 executables need to be able to have a segment which
844 covers this address. */
845 if (phdr->p_type == PT_LOAD
846 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
847 && (hdr->sh_offset + hdr->sh_size
848 <= phdr->p_offset + phdr->p_memsz)
849 && ((flags & SEC_LOAD) == 0
850 || (hdr->sh_offset + hdr->sh_size
851 <= phdr->p_offset + phdr->p_filesz)))
853 if ((flags & SEC_LOAD) == 0)
854 newsect->lma = (phdr->p_paddr
855 + hdr->sh_addr - phdr->p_vaddr);
856 else
857 /* We used to use the same adjustment for SEC_LOAD
858 sections, but that doesn't work if the segment
859 is packed with code from multiple VMAs.
860 Instead we calculate the section LMA based on
861 the segment LMA. It is assumed that the
862 segment will contain sections with contiguous
863 LMAs, even if the VMAs are not. */
864 newsect->lma = (phdr->p_paddr
865 + hdr->sh_offset - phdr->p_offset);
867 /* With contiguous segments, we can't tell from file
868 offsets whether a section with zero size should
869 be placed at the end of one segment or the
870 beginning of the next. Decide based on vaddr. */
871 if (hdr->sh_addr >= phdr->p_vaddr
872 && (hdr->sh_addr + hdr->sh_size
873 <= phdr->p_vaddr + phdr->p_memsz))
874 break;
880 hdr->bfd_section = newsect;
881 elf_section_data (newsect)->this_hdr = *hdr;
883 return TRUE;
887 INTERNAL_FUNCTION
888 bfd_elf_find_section
890 SYNOPSIS
891 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
893 DESCRIPTION
894 Helper functions for GDB to locate the string tables.
895 Since BFD hides string tables from callers, GDB needs to use an
896 internal hook to find them. Sun's .stabstr, in particular,
897 isn't even pointed to by the .stab section, so ordinary
898 mechanisms wouldn't work to find it, even if we had some.
901 struct elf_internal_shdr *
902 bfd_elf_find_section (abfd, name)
903 bfd *abfd;
904 char *name;
906 Elf_Internal_Shdr **i_shdrp;
907 char *shstrtab;
908 unsigned int max;
909 unsigned int i;
911 i_shdrp = elf_elfsections (abfd);
912 if (i_shdrp != NULL)
914 shstrtab = bfd_elf_get_str_section (abfd,
915 elf_elfheader (abfd)->e_shstrndx);
916 if (shstrtab != NULL)
918 max = elf_numsections (abfd);
919 for (i = 1; i < max; i++)
920 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
921 return i_shdrp[i];
924 return 0;
927 const char *const bfd_elf_section_type_names[] = {
928 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
929 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
930 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
933 /* ELF relocs are against symbols. If we are producing relocateable
934 output, and the reloc is against an external symbol, and nothing
935 has given us any additional addend, the resulting reloc will also
936 be against the same symbol. In such a case, we don't want to
937 change anything about the way the reloc is handled, since it will
938 all be done at final link time. Rather than put special case code
939 into bfd_perform_relocation, all the reloc types use this howto
940 function. It just short circuits the reloc if producing
941 relocateable output against an external symbol. */
943 bfd_reloc_status_type
944 bfd_elf_generic_reloc (abfd,
945 reloc_entry,
946 symbol,
947 data,
948 input_section,
949 output_bfd,
950 error_message)
951 bfd *abfd ATTRIBUTE_UNUSED;
952 arelent *reloc_entry;
953 asymbol *symbol;
954 PTR data ATTRIBUTE_UNUSED;
955 asection *input_section;
956 bfd *output_bfd;
957 char **error_message ATTRIBUTE_UNUSED;
959 if (output_bfd != (bfd *) NULL
960 && (symbol->flags & BSF_SECTION_SYM) == 0
961 && (! reloc_entry->howto->partial_inplace
962 || reloc_entry->addend == 0))
964 reloc_entry->address += input_section->output_offset;
965 return bfd_reloc_ok;
968 return bfd_reloc_continue;
971 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
973 static void
974 merge_sections_remove_hook (abfd, sec)
975 bfd *abfd ATTRIBUTE_UNUSED;
976 asection *sec;
978 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
979 sec->sec_info_type = ELF_INFO_TYPE_NONE;
982 /* Finish SHF_MERGE section merging. */
984 bfd_boolean
985 _bfd_elf_merge_sections (abfd, info)
986 bfd *abfd;
987 struct bfd_link_info *info;
989 if (!is_elf_hash_table (info))
990 return FALSE;
991 if (elf_hash_table (info)->merge_info)
992 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
993 merge_sections_remove_hook);
994 return TRUE;
997 void
998 _bfd_elf_link_just_syms (sec, info)
999 asection *sec;
1000 struct bfd_link_info *info;
1002 sec->output_section = bfd_abs_section_ptr;
1003 sec->output_offset = sec->vma;
1004 if (!is_elf_hash_table (info))
1005 return;
1007 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
1010 /* Copy the program header and other data from one object module to
1011 another. */
1013 bfd_boolean
1014 _bfd_elf_copy_private_bfd_data (ibfd, obfd)
1015 bfd *ibfd;
1016 bfd *obfd;
1018 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1019 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1020 return TRUE;
1022 BFD_ASSERT (!elf_flags_init (obfd)
1023 || (elf_elfheader (obfd)->e_flags
1024 == elf_elfheader (ibfd)->e_flags));
1026 elf_gp (obfd) = elf_gp (ibfd);
1027 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1028 elf_flags_init (obfd) = TRUE;
1029 return TRUE;
1032 /* Print out the program headers. */
1034 bfd_boolean
1035 _bfd_elf_print_private_bfd_data (abfd, farg)
1036 bfd *abfd;
1037 PTR farg;
1039 FILE *f = (FILE *) farg;
1040 Elf_Internal_Phdr *p;
1041 asection *s;
1042 bfd_byte *dynbuf = NULL;
1044 p = elf_tdata (abfd)->phdr;
1045 if (p != NULL)
1047 unsigned int i, c;
1049 fprintf (f, _("\nProgram Header:\n"));
1050 c = elf_elfheader (abfd)->e_phnum;
1051 for (i = 0; i < c; i++, p++)
1053 const char *pt;
1054 char buf[20];
1056 switch (p->p_type)
1058 case PT_NULL: pt = "NULL"; break;
1059 case PT_LOAD: pt = "LOAD"; break;
1060 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1061 case PT_INTERP: pt = "INTERP"; break;
1062 case PT_NOTE: pt = "NOTE"; break;
1063 case PT_SHLIB: pt = "SHLIB"; break;
1064 case PT_PHDR: pt = "PHDR"; break;
1065 case PT_TLS: pt = "TLS"; break;
1066 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1067 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
1069 fprintf (f, "%8s off 0x", pt);
1070 bfd_fprintf_vma (abfd, f, p->p_offset);
1071 fprintf (f, " vaddr 0x");
1072 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1073 fprintf (f, " paddr 0x");
1074 bfd_fprintf_vma (abfd, f, p->p_paddr);
1075 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1076 fprintf (f, " filesz 0x");
1077 bfd_fprintf_vma (abfd, f, p->p_filesz);
1078 fprintf (f, " memsz 0x");
1079 bfd_fprintf_vma (abfd, f, p->p_memsz);
1080 fprintf (f, " flags %c%c%c",
1081 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1082 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1083 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1084 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1085 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1086 fprintf (f, "\n");
1090 s = bfd_get_section_by_name (abfd, ".dynamic");
1091 if (s != NULL)
1093 int elfsec;
1094 unsigned long shlink;
1095 bfd_byte *extdyn, *extdynend;
1096 size_t extdynsize;
1097 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1099 fprintf (f, _("\nDynamic Section:\n"));
1101 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1102 if (dynbuf == NULL)
1103 goto error_return;
1104 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1105 s->_raw_size))
1106 goto error_return;
1108 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1109 if (elfsec == -1)
1110 goto error_return;
1111 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1113 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1114 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1116 extdyn = dynbuf;
1117 extdynend = extdyn + s->_raw_size;
1118 for (; extdyn < extdynend; extdyn += extdynsize)
1120 Elf_Internal_Dyn dyn;
1121 const char *name;
1122 char ab[20];
1123 bfd_boolean stringp;
1125 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1127 if (dyn.d_tag == DT_NULL)
1128 break;
1130 stringp = FALSE;
1131 switch (dyn.d_tag)
1133 default:
1134 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1135 name = ab;
1136 break;
1138 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1139 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1140 case DT_PLTGOT: name = "PLTGOT"; break;
1141 case DT_HASH: name = "HASH"; break;
1142 case DT_STRTAB: name = "STRTAB"; break;
1143 case DT_SYMTAB: name = "SYMTAB"; break;
1144 case DT_RELA: name = "RELA"; break;
1145 case DT_RELASZ: name = "RELASZ"; break;
1146 case DT_RELAENT: name = "RELAENT"; break;
1147 case DT_STRSZ: name = "STRSZ"; break;
1148 case DT_SYMENT: name = "SYMENT"; break;
1149 case DT_INIT: name = "INIT"; break;
1150 case DT_FINI: name = "FINI"; break;
1151 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1152 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1153 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1154 case DT_REL: name = "REL"; break;
1155 case DT_RELSZ: name = "RELSZ"; break;
1156 case DT_RELENT: name = "RELENT"; break;
1157 case DT_PLTREL: name = "PLTREL"; break;
1158 case DT_DEBUG: name = "DEBUG"; break;
1159 case DT_TEXTREL: name = "TEXTREL"; break;
1160 case DT_JMPREL: name = "JMPREL"; break;
1161 case DT_BIND_NOW: name = "BIND_NOW"; break;
1162 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1163 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1164 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1165 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1166 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1167 case DT_FLAGS: name = "FLAGS"; break;
1168 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1169 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1170 case DT_CHECKSUM: name = "CHECKSUM"; break;
1171 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1172 case DT_MOVEENT: name = "MOVEENT"; break;
1173 case DT_MOVESZ: name = "MOVESZ"; break;
1174 case DT_FEATURE: name = "FEATURE"; break;
1175 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1176 case DT_SYMINSZ: name = "SYMINSZ"; break;
1177 case DT_SYMINENT: name = "SYMINENT"; break;
1178 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1179 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1180 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1181 case DT_PLTPAD: name = "PLTPAD"; break;
1182 case DT_MOVETAB: name = "MOVETAB"; break;
1183 case DT_SYMINFO: name = "SYMINFO"; break;
1184 case DT_RELACOUNT: name = "RELACOUNT"; break;
1185 case DT_RELCOUNT: name = "RELCOUNT"; break;
1186 case DT_FLAGS_1: name = "FLAGS_1"; break;
1187 case DT_VERSYM: name = "VERSYM"; break;
1188 case DT_VERDEF: name = "VERDEF"; break;
1189 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1190 case DT_VERNEED: name = "VERNEED"; break;
1191 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1192 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1193 case DT_USED: name = "USED"; break;
1194 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1197 fprintf (f, " %-11s ", name);
1198 if (! stringp)
1199 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1200 else
1202 const char *string;
1203 unsigned int tagv = dyn.d_un.d_val;
1205 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1206 if (string == NULL)
1207 goto error_return;
1208 fprintf (f, "%s", string);
1210 fprintf (f, "\n");
1213 free (dynbuf);
1214 dynbuf = NULL;
1217 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1218 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1220 if (! _bfd_elf_slurp_version_tables (abfd))
1221 return FALSE;
1224 if (elf_dynverdef (abfd) != 0)
1226 Elf_Internal_Verdef *t;
1228 fprintf (f, _("\nVersion definitions:\n"));
1229 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1231 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1232 t->vd_flags, t->vd_hash, t->vd_nodename);
1233 if (t->vd_auxptr->vda_nextptr != NULL)
1235 Elf_Internal_Verdaux *a;
1237 fprintf (f, "\t");
1238 for (a = t->vd_auxptr->vda_nextptr;
1239 a != NULL;
1240 a = a->vda_nextptr)
1241 fprintf (f, "%s ", a->vda_nodename);
1242 fprintf (f, "\n");
1247 if (elf_dynverref (abfd) != 0)
1249 Elf_Internal_Verneed *t;
1251 fprintf (f, _("\nVersion References:\n"));
1252 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1254 Elf_Internal_Vernaux *a;
1256 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1257 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1258 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1259 a->vna_flags, a->vna_other, a->vna_nodename);
1263 return TRUE;
1265 error_return:
1266 if (dynbuf != NULL)
1267 free (dynbuf);
1268 return FALSE;
1271 /* Display ELF-specific fields of a symbol. */
1273 void
1274 bfd_elf_print_symbol (abfd, filep, symbol, how)
1275 bfd *abfd;
1276 PTR filep;
1277 asymbol *symbol;
1278 bfd_print_symbol_type how;
1280 FILE *file = (FILE *) filep;
1281 switch (how)
1283 case bfd_print_symbol_name:
1284 fprintf (file, "%s", symbol->name);
1285 break;
1286 case bfd_print_symbol_more:
1287 fprintf (file, "elf ");
1288 bfd_fprintf_vma (abfd, file, symbol->value);
1289 fprintf (file, " %lx", (long) symbol->flags);
1290 break;
1291 case bfd_print_symbol_all:
1293 const char *section_name;
1294 const char *name = NULL;
1295 struct elf_backend_data *bed;
1296 unsigned char st_other;
1297 bfd_vma val;
1299 section_name = symbol->section ? symbol->section->name : "(*none*)";
1301 bed = get_elf_backend_data (abfd);
1302 if (bed->elf_backend_print_symbol_all)
1303 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1305 if (name == NULL)
1307 name = symbol->name;
1308 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1311 fprintf (file, " %s\t", section_name);
1312 /* Print the "other" value for a symbol. For common symbols,
1313 we've already printed the size; now print the alignment.
1314 For other symbols, we have no specified alignment, and
1315 we've printed the address; now print the size. */
1316 if (bfd_is_com_section (symbol->section))
1317 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1318 else
1319 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1320 bfd_fprintf_vma (abfd, file, val);
1322 /* If we have version information, print it. */
1323 if (elf_tdata (abfd)->dynversym_section != 0
1324 && (elf_tdata (abfd)->dynverdef_section != 0
1325 || elf_tdata (abfd)->dynverref_section != 0))
1327 unsigned int vernum;
1328 const char *version_string;
1330 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1332 if (vernum == 0)
1333 version_string = "";
1334 else if (vernum == 1)
1335 version_string = "Base";
1336 else if (vernum <= elf_tdata (abfd)->cverdefs)
1337 version_string =
1338 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1339 else
1341 Elf_Internal_Verneed *t;
1343 version_string = "";
1344 for (t = elf_tdata (abfd)->verref;
1345 t != NULL;
1346 t = t->vn_nextref)
1348 Elf_Internal_Vernaux *a;
1350 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1352 if (a->vna_other == vernum)
1354 version_string = a->vna_nodename;
1355 break;
1361 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1362 fprintf (file, " %-11s", version_string);
1363 else
1365 int i;
1367 fprintf (file, " (%s)", version_string);
1368 for (i = 10 - strlen (version_string); i > 0; --i)
1369 putc (' ', file);
1373 /* If the st_other field is not zero, print it. */
1374 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1376 switch (st_other)
1378 case 0: break;
1379 case STV_INTERNAL: fprintf (file, " .internal"); break;
1380 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1381 case STV_PROTECTED: fprintf (file, " .protected"); break;
1382 default:
1383 /* Some other non-defined flags are also present, so print
1384 everything hex. */
1385 fprintf (file, " 0x%02x", (unsigned int) st_other);
1388 fprintf (file, " %s", name);
1390 break;
1394 /* Create an entry in an ELF linker hash table. */
1396 struct bfd_hash_entry *
1397 _bfd_elf_link_hash_newfunc (entry, table, string)
1398 struct bfd_hash_entry *entry;
1399 struct bfd_hash_table *table;
1400 const char *string;
1402 /* Allocate the structure if it has not already been allocated by a
1403 subclass. */
1404 if (entry == NULL)
1406 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1407 if (entry == NULL)
1408 return entry;
1411 /* Call the allocation method of the superclass. */
1412 entry = _bfd_link_hash_newfunc (entry, table, string);
1413 if (entry != NULL)
1415 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1416 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1418 /* Set local fields. */
1419 ret->indx = -1;
1420 ret->dynindx = -1;
1421 ret->dynstr_index = 0;
1422 ret->elf_hash_value = 0;
1423 ret->weakdef = NULL;
1424 ret->linker_section_pointer = NULL;
1425 ret->verinfo.verdef = NULL;
1426 ret->vtable_entries_size = 0;
1427 ret->vtable_entries_used = NULL;
1428 ret->vtable_parent = NULL;
1429 ret->got = htab->init_refcount;
1430 ret->plt = htab->init_refcount;
1431 ret->size = 0;
1432 ret->type = STT_NOTYPE;
1433 ret->other = 0;
1434 /* Assume that we have been called by a non-ELF symbol reader.
1435 This flag is then reset by the code which reads an ELF input
1436 file. This ensures that a symbol created by a non-ELF symbol
1437 reader will have the flag set correctly. */
1438 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1441 return entry;
1444 /* Copy data from an indirect symbol to its direct symbol, hiding the
1445 old indirect symbol. Also used for copying flags to a weakdef. */
1447 void
1448 _bfd_elf_link_hash_copy_indirect (bed, dir, ind)
1449 struct elf_backend_data *bed;
1450 struct elf_link_hash_entry *dir, *ind;
1452 bfd_signed_vma tmp;
1453 bfd_signed_vma lowest_valid = bed->can_refcount;
1455 /* Copy down any references that we may have already seen to the
1456 symbol which just became indirect. */
1458 dir->elf_link_hash_flags |=
1459 (ind->elf_link_hash_flags
1460 & (ELF_LINK_HASH_REF_DYNAMIC
1461 | ELF_LINK_HASH_REF_REGULAR
1462 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1463 | ELF_LINK_NON_GOT_REF));
1465 if (ind->root.type != bfd_link_hash_indirect)
1466 return;
1468 /* Copy over the global and procedure linkage table refcount entries.
1469 These may have been already set up by a check_relocs routine. */
1470 tmp = dir->got.refcount;
1471 if (tmp < lowest_valid)
1473 dir->got.refcount = ind->got.refcount;
1474 ind->got.refcount = tmp;
1476 else
1477 BFD_ASSERT (ind->got.refcount < lowest_valid);
1479 tmp = dir->plt.refcount;
1480 if (tmp < lowest_valid)
1482 dir->plt.refcount = ind->plt.refcount;
1483 ind->plt.refcount = tmp;
1485 else
1486 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1488 if (dir->dynindx == -1)
1490 dir->dynindx = ind->dynindx;
1491 dir->dynstr_index = ind->dynstr_index;
1492 ind->dynindx = -1;
1493 ind->dynstr_index = 0;
1495 else
1496 BFD_ASSERT (ind->dynindx == -1);
1499 void
1500 _bfd_elf_link_hash_hide_symbol (info, h, force_local)
1501 struct bfd_link_info *info;
1502 struct elf_link_hash_entry *h;
1503 bfd_boolean force_local;
1505 h->plt = elf_hash_table (info)->init_offset;
1506 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1507 if (force_local)
1509 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1510 if (h->dynindx != -1)
1512 h->dynindx = -1;
1513 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1514 h->dynstr_index);
1519 /* Initialize an ELF linker hash table. */
1521 bfd_boolean
1522 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1523 struct elf_link_hash_table *table;
1524 bfd *abfd;
1525 struct bfd_hash_entry *(*newfunc)
1526 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
1527 const char *));
1529 bfd_boolean ret;
1531 table->dynamic_sections_created = FALSE;
1532 table->dynobj = NULL;
1533 /* Make sure can_refcount is extended to the width and signedness of
1534 init_refcount before we subtract one from it. */
1535 table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1536 table->init_refcount.refcount -= 1;
1537 table->init_offset.offset = -(bfd_vma) 1;
1538 /* The first dynamic symbol is a dummy. */
1539 table->dynsymcount = 1;
1540 table->dynstr = NULL;
1541 table->bucketcount = 0;
1542 table->needed = NULL;
1543 table->hgot = NULL;
1544 table->stab_info = NULL;
1545 table->merge_info = NULL;
1546 memset (&table->eh_info, 0, sizeof (table->eh_info));
1547 table->dynlocal = NULL;
1548 table->runpath = NULL;
1549 table->tls_segment = NULL;
1550 table->loaded = NULL;
1552 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1553 table->root.type = bfd_link_elf_hash_table;
1555 return ret;
1558 /* Create an ELF linker hash table. */
1560 struct bfd_link_hash_table *
1561 _bfd_elf_link_hash_table_create (abfd)
1562 bfd *abfd;
1564 struct elf_link_hash_table *ret;
1565 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1567 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
1568 if (ret == (struct elf_link_hash_table *) NULL)
1569 return NULL;
1571 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1573 free (ret);
1574 return NULL;
1577 return &ret->root;
1580 /* This is a hook for the ELF emulation code in the generic linker to
1581 tell the backend linker what file name to use for the DT_NEEDED
1582 entry for a dynamic object. The generic linker passes name as an
1583 empty string to indicate that no DT_NEEDED entry should be made. */
1585 void
1586 bfd_elf_set_dt_needed_name (abfd, name)
1587 bfd *abfd;
1588 const char *name;
1590 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1591 && bfd_get_format (abfd) == bfd_object)
1592 elf_dt_name (abfd) = name;
1595 void
1596 bfd_elf_set_dt_needed_soname (abfd, name)
1597 bfd *abfd;
1598 const char *name;
1600 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1601 && bfd_get_format (abfd) == bfd_object)
1602 elf_dt_soname (abfd) = name;
1605 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1606 the linker ELF emulation code. */
1608 struct bfd_link_needed_list *
1609 bfd_elf_get_needed_list (abfd, info)
1610 bfd *abfd ATTRIBUTE_UNUSED;
1611 struct bfd_link_info *info;
1613 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1614 return NULL;
1615 return elf_hash_table (info)->needed;
1618 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1619 hook for the linker ELF emulation code. */
1621 struct bfd_link_needed_list *
1622 bfd_elf_get_runpath_list (abfd, info)
1623 bfd *abfd ATTRIBUTE_UNUSED;
1624 struct bfd_link_info *info;
1626 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1627 return NULL;
1628 return elf_hash_table (info)->runpath;
1631 /* Get the name actually used for a dynamic object for a link. This
1632 is the SONAME entry if there is one. Otherwise, it is the string
1633 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1635 const char *
1636 bfd_elf_get_dt_soname (abfd)
1637 bfd *abfd;
1639 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1640 && bfd_get_format (abfd) == bfd_object)
1641 return elf_dt_name (abfd);
1642 return NULL;
1645 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1646 the ELF linker emulation code. */
1648 bfd_boolean
1649 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1650 bfd *abfd;
1651 struct bfd_link_needed_list **pneeded;
1653 asection *s;
1654 bfd_byte *dynbuf = NULL;
1655 int elfsec;
1656 unsigned long shlink;
1657 bfd_byte *extdyn, *extdynend;
1658 size_t extdynsize;
1659 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1661 *pneeded = NULL;
1663 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1664 || bfd_get_format (abfd) != bfd_object)
1665 return TRUE;
1667 s = bfd_get_section_by_name (abfd, ".dynamic");
1668 if (s == NULL || s->_raw_size == 0)
1669 return TRUE;
1671 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1672 if (dynbuf == NULL)
1673 goto error_return;
1675 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1676 s->_raw_size))
1677 goto error_return;
1679 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1680 if (elfsec == -1)
1681 goto error_return;
1683 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1685 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1686 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1688 extdyn = dynbuf;
1689 extdynend = extdyn + s->_raw_size;
1690 for (; extdyn < extdynend; extdyn += extdynsize)
1692 Elf_Internal_Dyn dyn;
1694 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1696 if (dyn.d_tag == DT_NULL)
1697 break;
1699 if (dyn.d_tag == DT_NEEDED)
1701 const char *string;
1702 struct bfd_link_needed_list *l;
1703 unsigned int tagv = dyn.d_un.d_val;
1704 bfd_size_type amt;
1706 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1707 if (string == NULL)
1708 goto error_return;
1710 amt = sizeof *l;
1711 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1712 if (l == NULL)
1713 goto error_return;
1715 l->by = abfd;
1716 l->name = string;
1717 l->next = *pneeded;
1718 *pneeded = l;
1722 free (dynbuf);
1724 return TRUE;
1726 error_return:
1727 if (dynbuf != NULL)
1728 free (dynbuf);
1729 return FALSE;
1732 /* Allocate an ELF string table--force the first byte to be zero. */
1734 struct bfd_strtab_hash *
1735 _bfd_elf_stringtab_init ()
1737 struct bfd_strtab_hash *ret;
1739 ret = _bfd_stringtab_init ();
1740 if (ret != NULL)
1742 bfd_size_type loc;
1744 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1745 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1746 if (loc == (bfd_size_type) -1)
1748 _bfd_stringtab_free (ret);
1749 ret = NULL;
1752 return ret;
1755 /* ELF .o/exec file reading */
1757 /* Create a new bfd section from an ELF section header. */
1759 bfd_boolean
1760 bfd_section_from_shdr (abfd, shindex)
1761 bfd *abfd;
1762 unsigned int shindex;
1764 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1765 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1766 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1767 const char *name;
1769 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1771 switch (hdr->sh_type)
1773 case SHT_NULL:
1774 /* Inactive section. Throw it away. */
1775 return TRUE;
1777 case SHT_PROGBITS: /* Normal section with contents. */
1778 case SHT_NOBITS: /* .bss section. */
1779 case SHT_HASH: /* .hash section. */
1780 case SHT_NOTE: /* .note section. */
1781 case SHT_INIT_ARRAY: /* .init_array section. */
1782 case SHT_FINI_ARRAY: /* .fini_array section. */
1783 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1784 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1786 case SHT_DYNAMIC: /* Dynamic linking information. */
1787 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1788 return FALSE;
1789 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1791 Elf_Internal_Shdr *dynsymhdr;
1793 /* The shared libraries distributed with hpux11 have a bogus
1794 sh_link field for the ".dynamic" section. Find the
1795 string table for the ".dynsym" section instead. */
1796 if (elf_dynsymtab (abfd) != 0)
1798 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1799 hdr->sh_link = dynsymhdr->sh_link;
1801 else
1803 unsigned int i, num_sec;
1805 num_sec = elf_numsections (abfd);
1806 for (i = 1; i < num_sec; i++)
1808 dynsymhdr = elf_elfsections (abfd)[i];
1809 if (dynsymhdr->sh_type == SHT_DYNSYM)
1811 hdr->sh_link = dynsymhdr->sh_link;
1812 break;
1817 break;
1819 case SHT_SYMTAB: /* A symbol table */
1820 if (elf_onesymtab (abfd) == shindex)
1821 return TRUE;
1823 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1824 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1825 elf_onesymtab (abfd) = shindex;
1826 elf_tdata (abfd)->symtab_hdr = *hdr;
1827 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1828 abfd->flags |= HAS_SYMS;
1830 /* Sometimes a shared object will map in the symbol table. If
1831 SHF_ALLOC is set, and this is a shared object, then we also
1832 treat this section as a BFD section. We can not base the
1833 decision purely on SHF_ALLOC, because that flag is sometimes
1834 set in a relocateable object file, which would confuse the
1835 linker. */
1836 if ((hdr->sh_flags & SHF_ALLOC) != 0
1837 && (abfd->flags & DYNAMIC) != 0
1838 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1839 return FALSE;
1841 return TRUE;
1843 case SHT_DYNSYM: /* A dynamic symbol table */
1844 if (elf_dynsymtab (abfd) == shindex)
1845 return TRUE;
1847 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1848 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1849 elf_dynsymtab (abfd) = shindex;
1850 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1851 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1852 abfd->flags |= HAS_SYMS;
1854 /* Besides being a symbol table, we also treat this as a regular
1855 section, so that objcopy can handle it. */
1856 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1858 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1859 if (elf_symtab_shndx (abfd) == shindex)
1860 return TRUE;
1862 /* Get the associated symbol table. */
1863 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1864 || hdr->sh_link != elf_onesymtab (abfd))
1865 return FALSE;
1867 elf_symtab_shndx (abfd) = shindex;
1868 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1869 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1870 return TRUE;
1872 case SHT_STRTAB: /* A string table */
1873 if (hdr->bfd_section != NULL)
1874 return TRUE;
1875 if (ehdr->e_shstrndx == shindex)
1877 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1878 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1879 return TRUE;
1882 unsigned int i, num_sec;
1884 num_sec = elf_numsections (abfd);
1885 for (i = 1; i < num_sec; i++)
1887 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1888 if (hdr2->sh_link == shindex)
1890 if (! bfd_section_from_shdr (abfd, i))
1891 return FALSE;
1892 if (elf_onesymtab (abfd) == i)
1894 elf_tdata (abfd)->strtab_hdr = *hdr;
1895 elf_elfsections (abfd)[shindex] =
1896 &elf_tdata (abfd)->strtab_hdr;
1897 return TRUE;
1899 if (elf_dynsymtab (abfd) == i)
1901 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1902 elf_elfsections (abfd)[shindex] = hdr =
1903 &elf_tdata (abfd)->dynstrtab_hdr;
1904 /* We also treat this as a regular section, so
1905 that objcopy can handle it. */
1906 break;
1908 #if 0 /* Not handling other string tables specially right now. */
1909 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1910 /* We have a strtab for some random other section. */
1911 newsect = (asection *) hdr2->bfd_section;
1912 if (!newsect)
1913 break;
1914 hdr->bfd_section = newsect;
1915 hdr2 = &elf_section_data (newsect)->str_hdr;
1916 *hdr2 = *hdr;
1917 elf_elfsections (abfd)[shindex] = hdr2;
1918 #endif
1923 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1925 case SHT_REL:
1926 case SHT_RELA:
1927 /* *These* do a lot of work -- but build no sections! */
1929 asection *target_sect;
1930 Elf_Internal_Shdr *hdr2;
1931 unsigned int num_sec = elf_numsections (abfd);
1933 /* Check for a bogus link to avoid crashing. */
1934 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1935 || hdr->sh_link >= num_sec)
1937 ((*_bfd_error_handler)
1938 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1939 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1940 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1943 /* For some incomprehensible reason Oracle distributes
1944 libraries for Solaris in which some of the objects have
1945 bogus sh_link fields. It would be nice if we could just
1946 reject them, but, unfortunately, some people need to use
1947 them. We scan through the section headers; if we find only
1948 one suitable symbol table, we clobber the sh_link to point
1949 to it. I hope this doesn't break anything. */
1950 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1951 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1953 unsigned int scan;
1954 int found;
1956 found = 0;
1957 for (scan = 1; scan < num_sec; scan++)
1959 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1960 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1962 if (found != 0)
1964 found = 0;
1965 break;
1967 found = scan;
1970 if (found != 0)
1971 hdr->sh_link = found;
1974 /* Get the symbol table. */
1975 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1976 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1977 return FALSE;
1979 /* If this reloc section does not use the main symbol table we
1980 don't treat it as a reloc section. BFD can't adequately
1981 represent such a section, so at least for now, we don't
1982 try. We just present it as a normal section. We also
1983 can't use it as a reloc section if it points to the null
1984 section. */
1985 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1986 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1988 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1989 return FALSE;
1990 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1991 if (target_sect == NULL)
1992 return FALSE;
1994 if ((target_sect->flags & SEC_RELOC) == 0
1995 || target_sect->reloc_count == 0)
1996 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1997 else
1999 bfd_size_type amt;
2000 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
2001 amt = sizeof (*hdr2);
2002 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2003 elf_section_data (target_sect)->rel_hdr2 = hdr2;
2005 *hdr2 = *hdr;
2006 elf_elfsections (abfd)[shindex] = hdr2;
2007 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
2008 target_sect->flags |= SEC_RELOC;
2009 target_sect->relocation = NULL;
2010 target_sect->rel_filepos = hdr->sh_offset;
2011 /* In the section to which the relocations apply, mark whether
2012 its relocations are of the REL or RELA variety. */
2013 if (hdr->sh_size != 0)
2014 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
2015 abfd->flags |= HAS_RELOC;
2016 return TRUE;
2018 break;
2020 case SHT_GNU_verdef:
2021 elf_dynverdef (abfd) = shindex;
2022 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2023 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2024 break;
2026 case SHT_GNU_versym:
2027 elf_dynversym (abfd) = shindex;
2028 elf_tdata (abfd)->dynversym_hdr = *hdr;
2029 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2030 break;
2032 case SHT_GNU_verneed:
2033 elf_dynverref (abfd) = shindex;
2034 elf_tdata (abfd)->dynverref_hdr = *hdr;
2035 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2036 break;
2038 case SHT_SHLIB:
2039 return TRUE;
2041 case SHT_GROUP:
2042 /* We need a BFD section for objcopy and relocatable linking,
2043 and it's handy to have the signature available as the section
2044 name. */
2045 name = group_signature (abfd, hdr);
2046 if (name == NULL)
2047 return FALSE;
2048 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
2049 return FALSE;
2050 if (hdr->contents != NULL)
2052 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2053 unsigned int n_elt = hdr->sh_size / 4;
2054 asection *s;
2056 if (idx->flags & GRP_COMDAT)
2057 hdr->bfd_section->flags
2058 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2060 while (--n_elt != 0)
2061 if ((s = (++idx)->shdr->bfd_section) != NULL
2062 && elf_next_in_group (s) != NULL)
2064 elf_next_in_group (hdr->bfd_section) = s;
2065 break;
2068 break;
2070 default:
2071 /* Check for any processor-specific section types. */
2073 if (bed->elf_backend_section_from_shdr)
2074 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
2076 break;
2079 return TRUE;
2082 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2083 Return SEC for sections that have no elf section, and NULL on error. */
2085 asection *
2086 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
2087 bfd *abfd;
2088 struct sym_sec_cache *cache;
2089 asection *sec;
2090 unsigned long r_symndx;
2092 Elf_Internal_Shdr *symtab_hdr;
2093 unsigned char esym[sizeof (Elf64_External_Sym)];
2094 Elf_External_Sym_Shndx eshndx;
2095 Elf_Internal_Sym isym;
2096 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2098 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2099 return cache->sec[ent];
2101 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2102 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2103 &isym, esym, &eshndx) == NULL)
2104 return NULL;
2106 if (cache->abfd != abfd)
2108 memset (cache->indx, -1, sizeof (cache->indx));
2109 cache->abfd = abfd;
2111 cache->indx[ent] = r_symndx;
2112 cache->sec[ent] = sec;
2113 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
2115 asection *s;
2116 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2117 if (s != NULL)
2118 cache->sec[ent] = s;
2120 return cache->sec[ent];
2123 /* Given an ELF section number, retrieve the corresponding BFD
2124 section. */
2126 asection *
2127 bfd_section_from_elf_index (abfd, index)
2128 bfd *abfd;
2129 unsigned int index;
2131 if (index >= elf_numsections (abfd))
2132 return NULL;
2133 return elf_elfsections (abfd)[index]->bfd_section;
2136 bfd_boolean
2137 _bfd_elf_new_section_hook (abfd, sec)
2138 bfd *abfd;
2139 asection *sec;
2141 struct bfd_elf_section_data *sdata;
2143 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2144 if (sdata == NULL)
2146 bfd_size_type amt = sizeof (*sdata);
2147 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
2148 if (sdata == NULL)
2149 return FALSE;
2150 sec->used_by_bfd = (PTR) sdata;
2153 /* Indicate whether or not this section should use RELA relocations. */
2154 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2156 return TRUE;
2159 /* Create a new bfd section from an ELF program header.
2161 Since program segments have no names, we generate a synthetic name
2162 of the form segment<NUM>, where NUM is generally the index in the
2163 program header table. For segments that are split (see below) we
2164 generate the names segment<NUM>a and segment<NUM>b.
2166 Note that some program segments may have a file size that is different than
2167 (less than) the memory size. All this means is that at execution the
2168 system must allocate the amount of memory specified by the memory size,
2169 but only initialize it with the first "file size" bytes read from the
2170 file. This would occur for example, with program segments consisting
2171 of combined data+bss.
2173 To handle the above situation, this routine generates TWO bfd sections
2174 for the single program segment. The first has the length specified by
2175 the file size of the segment, and the second has the length specified
2176 by the difference between the two sizes. In effect, the segment is split
2177 into it's initialized and uninitialized parts.
2181 bfd_boolean
2182 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
2183 bfd *abfd;
2184 Elf_Internal_Phdr *hdr;
2185 int index;
2186 const char *typename;
2188 asection *newsect;
2189 char *name;
2190 char namebuf[64];
2191 size_t len;
2192 int split;
2194 split = ((hdr->p_memsz > 0)
2195 && (hdr->p_filesz > 0)
2196 && (hdr->p_memsz > hdr->p_filesz));
2197 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2198 len = strlen (namebuf) + 1;
2199 name = bfd_alloc (abfd, (bfd_size_type) len);
2200 if (!name)
2201 return FALSE;
2202 memcpy (name, namebuf, len);
2203 newsect = bfd_make_section (abfd, name);
2204 if (newsect == NULL)
2205 return FALSE;
2206 newsect->vma = hdr->p_vaddr;
2207 newsect->lma = hdr->p_paddr;
2208 newsect->_raw_size = hdr->p_filesz;
2209 newsect->filepos = hdr->p_offset;
2210 newsect->flags |= SEC_HAS_CONTENTS;
2211 if (hdr->p_type == PT_LOAD)
2213 newsect->flags |= SEC_ALLOC;
2214 newsect->flags |= SEC_LOAD;
2215 if (hdr->p_flags & PF_X)
2217 /* FIXME: all we known is that it has execute PERMISSION,
2218 may be data. */
2219 newsect->flags |= SEC_CODE;
2222 if (!(hdr->p_flags & PF_W))
2224 newsect->flags |= SEC_READONLY;
2227 if (split)
2229 sprintf (namebuf, "%s%db", typename, index);
2230 len = strlen (namebuf) + 1;
2231 name = bfd_alloc (abfd, (bfd_size_type) len);
2232 if (!name)
2233 return FALSE;
2234 memcpy (name, namebuf, len);
2235 newsect = bfd_make_section (abfd, name);
2236 if (newsect == NULL)
2237 return FALSE;
2238 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2239 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2240 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2241 if (hdr->p_type == PT_LOAD)
2243 newsect->flags |= SEC_ALLOC;
2244 if (hdr->p_flags & PF_X)
2245 newsect->flags |= SEC_CODE;
2247 if (!(hdr->p_flags & PF_W))
2248 newsect->flags |= SEC_READONLY;
2251 return TRUE;
2254 bfd_boolean
2255 bfd_section_from_phdr (abfd, hdr, index)
2256 bfd *abfd;
2257 Elf_Internal_Phdr *hdr;
2258 int index;
2260 struct elf_backend_data *bed;
2262 switch (hdr->p_type)
2264 case PT_NULL:
2265 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2267 case PT_LOAD:
2268 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2270 case PT_DYNAMIC:
2271 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2273 case PT_INTERP:
2274 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2276 case PT_NOTE:
2277 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2278 return FALSE;
2279 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
2280 return FALSE;
2281 return TRUE;
2283 case PT_SHLIB:
2284 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2286 case PT_PHDR:
2287 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2289 default:
2290 /* Check for any processor-specific program segment types.
2291 If no handler for them, default to making "segment" sections. */
2292 bed = get_elf_backend_data (abfd);
2293 if (bed->elf_backend_section_from_phdr)
2294 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2295 else
2296 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2300 /* Initialize REL_HDR, the section-header for new section, containing
2301 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2302 relocations; otherwise, we use REL relocations. */
2304 bfd_boolean
2305 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2306 bfd *abfd;
2307 Elf_Internal_Shdr *rel_hdr;
2308 asection *asect;
2309 bfd_boolean use_rela_p;
2311 char *name;
2312 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2313 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2315 name = bfd_alloc (abfd, amt);
2316 if (name == NULL)
2317 return FALSE;
2318 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2319 rel_hdr->sh_name =
2320 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2321 FALSE);
2322 if (rel_hdr->sh_name == (unsigned int) -1)
2323 return FALSE;
2324 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2325 rel_hdr->sh_entsize = (use_rela_p
2326 ? bed->s->sizeof_rela
2327 : bed->s->sizeof_rel);
2328 rel_hdr->sh_addralign = bed->s->file_align;
2329 rel_hdr->sh_flags = 0;
2330 rel_hdr->sh_addr = 0;
2331 rel_hdr->sh_size = 0;
2332 rel_hdr->sh_offset = 0;
2334 return TRUE;
2337 /* Set up an ELF internal section header for a section. */
2339 static void
2340 elf_fake_sections (abfd, asect, failedptrarg)
2341 bfd *abfd;
2342 asection *asect;
2343 PTR failedptrarg;
2345 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2346 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2347 Elf_Internal_Shdr *this_hdr;
2349 if (*failedptr)
2351 /* We already failed; just get out of the bfd_map_over_sections
2352 loop. */
2353 return;
2356 this_hdr = &elf_section_data (asect)->this_hdr;
2358 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2359 asect->name, FALSE);
2360 if (this_hdr->sh_name == (unsigned long) -1)
2362 *failedptr = TRUE;
2363 return;
2366 this_hdr->sh_flags = 0;
2368 if ((asect->flags & SEC_ALLOC) != 0
2369 || asect->user_set_vma)
2370 this_hdr->sh_addr = asect->vma;
2371 else
2372 this_hdr->sh_addr = 0;
2374 this_hdr->sh_offset = 0;
2375 this_hdr->sh_size = asect->_raw_size;
2376 this_hdr->sh_link = 0;
2377 this_hdr->sh_addralign = 1 << asect->alignment_power;
2378 /* The sh_entsize and sh_info fields may have been set already by
2379 copy_private_section_data. */
2381 this_hdr->bfd_section = asect;
2382 this_hdr->contents = NULL;
2384 /* FIXME: This should not be based on section names. */
2385 if (strcmp (asect->name, ".dynstr") == 0)
2386 this_hdr->sh_type = SHT_STRTAB;
2387 else if (strcmp (asect->name, ".hash") == 0)
2389 this_hdr->sh_type = SHT_HASH;
2390 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2392 else if (strcmp (asect->name, ".dynsym") == 0)
2394 this_hdr->sh_type = SHT_DYNSYM;
2395 this_hdr->sh_entsize = bed->s->sizeof_sym;
2397 else if (strcmp (asect->name, ".dynamic") == 0)
2399 this_hdr->sh_type = SHT_DYNAMIC;
2400 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2402 else if (strncmp (asect->name, ".rela", 5) == 0
2403 && get_elf_backend_data (abfd)->may_use_rela_p)
2405 this_hdr->sh_type = SHT_RELA;
2406 this_hdr->sh_entsize = bed->s->sizeof_rela;
2408 else if (strncmp (asect->name, ".rel", 4) == 0
2409 && get_elf_backend_data (abfd)->may_use_rel_p)
2411 this_hdr->sh_type = SHT_REL;
2412 this_hdr->sh_entsize = bed->s->sizeof_rel;
2414 else if (strcmp (asect->name, ".init_array") == 0)
2415 this_hdr->sh_type = SHT_INIT_ARRAY;
2416 else if (strcmp (asect->name, ".fini_array") == 0)
2417 this_hdr->sh_type = SHT_FINI_ARRAY;
2418 else if (strcmp (asect->name, ".preinit_array") == 0)
2419 this_hdr->sh_type = SHT_PREINIT_ARRAY;
2420 else if (strncmp (asect->name, ".note", 5) == 0)
2421 this_hdr->sh_type = SHT_NOTE;
2422 else if (strncmp (asect->name, ".stab", 5) == 0
2423 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2424 this_hdr->sh_type = SHT_STRTAB;
2425 else if (strcmp (asect->name, ".gnu.version") == 0)
2427 this_hdr->sh_type = SHT_GNU_versym;
2428 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2430 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2432 this_hdr->sh_type = SHT_GNU_verdef;
2433 this_hdr->sh_entsize = 0;
2434 /* objcopy or strip will copy over sh_info, but may not set
2435 cverdefs. The linker will set cverdefs, but sh_info will be
2436 zero. */
2437 if (this_hdr->sh_info == 0)
2438 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2439 else
2440 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2441 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2443 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2445 this_hdr->sh_type = SHT_GNU_verneed;
2446 this_hdr->sh_entsize = 0;
2447 /* objcopy or strip will copy over sh_info, but may not set
2448 cverrefs. The linker will set cverrefs, but sh_info will be
2449 zero. */
2450 if (this_hdr->sh_info == 0)
2451 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2452 else
2453 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2454 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2456 else if ((asect->flags & SEC_GROUP) != 0)
2458 this_hdr->sh_type = SHT_GROUP;
2459 this_hdr->sh_entsize = 4;
2461 else if ((asect->flags & SEC_ALLOC) != 0
2462 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2463 || (asect->flags & SEC_NEVER_LOAD) != 0))
2464 this_hdr->sh_type = SHT_NOBITS;
2465 else
2466 this_hdr->sh_type = SHT_PROGBITS;
2468 if ((asect->flags & SEC_ALLOC) != 0)
2469 this_hdr->sh_flags |= SHF_ALLOC;
2470 if ((asect->flags & SEC_READONLY) == 0)
2471 this_hdr->sh_flags |= SHF_WRITE;
2472 if ((asect->flags & SEC_CODE) != 0)
2473 this_hdr->sh_flags |= SHF_EXECINSTR;
2474 if ((asect->flags & SEC_MERGE) != 0)
2476 this_hdr->sh_flags |= SHF_MERGE;
2477 this_hdr->sh_entsize = asect->entsize;
2478 if ((asect->flags & SEC_STRINGS) != 0)
2479 this_hdr->sh_flags |= SHF_STRINGS;
2481 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2482 this_hdr->sh_flags |= SHF_GROUP;
2483 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2485 this_hdr->sh_flags |= SHF_TLS;
2486 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2488 struct bfd_link_order *o;
2490 this_hdr->sh_size = 0;
2491 for (o = asect->link_order_head; o != NULL; o = o->next)
2492 if (this_hdr->sh_size < o->offset + o->size)
2493 this_hdr->sh_size = o->offset + o->size;
2494 if (this_hdr->sh_size)
2495 this_hdr->sh_type = SHT_NOBITS;
2499 /* Check for processor-specific section types. */
2500 if (bed->elf_backend_fake_sections
2501 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2502 *failedptr = TRUE;
2504 /* If the section has relocs, set up a section header for the
2505 SHT_REL[A] section. If two relocation sections are required for
2506 this section, it is up to the processor-specific back-end to
2507 create the other. */
2508 if ((asect->flags & SEC_RELOC) != 0
2509 && !_bfd_elf_init_reloc_shdr (abfd,
2510 &elf_section_data (asect)->rel_hdr,
2511 asect,
2512 asect->use_rela_p))
2513 *failedptr = TRUE;
2516 /* Fill in the contents of a SHT_GROUP section. */
2518 void
2519 bfd_elf_set_group_contents (abfd, sec, failedptrarg)
2520 bfd *abfd;
2521 asection *sec;
2522 PTR failedptrarg;
2524 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2525 unsigned long symindx;
2526 asection *elt, *first;
2527 unsigned char *loc;
2528 struct bfd_link_order *l;
2529 bfd_boolean gas;
2531 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2532 || *failedptr)
2533 return;
2535 symindx = 0;
2536 if (elf_group_id (sec) != NULL)
2537 symindx = elf_group_id (sec)->udata.i;
2539 if (symindx == 0)
2541 /* If called from the assembler, swap_out_syms will have set up
2542 elf_section_syms; If called for "ld -r", use target_index. */
2543 if (elf_section_syms (abfd) != NULL)
2544 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2545 else
2546 symindx = sec->target_index;
2548 elf_section_data (sec)->this_hdr.sh_info = symindx;
2550 /* The contents won't be allocated for "ld -r" or objcopy. */
2551 gas = TRUE;
2552 if (sec->contents == NULL)
2554 gas = FALSE;
2555 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2557 /* Arrange for the section to be written out. */
2558 elf_section_data (sec)->this_hdr.contents = sec->contents;
2559 if (sec->contents == NULL)
2561 *failedptr = TRUE;
2562 return;
2566 loc = sec->contents + sec->_raw_size;
2568 /* Get the pointer to the first section in the group that gas
2569 squirreled away here. objcopy arranges for this to be set to the
2570 start of the input section group. */
2571 first = elt = elf_next_in_group (sec);
2573 /* First element is a flag word. Rest of section is elf section
2574 indices for all the sections of the group. Write them backwards
2575 just to keep the group in the same order as given in .section
2576 directives, not that it matters. */
2577 while (elt != NULL)
2579 asection *s;
2580 unsigned int idx;
2582 loc -= 4;
2583 s = elt;
2584 if (!gas)
2585 s = s->output_section;
2586 idx = 0;
2587 if (s != NULL)
2588 idx = elf_section_data (s)->this_idx;
2589 H_PUT_32 (abfd, idx, loc);
2590 elt = elf_next_in_group (elt);
2591 if (elt == first)
2592 break;
2595 /* If this is a relocatable link, then the above did nothing because
2596 SEC is the output section. Look through the input sections
2597 instead. */
2598 for (l = sec->link_order_head; l != NULL; l = l->next)
2599 if (l->type == bfd_indirect_link_order
2600 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2603 loc -= 4;
2604 H_PUT_32 (abfd,
2605 elf_section_data (elt->output_section)->this_idx, loc);
2606 elt = elf_next_in_group (elt);
2607 /* During a relocatable link, the lists are circular. */
2609 while (elt != elf_next_in_group (l->u.indirect.section));
2611 /* With ld -r, merging SHT_GROUP sections results in wasted space
2612 due to allowing for the flag word on each input. We may well
2613 duplicate entries too. */
2614 while ((loc -= 4) > sec->contents)
2615 H_PUT_32 (abfd, 0, loc);
2617 if (loc != sec->contents)
2618 abort ();
2620 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2623 /* Assign all ELF section numbers. The dummy first section is handled here
2624 too. The link/info pointers for the standard section types are filled
2625 in here too, while we're at it. */
2627 static bfd_boolean
2628 assign_section_numbers (abfd)
2629 bfd *abfd;
2631 struct elf_obj_tdata *t = elf_tdata (abfd);
2632 asection *sec;
2633 unsigned int section_number, secn;
2634 Elf_Internal_Shdr **i_shdrp;
2635 bfd_size_type amt;
2637 section_number = 1;
2639 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2641 for (sec = abfd->sections; sec; sec = sec->next)
2643 struct bfd_elf_section_data *d = elf_section_data (sec);
2645 if (section_number == SHN_LORESERVE)
2646 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2647 d->this_idx = section_number++;
2648 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2649 if ((sec->flags & SEC_RELOC) == 0)
2650 d->rel_idx = 0;
2651 else
2653 if (section_number == SHN_LORESERVE)
2654 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2655 d->rel_idx = section_number++;
2656 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2659 if (d->rel_hdr2)
2661 if (section_number == SHN_LORESERVE)
2662 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2663 d->rel_idx2 = section_number++;
2664 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2666 else
2667 d->rel_idx2 = 0;
2670 if (section_number == SHN_LORESERVE)
2671 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2672 t->shstrtab_section = section_number++;
2673 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2674 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2676 if (bfd_get_symcount (abfd) > 0)
2678 if (section_number == SHN_LORESERVE)
2679 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2680 t->symtab_section = section_number++;
2681 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2682 if (section_number > SHN_LORESERVE - 2)
2684 if (section_number == SHN_LORESERVE)
2685 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2686 t->symtab_shndx_section = section_number++;
2687 t->symtab_shndx_hdr.sh_name
2688 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2689 ".symtab_shndx", FALSE);
2690 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2691 return FALSE;
2693 if (section_number == SHN_LORESERVE)
2694 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2695 t->strtab_section = section_number++;
2696 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2699 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2700 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2702 elf_numsections (abfd) = section_number;
2703 elf_elfheader (abfd)->e_shnum = section_number;
2704 if (section_number > SHN_LORESERVE)
2705 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2707 /* Set up the list of section header pointers, in agreement with the
2708 indices. */
2709 amt = section_number * sizeof (Elf_Internal_Shdr *);
2710 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
2711 if (i_shdrp == NULL)
2712 return FALSE;
2714 amt = sizeof (Elf_Internal_Shdr);
2715 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
2716 if (i_shdrp[0] == NULL)
2718 bfd_release (abfd, i_shdrp);
2719 return FALSE;
2722 elf_elfsections (abfd) = i_shdrp;
2724 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2725 if (bfd_get_symcount (abfd) > 0)
2727 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2728 if (elf_numsections (abfd) > SHN_LORESERVE)
2730 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2731 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2733 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2734 t->symtab_hdr.sh_link = t->strtab_section;
2736 for (sec = abfd->sections; sec; sec = sec->next)
2738 struct bfd_elf_section_data *d = elf_section_data (sec);
2739 asection *s;
2740 const char *name;
2742 i_shdrp[d->this_idx] = &d->this_hdr;
2743 if (d->rel_idx != 0)
2744 i_shdrp[d->rel_idx] = &d->rel_hdr;
2745 if (d->rel_idx2 != 0)
2746 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2748 /* Fill in the sh_link and sh_info fields while we're at it. */
2750 /* sh_link of a reloc section is the section index of the symbol
2751 table. sh_info is the section index of the section to which
2752 the relocation entries apply. */
2753 if (d->rel_idx != 0)
2755 d->rel_hdr.sh_link = t->symtab_section;
2756 d->rel_hdr.sh_info = d->this_idx;
2758 if (d->rel_idx2 != 0)
2760 d->rel_hdr2->sh_link = t->symtab_section;
2761 d->rel_hdr2->sh_info = d->this_idx;
2764 switch (d->this_hdr.sh_type)
2766 case SHT_REL:
2767 case SHT_RELA:
2768 /* A reloc section which we are treating as a normal BFD
2769 section. sh_link is the section index of the symbol
2770 table. sh_info is the section index of the section to
2771 which the relocation entries apply. We assume that an
2772 allocated reloc section uses the dynamic symbol table.
2773 FIXME: How can we be sure? */
2774 s = bfd_get_section_by_name (abfd, ".dynsym");
2775 if (s != NULL)
2776 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2778 /* We look up the section the relocs apply to by name. */
2779 name = sec->name;
2780 if (d->this_hdr.sh_type == SHT_REL)
2781 name += 4;
2782 else
2783 name += 5;
2784 s = bfd_get_section_by_name (abfd, name);
2785 if (s != NULL)
2786 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2787 break;
2789 case SHT_STRTAB:
2790 /* We assume that a section named .stab*str is a stabs
2791 string section. We look for a section with the same name
2792 but without the trailing ``str'', and set its sh_link
2793 field to point to this section. */
2794 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2795 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2797 size_t len;
2798 char *alc;
2800 len = strlen (sec->name);
2801 alc = (char *) bfd_malloc ((bfd_size_type) (len - 2));
2802 if (alc == NULL)
2803 return FALSE;
2804 memcpy (alc, sec->name, len - 3);
2805 alc[len - 3] = '\0';
2806 s = bfd_get_section_by_name (abfd, alc);
2807 free (alc);
2808 if (s != NULL)
2810 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2812 /* This is a .stab section. */
2813 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2814 elf_section_data (s)->this_hdr.sh_entsize
2815 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2818 break;
2820 case SHT_DYNAMIC:
2821 case SHT_DYNSYM:
2822 case SHT_GNU_verneed:
2823 case SHT_GNU_verdef:
2824 /* sh_link is the section header index of the string table
2825 used for the dynamic entries, or the symbol table, or the
2826 version strings. */
2827 s = bfd_get_section_by_name (abfd, ".dynstr");
2828 if (s != NULL)
2829 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2830 break;
2832 case SHT_HASH:
2833 case SHT_GNU_versym:
2834 /* sh_link is the section header index of the symbol table
2835 this hash table or version table is for. */
2836 s = bfd_get_section_by_name (abfd, ".dynsym");
2837 if (s != NULL)
2838 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2839 break;
2841 case SHT_GROUP:
2842 d->this_hdr.sh_link = t->symtab_section;
2846 for (secn = 1; secn < section_number; ++secn)
2847 if (i_shdrp[secn] == NULL)
2848 i_shdrp[secn] = i_shdrp[0];
2849 else
2850 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2851 i_shdrp[secn]->sh_name);
2852 return TRUE;
2855 /* Map symbol from it's internal number to the external number, moving
2856 all local symbols to be at the head of the list. */
2858 static INLINE int
2859 sym_is_global (abfd, sym)
2860 bfd *abfd;
2861 asymbol *sym;
2863 /* If the backend has a special mapping, use it. */
2864 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2865 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2866 (abfd, sym));
2868 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2869 || bfd_is_und_section (bfd_get_section (sym))
2870 || bfd_is_com_section (bfd_get_section (sym)));
2873 static bfd_boolean
2874 elf_map_symbols (abfd)
2875 bfd *abfd;
2877 unsigned int symcount = bfd_get_symcount (abfd);
2878 asymbol **syms = bfd_get_outsymbols (abfd);
2879 asymbol **sect_syms;
2880 unsigned int num_locals = 0;
2881 unsigned int num_globals = 0;
2882 unsigned int num_locals2 = 0;
2883 unsigned int num_globals2 = 0;
2884 int max_index = 0;
2885 unsigned int idx;
2886 asection *asect;
2887 asymbol **new_syms;
2888 bfd_size_type amt;
2890 #ifdef DEBUG
2891 fprintf (stderr, "elf_map_symbols\n");
2892 fflush (stderr);
2893 #endif
2895 for (asect = abfd->sections; asect; asect = asect->next)
2897 if (max_index < asect->index)
2898 max_index = asect->index;
2901 max_index++;
2902 amt = max_index * sizeof (asymbol *);
2903 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2904 if (sect_syms == NULL)
2905 return FALSE;
2906 elf_section_syms (abfd) = sect_syms;
2907 elf_num_section_syms (abfd) = max_index;
2909 /* Init sect_syms entries for any section symbols we have already
2910 decided to output. */
2911 for (idx = 0; idx < symcount; idx++)
2913 asymbol *sym = syms[idx];
2915 if ((sym->flags & BSF_SECTION_SYM) != 0
2916 && sym->value == 0)
2918 asection *sec;
2920 sec = sym->section;
2922 if (sec->owner != NULL)
2924 if (sec->owner != abfd)
2926 if (sec->output_offset != 0)
2927 continue;
2929 sec = sec->output_section;
2931 /* Empty sections in the input files may have had a
2932 section symbol created for them. (See the comment
2933 near the end of _bfd_generic_link_output_symbols in
2934 linker.c). If the linker script discards such
2935 sections then we will reach this point. Since we know
2936 that we cannot avoid this case, we detect it and skip
2937 the abort and the assignment to the sect_syms array.
2938 To reproduce this particular case try running the
2939 linker testsuite test ld-scripts/weak.exp for an ELF
2940 port that uses the generic linker. */
2941 if (sec->owner == NULL)
2942 continue;
2944 BFD_ASSERT (sec->owner == abfd);
2946 sect_syms[sec->index] = syms[idx];
2951 /* Classify all of the symbols. */
2952 for (idx = 0; idx < symcount; idx++)
2954 if (!sym_is_global (abfd, syms[idx]))
2955 num_locals++;
2956 else
2957 num_globals++;
2960 /* We will be adding a section symbol for each BFD section. Most normal
2961 sections will already have a section symbol in outsymbols, but
2962 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2963 at least in that case. */
2964 for (asect = abfd->sections; asect; asect = asect->next)
2966 if (sect_syms[asect->index] == NULL)
2968 if (!sym_is_global (abfd, asect->symbol))
2969 num_locals++;
2970 else
2971 num_globals++;
2975 /* Now sort the symbols so the local symbols are first. */
2976 amt = (num_locals + num_globals) * sizeof (asymbol *);
2977 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2979 if (new_syms == NULL)
2980 return FALSE;
2982 for (idx = 0; idx < symcount; idx++)
2984 asymbol *sym = syms[idx];
2985 unsigned int i;
2987 if (!sym_is_global (abfd, sym))
2988 i = num_locals2++;
2989 else
2990 i = num_locals + num_globals2++;
2991 new_syms[i] = sym;
2992 sym->udata.i = i + 1;
2994 for (asect = abfd->sections; asect; asect = asect->next)
2996 if (sect_syms[asect->index] == NULL)
2998 asymbol *sym = asect->symbol;
2999 unsigned int i;
3001 sect_syms[asect->index] = sym;
3002 if (!sym_is_global (abfd, sym))
3003 i = num_locals2++;
3004 else
3005 i = num_locals + num_globals2++;
3006 new_syms[i] = sym;
3007 sym->udata.i = i + 1;
3011 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3013 elf_num_locals (abfd) = num_locals;
3014 elf_num_globals (abfd) = num_globals;
3015 return TRUE;
3018 /* Align to the maximum file alignment that could be required for any
3019 ELF data structure. */
3021 static INLINE file_ptr align_file_position
3022 PARAMS ((file_ptr, int));
3023 static INLINE file_ptr
3024 align_file_position (off, align)
3025 file_ptr off;
3026 int align;
3028 return (off + align - 1) & ~(align - 1);
3031 /* Assign a file position to a section, optionally aligning to the
3032 required section alignment. */
3034 INLINE file_ptr
3035 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
3036 Elf_Internal_Shdr *i_shdrp;
3037 file_ptr offset;
3038 bfd_boolean align;
3040 if (align)
3042 unsigned int al;
3044 al = i_shdrp->sh_addralign;
3045 if (al > 1)
3046 offset = BFD_ALIGN (offset, al);
3048 i_shdrp->sh_offset = offset;
3049 if (i_shdrp->bfd_section != NULL)
3050 i_shdrp->bfd_section->filepos = offset;
3051 if (i_shdrp->sh_type != SHT_NOBITS)
3052 offset += i_shdrp->sh_size;
3053 return offset;
3056 /* Compute the file positions we are going to put the sections at, and
3057 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3058 is not NULL, this is being called by the ELF backend linker. */
3060 bfd_boolean
3061 _bfd_elf_compute_section_file_positions (abfd, link_info)
3062 bfd *abfd;
3063 struct bfd_link_info *link_info;
3065 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3066 bfd_boolean failed;
3067 struct bfd_strtab_hash *strtab;
3068 Elf_Internal_Shdr *shstrtab_hdr;
3070 if (abfd->output_has_begun)
3071 return TRUE;
3073 /* Do any elf backend specific processing first. */
3074 if (bed->elf_backend_begin_write_processing)
3075 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3077 if (! prep_headers (abfd))
3078 return FALSE;
3080 /* Post process the headers if necessary. */
3081 if (bed->elf_backend_post_process_headers)
3082 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3084 failed = FALSE;
3085 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3086 if (failed)
3087 return FALSE;
3089 if (!assign_section_numbers (abfd))
3090 return FALSE;
3092 /* The backend linker builds symbol table information itself. */
3093 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3095 /* Non-zero if doing a relocatable link. */
3096 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3098 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3099 return FALSE;
3102 if (link_info == NULL)
3104 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3105 if (failed)
3106 return FALSE;
3109 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3110 /* sh_name was set in prep_headers. */
3111 shstrtab_hdr->sh_type = SHT_STRTAB;
3112 shstrtab_hdr->sh_flags = 0;
3113 shstrtab_hdr->sh_addr = 0;
3114 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3115 shstrtab_hdr->sh_entsize = 0;
3116 shstrtab_hdr->sh_link = 0;
3117 shstrtab_hdr->sh_info = 0;
3118 /* sh_offset is set in assign_file_positions_except_relocs. */
3119 shstrtab_hdr->sh_addralign = 1;
3121 if (!assign_file_positions_except_relocs (abfd))
3122 return FALSE;
3124 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3126 file_ptr off;
3127 Elf_Internal_Shdr *hdr;
3129 off = elf_tdata (abfd)->next_file_pos;
3131 hdr = &elf_tdata (abfd)->symtab_hdr;
3132 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3134 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3135 if (hdr->sh_size != 0)
3136 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3138 hdr = &elf_tdata (abfd)->strtab_hdr;
3139 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3141 elf_tdata (abfd)->next_file_pos = off;
3143 /* Now that we know where the .strtab section goes, write it
3144 out. */
3145 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3146 || ! _bfd_stringtab_emit (abfd, strtab))
3147 return FALSE;
3148 _bfd_stringtab_free (strtab);
3151 abfd->output_has_begun = TRUE;
3153 return TRUE;
3156 /* Create a mapping from a set of sections to a program segment. */
3158 static INLINE struct elf_segment_map *
3159 make_mapping (abfd, sections, from, to, phdr)
3160 bfd *abfd;
3161 asection **sections;
3162 unsigned int from;
3163 unsigned int to;
3164 bfd_boolean phdr;
3166 struct elf_segment_map *m;
3167 unsigned int i;
3168 asection **hdrpp;
3169 bfd_size_type amt;
3171 amt = sizeof (struct elf_segment_map);
3172 amt += (to - from - 1) * sizeof (asection *);
3173 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3174 if (m == NULL)
3175 return NULL;
3176 m->next = NULL;
3177 m->p_type = PT_LOAD;
3178 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3179 m->sections[i - from] = *hdrpp;
3180 m->count = to - from;
3182 if (from == 0 && phdr)
3184 /* Include the headers in the first PT_LOAD segment. */
3185 m->includes_filehdr = 1;
3186 m->includes_phdrs = 1;
3189 return m;
3192 /* Set up a mapping from BFD sections to program segments. */
3194 static bfd_boolean
3195 map_sections_to_segments (abfd)
3196 bfd *abfd;
3198 asection **sections = NULL;
3199 asection *s;
3200 unsigned int i;
3201 unsigned int count;
3202 struct elf_segment_map *mfirst;
3203 struct elf_segment_map **pm;
3204 struct elf_segment_map *m;
3205 asection *last_hdr;
3206 unsigned int phdr_index;
3207 bfd_vma maxpagesize;
3208 asection **hdrpp;
3209 bfd_boolean phdr_in_segment = TRUE;
3210 bfd_boolean writable;
3211 int tls_count = 0;
3212 asection *first_tls = NULL;
3213 asection *dynsec, *eh_frame_hdr;
3214 bfd_size_type amt;
3216 if (elf_tdata (abfd)->segment_map != NULL)
3217 return TRUE;
3219 if (bfd_count_sections (abfd) == 0)
3220 return TRUE;
3222 /* Select the allocated sections, and sort them. */
3224 amt = bfd_count_sections (abfd) * sizeof (asection *);
3225 sections = (asection **) bfd_malloc (amt);
3226 if (sections == NULL)
3227 goto error_return;
3229 i = 0;
3230 for (s = abfd->sections; s != NULL; s = s->next)
3232 if ((s->flags & SEC_ALLOC) != 0)
3234 sections[i] = s;
3235 ++i;
3238 BFD_ASSERT (i <= bfd_count_sections (abfd));
3239 count = i;
3241 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3243 /* Build the mapping. */
3245 mfirst = NULL;
3246 pm = &mfirst;
3248 /* If we have a .interp section, then create a PT_PHDR segment for
3249 the program headers and a PT_INTERP segment for the .interp
3250 section. */
3251 s = bfd_get_section_by_name (abfd, ".interp");
3252 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3254 amt = sizeof (struct elf_segment_map);
3255 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3256 if (m == NULL)
3257 goto error_return;
3258 m->next = NULL;
3259 m->p_type = PT_PHDR;
3260 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3261 m->p_flags = PF_R | PF_X;
3262 m->p_flags_valid = 1;
3263 m->includes_phdrs = 1;
3265 *pm = m;
3266 pm = &m->next;
3268 amt = sizeof (struct elf_segment_map);
3269 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3270 if (m == NULL)
3271 goto error_return;
3272 m->next = NULL;
3273 m->p_type = PT_INTERP;
3274 m->count = 1;
3275 m->sections[0] = s;
3277 *pm = m;
3278 pm = &m->next;
3281 /* Look through the sections. We put sections in the same program
3282 segment when the start of the second section can be placed within
3283 a few bytes of the end of the first section. */
3284 last_hdr = NULL;
3285 phdr_index = 0;
3286 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3287 writable = FALSE;
3288 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3289 if (dynsec != NULL
3290 && (dynsec->flags & SEC_LOAD) == 0)
3291 dynsec = NULL;
3293 /* Deal with -Ttext or something similar such that the first section
3294 is not adjacent to the program headers. This is an
3295 approximation, since at this point we don't know exactly how many
3296 program headers we will need. */
3297 if (count > 0)
3299 bfd_size_type phdr_size;
3301 phdr_size = elf_tdata (abfd)->program_header_size;
3302 if (phdr_size == 0)
3303 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3304 if ((abfd->flags & D_PAGED) == 0
3305 || sections[0]->lma < phdr_size
3306 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3307 phdr_in_segment = FALSE;
3310 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3312 asection *hdr;
3313 bfd_boolean new_segment;
3315 hdr = *hdrpp;
3317 /* See if this section and the last one will fit in the same
3318 segment. */
3320 if (last_hdr == NULL)
3322 /* If we don't have a segment yet, then we don't need a new
3323 one (we build the last one after this loop). */
3324 new_segment = FALSE;
3326 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3328 /* If this section has a different relation between the
3329 virtual address and the load address, then we need a new
3330 segment. */
3331 new_segment = TRUE;
3333 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3334 < BFD_ALIGN (hdr->lma, maxpagesize))
3336 /* If putting this section in this segment would force us to
3337 skip a page in the segment, then we need a new segment. */
3338 new_segment = TRUE;
3340 else if ((last_hdr->flags & SEC_LOAD) == 0
3341 && (hdr->flags & SEC_LOAD) != 0)
3343 /* We don't want to put a loadable section after a
3344 nonloadable section in the same segment. */
3345 new_segment = TRUE;
3347 else if ((abfd->flags & D_PAGED) == 0)
3349 /* If the file is not demand paged, which means that we
3350 don't require the sections to be correctly aligned in the
3351 file, then there is no other reason for a new segment. */
3352 new_segment = FALSE;
3354 else if (! writable
3355 && (hdr->flags & SEC_READONLY) == 0
3356 && (((last_hdr->lma + last_hdr->_raw_size - 1)
3357 & ~(maxpagesize - 1))
3358 != (hdr->lma & ~(maxpagesize - 1))))
3360 /* We don't want to put a writable section in a read only
3361 segment, unless they are on the same page in memory
3362 anyhow. We already know that the last section does not
3363 bring us past the current section on the page, so the
3364 only case in which the new section is not on the same
3365 page as the previous section is when the previous section
3366 ends precisely on a page boundary. */
3367 new_segment = TRUE;
3369 else
3371 /* Otherwise, we can use the same segment. */
3372 new_segment = FALSE;
3375 if (! new_segment)
3377 if ((hdr->flags & SEC_READONLY) == 0)
3378 writable = TRUE;
3379 last_hdr = hdr;
3380 continue;
3383 /* We need a new program segment. We must create a new program
3384 header holding all the sections from phdr_index until hdr. */
3386 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3387 if (m == NULL)
3388 goto error_return;
3390 *pm = m;
3391 pm = &m->next;
3393 if ((hdr->flags & SEC_READONLY) == 0)
3394 writable = TRUE;
3395 else
3396 writable = FALSE;
3398 last_hdr = hdr;
3399 phdr_index = i;
3400 phdr_in_segment = FALSE;
3403 /* Create a final PT_LOAD program segment. */
3404 if (last_hdr != NULL)
3406 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3407 if (m == NULL)
3408 goto error_return;
3410 *pm = m;
3411 pm = &m->next;
3414 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3415 if (dynsec != NULL)
3417 amt = sizeof (struct elf_segment_map);
3418 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3419 if (m == NULL)
3420 goto error_return;
3421 m->next = NULL;
3422 m->p_type = PT_DYNAMIC;
3423 m->count = 1;
3424 m->sections[0] = dynsec;
3426 *pm = m;
3427 pm = &m->next;
3430 /* For each loadable .note section, add a PT_NOTE segment. We don't
3431 use bfd_get_section_by_name, because if we link together
3432 nonloadable .note sections and loadable .note sections, we will
3433 generate two .note sections in the output file. FIXME: Using
3434 names for section types is bogus anyhow. */
3435 for (s = abfd->sections; s != NULL; s = s->next)
3437 if ((s->flags & SEC_LOAD) != 0
3438 && strncmp (s->name, ".note", 5) == 0)
3440 amt = sizeof (struct elf_segment_map);
3441 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3442 if (m == NULL)
3443 goto error_return;
3444 m->next = NULL;
3445 m->p_type = PT_NOTE;
3446 m->count = 1;
3447 m->sections[0] = s;
3449 *pm = m;
3450 pm = &m->next;
3452 if (s->flags & SEC_THREAD_LOCAL)
3454 if (! tls_count)
3455 first_tls = s;
3456 tls_count++;
3460 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3461 if (tls_count > 0)
3463 int i;
3465 amt = sizeof (struct elf_segment_map);
3466 amt += (tls_count - 1) * sizeof (asection *);
3467 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3468 if (m == NULL)
3469 goto error_return;
3470 m->next = NULL;
3471 m->p_type = PT_TLS;
3472 m->count = tls_count;
3473 /* Mandated PF_R. */
3474 m->p_flags = PF_R;
3475 m->p_flags_valid = 1;
3476 for (i = 0; i < tls_count; ++i)
3478 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3479 m->sections[i] = first_tls;
3480 first_tls = first_tls->next;
3483 *pm = m;
3484 pm = &m->next;
3487 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3488 segment. */
3489 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3490 if (eh_frame_hdr != NULL
3491 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3493 amt = sizeof (struct elf_segment_map);
3494 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3495 if (m == NULL)
3496 goto error_return;
3497 m->next = NULL;
3498 m->p_type = PT_GNU_EH_FRAME;
3499 m->count = 1;
3500 m->sections[0] = eh_frame_hdr->output_section;
3502 *pm = m;
3503 pm = &m->next;
3506 free (sections);
3507 sections = NULL;
3509 elf_tdata (abfd)->segment_map = mfirst;
3510 return TRUE;
3512 error_return:
3513 if (sections != NULL)
3514 free (sections);
3515 return FALSE;
3518 /* Sort sections by address. */
3520 static int
3521 elf_sort_sections (arg1, arg2)
3522 const PTR arg1;
3523 const PTR arg2;
3525 const asection *sec1 = *(const asection **) arg1;
3526 const asection *sec2 = *(const asection **) arg2;
3527 bfd_size_type size1, size2;
3529 /* Sort by LMA first, since this is the address used to
3530 place the section into a segment. */
3531 if (sec1->lma < sec2->lma)
3532 return -1;
3533 else if (sec1->lma > sec2->lma)
3534 return 1;
3536 /* Then sort by VMA. Normally the LMA and the VMA will be
3537 the same, and this will do nothing. */
3538 if (sec1->vma < sec2->vma)
3539 return -1;
3540 else if (sec1->vma > sec2->vma)
3541 return 1;
3543 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3545 #define TOEND(x) (((x)->flags & (SEC_LOAD|SEC_THREAD_LOCAL)) == 0)
3547 if (TOEND (sec1))
3549 if (TOEND (sec2))
3551 /* If the indicies are the same, do not return 0
3552 here, but continue to try the next comparison. */
3553 if (sec1->target_index - sec2->target_index != 0)
3554 return sec1->target_index - sec2->target_index;
3556 else
3557 return 1;
3559 else if (TOEND (sec2))
3560 return -1;
3562 #undef TOEND
3564 /* Sort by size, to put zero sized sections
3565 before others at the same address. */
3567 size1 = (sec1->flags & SEC_LOAD) ? sec1->_raw_size : 0;
3568 size2 = (sec2->flags & SEC_LOAD) ? sec2->_raw_size : 0;
3570 if (size1 < size2)
3571 return -1;
3572 if (size1 > size2)
3573 return 1;
3575 return sec1->target_index - sec2->target_index;
3578 /* Assign file positions to the sections based on the mapping from
3579 sections to segments. This function also sets up some fields in
3580 the file header, and writes out the program headers. */
3582 static bfd_boolean
3583 assign_file_positions_for_segments (abfd)
3584 bfd *abfd;
3586 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3587 unsigned int count;
3588 struct elf_segment_map *m;
3589 unsigned int alloc;
3590 Elf_Internal_Phdr *phdrs;
3591 file_ptr off, voff;
3592 bfd_vma filehdr_vaddr, filehdr_paddr;
3593 bfd_vma phdrs_vaddr, phdrs_paddr;
3594 Elf_Internal_Phdr *p;
3595 bfd_size_type amt;
3597 if (elf_tdata (abfd)->segment_map == NULL)
3599 if (! map_sections_to_segments (abfd))
3600 return FALSE;
3602 else
3604 /* The placement algorithm assumes that non allocated sections are
3605 not in PT_LOAD segments. We ensure this here by removing such
3606 sections from the segment map. */
3607 for (m = elf_tdata (abfd)->segment_map;
3608 m != NULL;
3609 m = m->next)
3611 unsigned int new_count;
3612 unsigned int i;
3614 if (m->p_type != PT_LOAD)
3615 continue;
3617 new_count = 0;
3618 for (i = 0; i < m->count; i ++)
3620 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3622 if (i != new_count)
3623 m->sections[new_count] = m->sections[i];
3625 new_count ++;
3629 if (new_count != m->count)
3630 m->count = new_count;
3634 if (bed->elf_backend_modify_segment_map)
3636 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3637 return FALSE;
3640 count = 0;
3641 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3642 ++count;
3644 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3645 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3646 elf_elfheader (abfd)->e_phnum = count;
3648 if (count == 0)
3649 return TRUE;
3651 /* If we already counted the number of program segments, make sure
3652 that we allocated enough space. This happens when SIZEOF_HEADERS
3653 is used in a linker script. */
3654 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3655 if (alloc != 0 && count > alloc)
3657 ((*_bfd_error_handler)
3658 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3659 bfd_get_filename (abfd), alloc, count));
3660 bfd_set_error (bfd_error_bad_value);
3661 return FALSE;
3664 if (alloc == 0)
3665 alloc = count;
3667 amt = alloc * sizeof (Elf_Internal_Phdr);
3668 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3669 if (phdrs == NULL)
3670 return FALSE;
3672 off = bed->s->sizeof_ehdr;
3673 off += alloc * bed->s->sizeof_phdr;
3675 filehdr_vaddr = 0;
3676 filehdr_paddr = 0;
3677 phdrs_vaddr = 0;
3678 phdrs_paddr = 0;
3680 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3681 m != NULL;
3682 m = m->next, p++)
3684 unsigned int i;
3685 asection **secpp;
3687 /* If elf_segment_map is not from map_sections_to_segments, the
3688 sections may not be correctly ordered. NOTE: sorting should
3689 not be done to the PT_NOTE section of a corefile, which may
3690 contain several pseudo-sections artificially created by bfd.
3691 Sorting these pseudo-sections breaks things badly. */
3692 if (m->count > 1
3693 && !(elf_elfheader (abfd)->e_type == ET_CORE
3694 && m->p_type == PT_NOTE))
3695 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3696 elf_sort_sections);
3698 p->p_type = m->p_type;
3699 p->p_flags = m->p_flags;
3701 if (p->p_type == PT_LOAD
3702 && m->count > 0
3703 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3705 if ((abfd->flags & D_PAGED) != 0)
3706 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3707 else
3709 bfd_size_type align;
3711 align = 0;
3712 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3714 bfd_size_type secalign;
3716 secalign = bfd_get_section_alignment (abfd, *secpp);
3717 if (secalign > align)
3718 align = secalign;
3721 off += (m->sections[0]->vma - off) % (1 << align);
3725 if (m->count == 0)
3726 p->p_vaddr = 0;
3727 else
3728 p->p_vaddr = m->sections[0]->vma;
3730 if (m->p_paddr_valid)
3731 p->p_paddr = m->p_paddr;
3732 else if (m->count == 0)
3733 p->p_paddr = 0;
3734 else
3735 p->p_paddr = m->sections[0]->lma;
3737 if (p->p_type == PT_LOAD
3738 && (abfd->flags & D_PAGED) != 0)
3739 p->p_align = bed->maxpagesize;
3740 else if (m->count == 0)
3741 p->p_align = bed->s->file_align;
3742 else
3743 p->p_align = 0;
3745 p->p_offset = 0;
3746 p->p_filesz = 0;
3747 p->p_memsz = 0;
3749 if (m->includes_filehdr)
3751 if (! m->p_flags_valid)
3752 p->p_flags |= PF_R;
3753 p->p_offset = 0;
3754 p->p_filesz = bed->s->sizeof_ehdr;
3755 p->p_memsz = bed->s->sizeof_ehdr;
3756 if (m->count > 0)
3758 BFD_ASSERT (p->p_type == PT_LOAD);
3760 if (p->p_vaddr < (bfd_vma) off)
3762 (*_bfd_error_handler)
3763 (_("%s: Not enough room for program headers, try linking with -N"),
3764 bfd_get_filename (abfd));
3765 bfd_set_error (bfd_error_bad_value);
3766 return FALSE;
3769 p->p_vaddr -= off;
3770 if (! m->p_paddr_valid)
3771 p->p_paddr -= off;
3773 if (p->p_type == PT_LOAD)
3775 filehdr_vaddr = p->p_vaddr;
3776 filehdr_paddr = p->p_paddr;
3780 if (m->includes_phdrs)
3782 if (! m->p_flags_valid)
3783 p->p_flags |= PF_R;
3785 if (m->includes_filehdr)
3787 if (p->p_type == PT_LOAD)
3789 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3790 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3793 else
3795 p->p_offset = bed->s->sizeof_ehdr;
3797 if (m->count > 0)
3799 BFD_ASSERT (p->p_type == PT_LOAD);
3800 p->p_vaddr -= off - p->p_offset;
3801 if (! m->p_paddr_valid)
3802 p->p_paddr -= off - p->p_offset;
3805 if (p->p_type == PT_LOAD)
3807 phdrs_vaddr = p->p_vaddr;
3808 phdrs_paddr = p->p_paddr;
3810 else
3811 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3814 p->p_filesz += alloc * bed->s->sizeof_phdr;
3815 p->p_memsz += alloc * bed->s->sizeof_phdr;
3818 if (p->p_type == PT_LOAD
3819 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3821 if (! m->includes_filehdr && ! m->includes_phdrs)
3822 p->p_offset = off;
3823 else
3825 file_ptr adjust;
3827 adjust = off - (p->p_offset + p->p_filesz);
3828 p->p_filesz += adjust;
3829 p->p_memsz += adjust;
3833 voff = off;
3835 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3837 asection *sec;
3838 flagword flags;
3839 bfd_size_type align;
3841 sec = *secpp;
3842 flags = sec->flags;
3843 align = 1 << bfd_get_section_alignment (abfd, sec);
3845 /* The section may have artificial alignment forced by a
3846 link script. Notice this case by the gap between the
3847 cumulative phdr lma and the section's lma. */
3848 if (p->p_paddr + p->p_memsz < sec->lma)
3850 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3852 p->p_memsz += adjust;
3853 if (p->p_type == PT_LOAD
3854 || (p->p_type == PT_NOTE
3855 && bfd_get_format (abfd) == bfd_core))
3857 off += adjust;
3858 voff += adjust;
3860 if ((flags & SEC_LOAD) != 0
3861 || (flags & SEC_THREAD_LOCAL) != 0)
3862 p->p_filesz += adjust;
3865 if (p->p_type == PT_LOAD)
3867 bfd_signed_vma adjust;
3869 if ((flags & SEC_LOAD) != 0)
3871 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3872 if (adjust < 0)
3873 adjust = 0;
3875 else if ((flags & SEC_ALLOC) != 0)
3877 /* The section VMA must equal the file position
3878 modulo the page size. FIXME: I'm not sure if
3879 this adjustment is really necessary. We used to
3880 not have the SEC_LOAD case just above, and then
3881 this was necessary, but now I'm not sure. */
3882 if ((abfd->flags & D_PAGED) != 0)
3883 adjust = (sec->vma - voff) % bed->maxpagesize;
3884 else
3885 adjust = (sec->vma - voff) % align;
3887 else
3888 adjust = 0;
3890 if (adjust != 0)
3892 if (i == 0)
3894 (* _bfd_error_handler) (_("\
3895 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3896 bfd_section_name (abfd, sec),
3897 sec->lma,
3898 p->p_paddr);
3899 return FALSE;
3901 p->p_memsz += adjust;
3902 off += adjust;
3903 voff += adjust;
3904 if ((flags & SEC_LOAD) != 0)
3905 p->p_filesz += adjust;
3908 sec->filepos = off;
3910 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3911 used in a linker script we may have a section with
3912 SEC_LOAD clear but which is supposed to have
3913 contents. */
3914 if ((flags & SEC_LOAD) != 0
3915 || (flags & SEC_HAS_CONTENTS) != 0)
3916 off += sec->_raw_size;
3918 if ((flags & SEC_ALLOC) != 0
3919 && ((flags & SEC_LOAD) != 0
3920 || (flags & SEC_THREAD_LOCAL) == 0))
3921 voff += sec->_raw_size;
3924 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3926 /* The actual "note" segment has i == 0.
3927 This is the one that actually contains everything. */
3928 if (i == 0)
3930 sec->filepos = off;
3931 p->p_filesz = sec->_raw_size;
3932 off += sec->_raw_size;
3933 voff = off;
3935 else
3937 /* Fake sections -- don't need to be written. */
3938 sec->filepos = 0;
3939 sec->_raw_size = 0;
3940 flags = sec->flags = 0;
3942 p->p_memsz = 0;
3943 p->p_align = 1;
3945 else
3947 if ((sec->flags & SEC_LOAD) != 0
3948 || (sec->flags & SEC_THREAD_LOCAL) == 0
3949 || p->p_type == PT_TLS)
3950 p->p_memsz += sec->_raw_size;
3952 if ((flags & SEC_LOAD) != 0)
3953 p->p_filesz += sec->_raw_size;
3955 if (p->p_type == PT_TLS
3956 && sec->_raw_size == 0
3957 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3959 struct bfd_link_order *o;
3960 bfd_vma tbss_size = 0;
3962 for (o = sec->link_order_head; o != NULL; o = o->next)
3963 if (tbss_size < o->offset + o->size)
3964 tbss_size = o->offset + o->size;
3966 p->p_memsz += tbss_size;
3969 if (align > p->p_align
3970 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3971 p->p_align = align;
3974 if (! m->p_flags_valid)
3976 p->p_flags |= PF_R;
3977 if ((flags & SEC_CODE) != 0)
3978 p->p_flags |= PF_X;
3979 if ((flags & SEC_READONLY) == 0)
3980 p->p_flags |= PF_W;
3985 /* Now that we have set the section file positions, we can set up
3986 the file positions for the non PT_LOAD segments. */
3987 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3988 m != NULL;
3989 m = m->next, p++)
3991 if (p->p_type != PT_LOAD && m->count > 0)
3993 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3994 p->p_offset = m->sections[0]->filepos;
3996 if (m->count == 0)
3998 if (m->includes_filehdr)
4000 p->p_vaddr = filehdr_vaddr;
4001 if (! m->p_paddr_valid)
4002 p->p_paddr = filehdr_paddr;
4004 else if (m->includes_phdrs)
4006 p->p_vaddr = phdrs_vaddr;
4007 if (! m->p_paddr_valid)
4008 p->p_paddr = phdrs_paddr;
4013 /* Clear out any program headers we allocated but did not use. */
4014 for (; count < alloc; count++, p++)
4016 memset (p, 0, sizeof *p);
4017 p->p_type = PT_NULL;
4020 elf_tdata (abfd)->phdr = phdrs;
4022 elf_tdata (abfd)->next_file_pos = off;
4024 /* Write out the program headers. */
4025 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4026 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4027 return FALSE;
4029 return TRUE;
4032 /* Get the size of the program header.
4034 If this is called by the linker before any of the section VMA's are set, it
4035 can't calculate the correct value for a strange memory layout. This only
4036 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4037 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4038 data segment (exclusive of .interp and .dynamic).
4040 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4041 will be two segments. */
4043 static bfd_size_type
4044 get_program_header_size (abfd)
4045 bfd *abfd;
4047 size_t segs;
4048 asection *s;
4049 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4051 /* We can't return a different result each time we're called. */
4052 if (elf_tdata (abfd)->program_header_size != 0)
4053 return elf_tdata (abfd)->program_header_size;
4055 if (elf_tdata (abfd)->segment_map != NULL)
4057 struct elf_segment_map *m;
4059 segs = 0;
4060 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4061 ++segs;
4062 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4063 return elf_tdata (abfd)->program_header_size;
4066 /* Assume we will need exactly two PT_LOAD segments: one for text
4067 and one for data. */
4068 segs = 2;
4070 s = bfd_get_section_by_name (abfd, ".interp");
4071 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4073 /* If we have a loadable interpreter section, we need a
4074 PT_INTERP segment. In this case, assume we also need a
4075 PT_PHDR segment, although that may not be true for all
4076 targets. */
4077 segs += 2;
4080 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4082 /* We need a PT_DYNAMIC segment. */
4083 ++segs;
4086 if (elf_tdata (abfd)->eh_frame_hdr)
4088 /* We need a PT_GNU_EH_FRAME segment. */
4089 ++segs;
4092 for (s = abfd->sections; s != NULL; s = s->next)
4094 if ((s->flags & SEC_LOAD) != 0
4095 && strncmp (s->name, ".note", 5) == 0)
4097 /* We need a PT_NOTE segment. */
4098 ++segs;
4102 for (s = abfd->sections; s != NULL; s = s->next)
4104 if (s->flags & SEC_THREAD_LOCAL)
4106 /* We need a PT_TLS segment. */
4107 ++segs;
4108 break;
4112 /* Let the backend count up any program headers it might need. */
4113 if (bed->elf_backend_additional_program_headers)
4115 int a;
4117 a = (*bed->elf_backend_additional_program_headers) (abfd);
4118 if (a == -1)
4119 abort ();
4120 segs += a;
4123 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4124 return elf_tdata (abfd)->program_header_size;
4127 /* Work out the file positions of all the sections. This is called by
4128 _bfd_elf_compute_section_file_positions. All the section sizes and
4129 VMAs must be known before this is called.
4131 We do not consider reloc sections at this point, unless they form
4132 part of the loadable image. Reloc sections are assigned file
4133 positions in assign_file_positions_for_relocs, which is called by
4134 write_object_contents and final_link.
4136 We also don't set the positions of the .symtab and .strtab here. */
4138 static bfd_boolean
4139 assign_file_positions_except_relocs (abfd)
4140 bfd *abfd;
4142 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4143 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4144 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4145 unsigned int num_sec = elf_numsections (abfd);
4146 file_ptr off;
4147 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4149 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4150 && bfd_get_format (abfd) != bfd_core)
4152 Elf_Internal_Shdr **hdrpp;
4153 unsigned int i;
4155 /* Start after the ELF header. */
4156 off = i_ehdrp->e_ehsize;
4158 /* We are not creating an executable, which means that we are
4159 not creating a program header, and that the actual order of
4160 the sections in the file is unimportant. */
4161 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4163 Elf_Internal_Shdr *hdr;
4165 hdr = *hdrpp;
4166 if (hdr->sh_type == SHT_REL
4167 || hdr->sh_type == SHT_RELA
4168 || i == tdata->symtab_section
4169 || i == tdata->symtab_shndx_section
4170 || i == tdata->strtab_section)
4172 hdr->sh_offset = -1;
4174 else
4175 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4177 if (i == SHN_LORESERVE - 1)
4179 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4180 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4184 else
4186 unsigned int i;
4187 Elf_Internal_Shdr **hdrpp;
4189 /* Assign file positions for the loaded sections based on the
4190 assignment of sections to segments. */
4191 if (! assign_file_positions_for_segments (abfd))
4192 return FALSE;
4194 /* Assign file positions for the other sections. */
4196 off = elf_tdata (abfd)->next_file_pos;
4197 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4199 Elf_Internal_Shdr *hdr;
4201 hdr = *hdrpp;
4202 if (hdr->bfd_section != NULL
4203 && hdr->bfd_section->filepos != 0)
4204 hdr->sh_offset = hdr->bfd_section->filepos;
4205 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4207 ((*_bfd_error_handler)
4208 (_("%s: warning: allocated section `%s' not in segment"),
4209 bfd_get_filename (abfd),
4210 (hdr->bfd_section == NULL
4211 ? "*unknown*"
4212 : hdr->bfd_section->name)));
4213 if ((abfd->flags & D_PAGED) != 0)
4214 off += (hdr->sh_addr - off) % bed->maxpagesize;
4215 else
4216 off += (hdr->sh_addr - off) % hdr->sh_addralign;
4217 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4218 FALSE);
4220 else if (hdr->sh_type == SHT_REL
4221 || hdr->sh_type == SHT_RELA
4222 || hdr == i_shdrpp[tdata->symtab_section]
4223 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4224 || hdr == i_shdrpp[tdata->strtab_section])
4225 hdr->sh_offset = -1;
4226 else
4227 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4229 if (i == SHN_LORESERVE - 1)
4231 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4232 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4237 /* Place the section headers. */
4238 off = align_file_position (off, bed->s->file_align);
4239 i_ehdrp->e_shoff = off;
4240 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4242 elf_tdata (abfd)->next_file_pos = off;
4244 return TRUE;
4247 static bfd_boolean
4248 prep_headers (abfd)
4249 bfd *abfd;
4251 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4252 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4253 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4254 struct elf_strtab_hash *shstrtab;
4255 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4257 i_ehdrp = elf_elfheader (abfd);
4258 i_shdrp = elf_elfsections (abfd);
4260 shstrtab = _bfd_elf_strtab_init ();
4261 if (shstrtab == NULL)
4262 return FALSE;
4264 elf_shstrtab (abfd) = shstrtab;
4266 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4267 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4268 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4269 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4271 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4272 i_ehdrp->e_ident[EI_DATA] =
4273 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4274 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4276 if ((abfd->flags & DYNAMIC) != 0)
4277 i_ehdrp->e_type = ET_DYN;
4278 else if ((abfd->flags & EXEC_P) != 0)
4279 i_ehdrp->e_type = ET_EXEC;
4280 else if (bfd_get_format (abfd) == bfd_core)
4281 i_ehdrp->e_type = ET_CORE;
4282 else
4283 i_ehdrp->e_type = ET_REL;
4285 switch (bfd_get_arch (abfd))
4287 case bfd_arch_unknown:
4288 i_ehdrp->e_machine = EM_NONE;
4289 break;
4291 /* There used to be a long list of cases here, each one setting
4292 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4293 in the corresponding bfd definition. To avoid duplication,
4294 the switch was removed. Machines that need special handling
4295 can generally do it in elf_backend_final_write_processing(),
4296 unless they need the information earlier than the final write.
4297 Such need can generally be supplied by replacing the tests for
4298 e_machine with the conditions used to determine it. */
4299 default:
4300 if (get_elf_backend_data (abfd) != NULL)
4301 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
4302 else
4303 i_ehdrp->e_machine = EM_NONE;
4306 i_ehdrp->e_version = bed->s->ev_current;
4307 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4309 /* No program header, for now. */
4310 i_ehdrp->e_phoff = 0;
4311 i_ehdrp->e_phentsize = 0;
4312 i_ehdrp->e_phnum = 0;
4314 /* Each bfd section is section header entry. */
4315 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4316 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4318 /* If we're building an executable, we'll need a program header table. */
4319 if (abfd->flags & EXEC_P)
4321 /* It all happens later. */
4322 #if 0
4323 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4325 /* elf_build_phdrs() returns a (NULL-terminated) array of
4326 Elf_Internal_Phdrs. */
4327 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4328 i_ehdrp->e_phoff = outbase;
4329 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4330 #endif
4332 else
4334 i_ehdrp->e_phentsize = 0;
4335 i_phdrp = 0;
4336 i_ehdrp->e_phoff = 0;
4339 elf_tdata (abfd)->symtab_hdr.sh_name =
4340 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4341 elf_tdata (abfd)->strtab_hdr.sh_name =
4342 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4343 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4344 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4345 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4346 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4347 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4348 return FALSE;
4350 return TRUE;
4353 /* Assign file positions for all the reloc sections which are not part
4354 of the loadable file image. */
4356 void
4357 _bfd_elf_assign_file_positions_for_relocs (abfd)
4358 bfd *abfd;
4360 file_ptr off;
4361 unsigned int i, num_sec;
4362 Elf_Internal_Shdr **shdrpp;
4364 off = elf_tdata (abfd)->next_file_pos;
4366 num_sec = elf_numsections (abfd);
4367 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4369 Elf_Internal_Shdr *shdrp;
4371 shdrp = *shdrpp;
4372 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4373 && shdrp->sh_offset == -1)
4374 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4377 elf_tdata (abfd)->next_file_pos = off;
4380 bfd_boolean
4381 _bfd_elf_write_object_contents (abfd)
4382 bfd *abfd;
4384 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4385 Elf_Internal_Ehdr *i_ehdrp;
4386 Elf_Internal_Shdr **i_shdrp;
4387 bfd_boolean failed;
4388 unsigned int count, num_sec;
4390 if (! abfd->output_has_begun
4391 && ! _bfd_elf_compute_section_file_positions
4392 (abfd, (struct bfd_link_info *) NULL))
4393 return FALSE;
4395 i_shdrp = elf_elfsections (abfd);
4396 i_ehdrp = elf_elfheader (abfd);
4398 failed = FALSE;
4399 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4400 if (failed)
4401 return FALSE;
4403 _bfd_elf_assign_file_positions_for_relocs (abfd);
4405 /* After writing the headers, we need to write the sections too... */
4406 num_sec = elf_numsections (abfd);
4407 for (count = 1; count < num_sec; count++)
4409 if (bed->elf_backend_section_processing)
4410 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4411 if (i_shdrp[count]->contents)
4413 bfd_size_type amt = i_shdrp[count]->sh_size;
4415 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4416 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4417 return FALSE;
4419 if (count == SHN_LORESERVE - 1)
4420 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4423 /* Write out the section header names. */
4424 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4425 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4426 return FALSE;
4428 if (bed->elf_backend_final_write_processing)
4429 (*bed->elf_backend_final_write_processing) (abfd,
4430 elf_tdata (abfd)->linker);
4432 return bed->s->write_shdrs_and_ehdr (abfd);
4435 bfd_boolean
4436 _bfd_elf_write_corefile_contents (abfd)
4437 bfd *abfd;
4439 /* Hopefully this can be done just like an object file. */
4440 return _bfd_elf_write_object_contents (abfd);
4443 /* Given a section, search the header to find them. */
4446 _bfd_elf_section_from_bfd_section (abfd, asect)
4447 bfd *abfd;
4448 struct sec *asect;
4450 struct elf_backend_data *bed;
4451 int index;
4453 if (elf_section_data (asect) != NULL
4454 && elf_section_data (asect)->this_idx != 0)
4455 return elf_section_data (asect)->this_idx;
4457 if (bfd_is_abs_section (asect))
4458 index = SHN_ABS;
4459 else if (bfd_is_com_section (asect))
4460 index = SHN_COMMON;
4461 else if (bfd_is_und_section (asect))
4462 index = SHN_UNDEF;
4463 else
4465 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4466 int maxindex = elf_numsections (abfd);
4468 for (index = 1; index < maxindex; index++)
4470 Elf_Internal_Shdr *hdr = i_shdrp[index];
4472 if (hdr != NULL && hdr->bfd_section == asect)
4473 return index;
4475 index = -1;
4478 bed = get_elf_backend_data (abfd);
4479 if (bed->elf_backend_section_from_bfd_section)
4481 int retval = index;
4483 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4484 return retval;
4487 if (index == -1)
4488 bfd_set_error (bfd_error_nonrepresentable_section);
4490 return index;
4493 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4494 on error. */
4497 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4498 bfd *abfd;
4499 asymbol **asym_ptr_ptr;
4501 asymbol *asym_ptr = *asym_ptr_ptr;
4502 int idx;
4503 flagword flags = asym_ptr->flags;
4505 /* When gas creates relocations against local labels, it creates its
4506 own symbol for the section, but does put the symbol into the
4507 symbol chain, so udata is 0. When the linker is generating
4508 relocatable output, this section symbol may be for one of the
4509 input sections rather than the output section. */
4510 if (asym_ptr->udata.i == 0
4511 && (flags & BSF_SECTION_SYM)
4512 && asym_ptr->section)
4514 int indx;
4516 if (asym_ptr->section->output_section != NULL)
4517 indx = asym_ptr->section->output_section->index;
4518 else
4519 indx = asym_ptr->section->index;
4520 if (indx < elf_num_section_syms (abfd)
4521 && elf_section_syms (abfd)[indx] != NULL)
4522 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4525 idx = asym_ptr->udata.i;
4527 if (idx == 0)
4529 /* This case can occur when using --strip-symbol on a symbol
4530 which is used in a relocation entry. */
4531 (*_bfd_error_handler)
4532 (_("%s: symbol `%s' required but not present"),
4533 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4534 bfd_set_error (bfd_error_no_symbols);
4535 return -1;
4538 #if DEBUG & 4
4540 fprintf (stderr,
4541 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4542 (long) asym_ptr, asym_ptr->name, idx, flags,
4543 elf_symbol_flags (flags));
4544 fflush (stderr);
4546 #endif
4548 return idx;
4551 /* Copy private BFD data. This copies any program header information. */
4553 static bfd_boolean
4554 copy_private_bfd_data (ibfd, obfd)
4555 bfd *ibfd;
4556 bfd *obfd;
4558 Elf_Internal_Ehdr *iehdr;
4559 struct elf_segment_map *map;
4560 struct elf_segment_map *map_first;
4561 struct elf_segment_map **pointer_to_map;
4562 Elf_Internal_Phdr *segment;
4563 asection *section;
4564 unsigned int i;
4565 unsigned int num_segments;
4566 bfd_boolean phdr_included = FALSE;
4567 bfd_vma maxpagesize;
4568 struct elf_segment_map *phdr_adjust_seg = NULL;
4569 unsigned int phdr_adjust_num = 0;
4570 struct elf_backend_data *bed;
4572 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4573 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4574 return TRUE;
4576 if (elf_tdata (ibfd)->phdr == NULL)
4577 return TRUE;
4579 bed = get_elf_backend_data (ibfd);
4580 iehdr = elf_elfheader (ibfd);
4582 map_first = NULL;
4583 pointer_to_map = &map_first;
4585 num_segments = elf_elfheader (ibfd)->e_phnum;
4586 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4588 /* Returns the end address of the segment + 1. */
4589 #define SEGMENT_END(segment, start) \
4590 (start + (segment->p_memsz > segment->p_filesz \
4591 ? segment->p_memsz : segment->p_filesz))
4593 #define SECTION_SIZE(section, segment) \
4594 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4595 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4596 ? section->_raw_size : 0)
4598 /* Returns TRUE if the given section is contained within
4599 the given segment. VMA addresses are compared. */
4600 #define IS_CONTAINED_BY_VMA(section, segment) \
4601 (section->vma >= segment->p_vaddr \
4602 && (section->vma + SECTION_SIZE (section, segment) \
4603 <= (SEGMENT_END (segment, segment->p_vaddr))))
4605 /* Returns TRUE if the given section is contained within
4606 the given segment. LMA addresses are compared. */
4607 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4608 (section->lma >= base \
4609 && (section->lma + SECTION_SIZE (section, segment) \
4610 <= SEGMENT_END (segment, base)))
4612 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4613 #define IS_COREFILE_NOTE(p, s) \
4614 (p->p_type == PT_NOTE \
4615 && bfd_get_format (ibfd) == bfd_core \
4616 && s->vma == 0 && s->lma == 0 \
4617 && (bfd_vma) s->filepos >= p->p_offset \
4618 && ((bfd_vma) s->filepos + s->_raw_size \
4619 <= p->p_offset + p->p_filesz))
4621 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4622 linker, which generates a PT_INTERP section with p_vaddr and
4623 p_memsz set to 0. */
4624 #define IS_SOLARIS_PT_INTERP(p, s) \
4625 (p->p_vaddr == 0 \
4626 && p->p_paddr == 0 \
4627 && p->p_memsz == 0 \
4628 && p->p_filesz > 0 \
4629 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4630 && s->_raw_size > 0 \
4631 && (bfd_vma) s->filepos >= p->p_offset \
4632 && ((bfd_vma) s->filepos + s->_raw_size \
4633 <= p->p_offset + p->p_filesz))
4635 /* Decide if the given section should be included in the given segment.
4636 A section will be included if:
4637 1. It is within the address space of the segment -- we use the LMA
4638 if that is set for the segment and the VMA otherwise,
4639 2. It is an allocated segment,
4640 3. There is an output section associated with it,
4641 4. The section has not already been allocated to a previous segment.
4642 5. PT_TLS segment includes only SHF_TLS sections.
4643 6. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4644 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4645 ((((segment->p_paddr \
4646 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4647 : IS_CONTAINED_BY_VMA (section, segment)) \
4648 && (section->flags & SEC_ALLOC) != 0) \
4649 || IS_COREFILE_NOTE (segment, section)) \
4650 && section->output_section != NULL \
4651 && (segment->p_type != PT_TLS \
4652 || (section->flags & SEC_THREAD_LOCAL)) \
4653 && (segment->p_type == PT_LOAD \
4654 || segment->p_type == PT_TLS \
4655 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4656 && ! section->segment_mark)
4658 /* Returns TRUE iff seg1 starts after the end of seg2. */
4659 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4660 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4662 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4663 their VMA address ranges and their LMA address ranges overlap.
4664 It is possible to have overlapping VMA ranges without overlapping LMA
4665 ranges. RedBoot images for example can have both .data and .bss mapped
4666 to the same VMA range, but with the .data section mapped to a different
4667 LMA. */
4668 #define SEGMENT_OVERLAPS(seg1, seg2) \
4669 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4670 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4671 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4672 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4674 /* Initialise the segment mark field. */
4675 for (section = ibfd->sections; section != NULL; section = section->next)
4676 section->segment_mark = FALSE;
4678 /* Scan through the segments specified in the program header
4679 of the input BFD. For this first scan we look for overlaps
4680 in the loadable segments. These can be created by weird
4681 parameters to objcopy. Also, fix some solaris weirdness. */
4682 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4683 i < num_segments;
4684 i++, segment++)
4686 unsigned int j;
4687 Elf_Internal_Phdr *segment2;
4689 if (segment->p_type == PT_INTERP)
4690 for (section = ibfd->sections; section; section = section->next)
4691 if (IS_SOLARIS_PT_INTERP (segment, section))
4693 /* Mininal change so that the normal section to segment
4694 assigment code will work. */
4695 segment->p_vaddr = section->vma;
4696 break;
4699 if (segment->p_type != PT_LOAD)
4700 continue;
4702 /* Determine if this segment overlaps any previous segments. */
4703 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4705 bfd_signed_vma extra_length;
4707 if (segment2->p_type != PT_LOAD
4708 || ! SEGMENT_OVERLAPS (segment, segment2))
4709 continue;
4711 /* Merge the two segments together. */
4712 if (segment2->p_vaddr < segment->p_vaddr)
4714 /* Extend SEGMENT2 to include SEGMENT and then delete
4715 SEGMENT. */
4716 extra_length =
4717 SEGMENT_END (segment, segment->p_vaddr)
4718 - SEGMENT_END (segment2, segment2->p_vaddr);
4720 if (extra_length > 0)
4722 segment2->p_memsz += extra_length;
4723 segment2->p_filesz += extra_length;
4726 segment->p_type = PT_NULL;
4728 /* Since we have deleted P we must restart the outer loop. */
4729 i = 0;
4730 segment = elf_tdata (ibfd)->phdr;
4731 break;
4733 else
4735 /* Extend SEGMENT to include SEGMENT2 and then delete
4736 SEGMENT2. */
4737 extra_length =
4738 SEGMENT_END (segment2, segment2->p_vaddr)
4739 - SEGMENT_END (segment, segment->p_vaddr);
4741 if (extra_length > 0)
4743 segment->p_memsz += extra_length;
4744 segment->p_filesz += extra_length;
4747 segment2->p_type = PT_NULL;
4752 /* The second scan attempts to assign sections to segments. */
4753 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4754 i < num_segments;
4755 i ++, segment ++)
4757 unsigned int section_count;
4758 asection ** sections;
4759 asection * output_section;
4760 unsigned int isec;
4761 bfd_vma matching_lma;
4762 bfd_vma suggested_lma;
4763 unsigned int j;
4764 bfd_size_type amt;
4766 if (segment->p_type == PT_NULL)
4767 continue;
4769 /* Compute how many sections might be placed into this segment. */
4770 for (section = ibfd->sections, section_count = 0;
4771 section != NULL;
4772 section = section->next)
4773 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4774 ++section_count;
4776 /* Allocate a segment map big enough to contain
4777 all of the sections we have selected. */
4778 amt = sizeof (struct elf_segment_map);
4779 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4780 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4781 if (map == NULL)
4782 return FALSE;
4784 /* Initialise the fields of the segment map. Default to
4785 using the physical address of the segment in the input BFD. */
4786 map->next = NULL;
4787 map->p_type = segment->p_type;
4788 map->p_flags = segment->p_flags;
4789 map->p_flags_valid = 1;
4790 map->p_paddr = segment->p_paddr;
4791 map->p_paddr_valid = 1;
4793 /* Determine if this segment contains the ELF file header
4794 and if it contains the program headers themselves. */
4795 map->includes_filehdr = (segment->p_offset == 0
4796 && segment->p_filesz >= iehdr->e_ehsize);
4798 map->includes_phdrs = 0;
4800 if (! phdr_included || segment->p_type != PT_LOAD)
4802 map->includes_phdrs =
4803 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4804 && (segment->p_offset + segment->p_filesz
4805 >= ((bfd_vma) iehdr->e_phoff
4806 + iehdr->e_phnum * iehdr->e_phentsize)));
4808 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4809 phdr_included = TRUE;
4812 if (section_count == 0)
4814 /* Special segments, such as the PT_PHDR segment, may contain
4815 no sections, but ordinary, loadable segments should contain
4816 something. They are allowed by the ELF spec however, so only
4817 a warning is produced. */
4818 if (segment->p_type == PT_LOAD)
4819 (*_bfd_error_handler)
4820 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4821 bfd_archive_filename (ibfd));
4823 map->count = 0;
4824 *pointer_to_map = map;
4825 pointer_to_map = &map->next;
4827 continue;
4830 /* Now scan the sections in the input BFD again and attempt
4831 to add their corresponding output sections to the segment map.
4832 The problem here is how to handle an output section which has
4833 been moved (ie had its LMA changed). There are four possibilities:
4835 1. None of the sections have been moved.
4836 In this case we can continue to use the segment LMA from the
4837 input BFD.
4839 2. All of the sections have been moved by the same amount.
4840 In this case we can change the segment's LMA to match the LMA
4841 of the first section.
4843 3. Some of the sections have been moved, others have not.
4844 In this case those sections which have not been moved can be
4845 placed in the current segment which will have to have its size,
4846 and possibly its LMA changed, and a new segment or segments will
4847 have to be created to contain the other sections.
4849 4. The sections have been moved, but not by the same amount.
4850 In this case we can change the segment's LMA to match the LMA
4851 of the first section and we will have to create a new segment
4852 or segments to contain the other sections.
4854 In order to save time, we allocate an array to hold the section
4855 pointers that we are interested in. As these sections get assigned
4856 to a segment, they are removed from this array. */
4858 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4859 to work around this long long bug. */
4860 amt = section_count * sizeof (asection *);
4861 sections = (asection **) bfd_malloc (amt);
4862 if (sections == NULL)
4863 return FALSE;
4865 /* Step One: Scan for segment vs section LMA conflicts.
4866 Also add the sections to the section array allocated above.
4867 Also add the sections to the current segment. In the common
4868 case, where the sections have not been moved, this means that
4869 we have completely filled the segment, and there is nothing
4870 more to do. */
4871 isec = 0;
4872 matching_lma = 0;
4873 suggested_lma = 0;
4875 for (j = 0, section = ibfd->sections;
4876 section != NULL;
4877 section = section->next)
4879 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4881 output_section = section->output_section;
4883 sections[j ++] = section;
4885 /* The Solaris native linker always sets p_paddr to 0.
4886 We try to catch that case here, and set it to the
4887 correct value. Note - some backends require that
4888 p_paddr be left as zero. */
4889 if (segment->p_paddr == 0
4890 && segment->p_vaddr != 0
4891 && (! bed->want_p_paddr_set_to_zero)
4892 && isec == 0
4893 && output_section->lma != 0
4894 && (output_section->vma == (segment->p_vaddr
4895 + (map->includes_filehdr
4896 ? iehdr->e_ehsize
4897 : 0)
4898 + (map->includes_phdrs
4899 ? (iehdr->e_phnum
4900 * iehdr->e_phentsize)
4901 : 0))))
4902 map->p_paddr = segment->p_vaddr;
4904 /* Match up the physical address of the segment with the
4905 LMA address of the output section. */
4906 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4907 || IS_COREFILE_NOTE (segment, section)
4908 || (bed->want_p_paddr_set_to_zero &&
4909 IS_CONTAINED_BY_VMA (output_section, segment))
4912 if (matching_lma == 0)
4913 matching_lma = output_section->lma;
4915 /* We assume that if the section fits within the segment
4916 then it does not overlap any other section within that
4917 segment. */
4918 map->sections[isec ++] = output_section;
4920 else if (suggested_lma == 0)
4921 suggested_lma = output_section->lma;
4925 BFD_ASSERT (j == section_count);
4927 /* Step Two: Adjust the physical address of the current segment,
4928 if necessary. */
4929 if (isec == section_count)
4931 /* All of the sections fitted within the segment as currently
4932 specified. This is the default case. Add the segment to
4933 the list of built segments and carry on to process the next
4934 program header in the input BFD. */
4935 map->count = section_count;
4936 *pointer_to_map = map;
4937 pointer_to_map = &map->next;
4939 free (sections);
4940 continue;
4942 else
4944 if (matching_lma != 0)
4946 /* At least one section fits inside the current segment.
4947 Keep it, but modify its physical address to match the
4948 LMA of the first section that fitted. */
4949 map->p_paddr = matching_lma;
4951 else
4953 /* None of the sections fitted inside the current segment.
4954 Change the current segment's physical address to match
4955 the LMA of the first section. */
4956 map->p_paddr = suggested_lma;
4959 /* Offset the segment physical address from the lma
4960 to allow for space taken up by elf headers. */
4961 if (map->includes_filehdr)
4962 map->p_paddr -= iehdr->e_ehsize;
4964 if (map->includes_phdrs)
4966 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4968 /* iehdr->e_phnum is just an estimate of the number
4969 of program headers that we will need. Make a note
4970 here of the number we used and the segment we chose
4971 to hold these headers, so that we can adjust the
4972 offset when we know the correct value. */
4973 phdr_adjust_num = iehdr->e_phnum;
4974 phdr_adjust_seg = map;
4978 /* Step Three: Loop over the sections again, this time assigning
4979 those that fit to the current segment and removing them from the
4980 sections array; but making sure not to leave large gaps. Once all
4981 possible sections have been assigned to the current segment it is
4982 added to the list of built segments and if sections still remain
4983 to be assigned, a new segment is constructed before repeating
4984 the loop. */
4985 isec = 0;
4988 map->count = 0;
4989 suggested_lma = 0;
4991 /* Fill the current segment with sections that fit. */
4992 for (j = 0; j < section_count; j++)
4994 section = sections[j];
4996 if (section == NULL)
4997 continue;
4999 output_section = section->output_section;
5001 BFD_ASSERT (output_section != NULL);
5003 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5004 || IS_COREFILE_NOTE (segment, section))
5006 if (map->count == 0)
5008 /* If the first section in a segment does not start at
5009 the beginning of the segment, then something is
5010 wrong. */
5011 if (output_section->lma !=
5012 (map->p_paddr
5013 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5014 + (map->includes_phdrs
5015 ? iehdr->e_phnum * iehdr->e_phentsize
5016 : 0)))
5017 abort ();
5019 else
5021 asection * prev_sec;
5023 prev_sec = map->sections[map->count - 1];
5025 /* If the gap between the end of the previous section
5026 and the start of this section is more than
5027 maxpagesize then we need to start a new segment. */
5028 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
5029 maxpagesize)
5030 < BFD_ALIGN (output_section->lma, maxpagesize))
5031 || ((prev_sec->lma + prev_sec->_raw_size)
5032 > output_section->lma))
5034 if (suggested_lma == 0)
5035 suggested_lma = output_section->lma;
5037 continue;
5041 map->sections[map->count++] = output_section;
5042 ++isec;
5043 sections[j] = NULL;
5044 section->segment_mark = TRUE;
5046 else if (suggested_lma == 0)
5047 suggested_lma = output_section->lma;
5050 BFD_ASSERT (map->count > 0);
5052 /* Add the current segment to the list of built segments. */
5053 *pointer_to_map = map;
5054 pointer_to_map = &map->next;
5056 if (isec < section_count)
5058 /* We still have not allocated all of the sections to
5059 segments. Create a new segment here, initialise it
5060 and carry on looping. */
5061 amt = sizeof (struct elf_segment_map);
5062 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5063 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5064 if (map == NULL)
5065 return FALSE;
5067 /* Initialise the fields of the segment map. Set the physical
5068 physical address to the LMA of the first section that has
5069 not yet been assigned. */
5070 map->next = NULL;
5071 map->p_type = segment->p_type;
5072 map->p_flags = segment->p_flags;
5073 map->p_flags_valid = 1;
5074 map->p_paddr = suggested_lma;
5075 map->p_paddr_valid = 1;
5076 map->includes_filehdr = 0;
5077 map->includes_phdrs = 0;
5080 while (isec < section_count);
5082 free (sections);
5085 /* The Solaris linker creates program headers in which all the
5086 p_paddr fields are zero. When we try to objcopy or strip such a
5087 file, we get confused. Check for this case, and if we find it
5088 reset the p_paddr_valid fields. */
5089 for (map = map_first; map != NULL; map = map->next)
5090 if (map->p_paddr != 0)
5091 break;
5092 if (map == NULL)
5093 for (map = map_first; map != NULL; map = map->next)
5094 map->p_paddr_valid = 0;
5096 elf_tdata (obfd)->segment_map = map_first;
5098 /* If we had to estimate the number of program headers that were
5099 going to be needed, then check our estimate now and adjust
5100 the offset if necessary. */
5101 if (phdr_adjust_seg != NULL)
5103 unsigned int count;
5105 for (count = 0, map = map_first; map != NULL; map = map->next)
5106 count++;
5108 if (count > phdr_adjust_num)
5109 phdr_adjust_seg->p_paddr
5110 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5113 #if 0
5114 /* Final Step: Sort the segments into ascending order of physical
5115 address. */
5116 if (map_first != NULL)
5118 struct elf_segment_map *prev;
5120 prev = map_first;
5121 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5123 /* Yes I know - its a bubble sort.... */
5124 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5126 /* Swap map and map->next. */
5127 prev->next = map->next;
5128 map->next = map->next->next;
5129 prev->next->next = map;
5131 /* Restart loop. */
5132 map = map_first;
5136 #endif
5138 #undef SEGMENT_END
5139 #undef SECTION_SIZE
5140 #undef IS_CONTAINED_BY_VMA
5141 #undef IS_CONTAINED_BY_LMA
5142 #undef IS_COREFILE_NOTE
5143 #undef IS_SOLARIS_PT_INTERP
5144 #undef INCLUDE_SECTION_IN_SEGMENT
5145 #undef SEGMENT_AFTER_SEGMENT
5146 #undef SEGMENT_OVERLAPS
5147 return TRUE;
5150 /* Copy private section information. This copies over the entsize
5151 field, and sometimes the info field. */
5153 bfd_boolean
5154 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
5155 bfd *ibfd;
5156 asection *isec;
5157 bfd *obfd;
5158 asection *osec;
5160 Elf_Internal_Shdr *ihdr, *ohdr;
5162 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5163 || obfd->xvec->flavour != bfd_target_elf_flavour)
5164 return TRUE;
5166 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5168 asection *s;
5170 /* Only set up the segments if there are no more SEC_ALLOC
5171 sections. FIXME: This won't do the right thing if objcopy is
5172 used to remove the last SEC_ALLOC section, since objcopy
5173 won't call this routine in that case. */
5174 for (s = isec->next; s != NULL; s = s->next)
5175 if ((s->flags & SEC_ALLOC) != 0)
5176 break;
5177 if (s == NULL)
5179 if (! copy_private_bfd_data (ibfd, obfd))
5180 return FALSE;
5184 ihdr = &elf_section_data (isec)->this_hdr;
5185 ohdr = &elf_section_data (osec)->this_hdr;
5187 ohdr->sh_entsize = ihdr->sh_entsize;
5189 if (ihdr->sh_type == SHT_SYMTAB
5190 || ihdr->sh_type == SHT_DYNSYM
5191 || ihdr->sh_type == SHT_GNU_verneed
5192 || ihdr->sh_type == SHT_GNU_verdef)
5193 ohdr->sh_info = ihdr->sh_info;
5195 /* Set things up for objcopy. The output SHT_GROUP section will
5196 have its elf_next_in_group pointing back to the input group
5197 members. */
5198 elf_next_in_group (osec) = elf_next_in_group (isec);
5199 elf_group_name (osec) = elf_group_name (isec);
5201 osec->use_rela_p = isec->use_rela_p;
5203 return TRUE;
5206 /* Copy private symbol information. If this symbol is in a section
5207 which we did not map into a BFD section, try to map the section
5208 index correctly. We use special macro definitions for the mapped
5209 section indices; these definitions are interpreted by the
5210 swap_out_syms function. */
5212 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5213 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5214 #define MAP_STRTAB (SHN_HIOS + 3)
5215 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5216 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5218 bfd_boolean
5219 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
5220 bfd *ibfd;
5221 asymbol *isymarg;
5222 bfd *obfd;
5223 asymbol *osymarg;
5225 elf_symbol_type *isym, *osym;
5227 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5228 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5229 return TRUE;
5231 isym = elf_symbol_from (ibfd, isymarg);
5232 osym = elf_symbol_from (obfd, osymarg);
5234 if (isym != NULL
5235 && osym != NULL
5236 && bfd_is_abs_section (isym->symbol.section))
5238 unsigned int shndx;
5240 shndx = isym->internal_elf_sym.st_shndx;
5241 if (shndx == elf_onesymtab (ibfd))
5242 shndx = MAP_ONESYMTAB;
5243 else if (shndx == elf_dynsymtab (ibfd))
5244 shndx = MAP_DYNSYMTAB;
5245 else if (shndx == elf_tdata (ibfd)->strtab_section)
5246 shndx = MAP_STRTAB;
5247 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5248 shndx = MAP_SHSTRTAB;
5249 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5250 shndx = MAP_SYM_SHNDX;
5251 osym->internal_elf_sym.st_shndx = shndx;
5254 return TRUE;
5257 /* Swap out the symbols. */
5259 static bfd_boolean
5260 swap_out_syms (abfd, sttp, relocatable_p)
5261 bfd *abfd;
5262 struct bfd_strtab_hash **sttp;
5263 int relocatable_p;
5265 struct elf_backend_data *bed;
5266 int symcount;
5267 asymbol **syms;
5268 struct bfd_strtab_hash *stt;
5269 Elf_Internal_Shdr *symtab_hdr;
5270 Elf_Internal_Shdr *symtab_shndx_hdr;
5271 Elf_Internal_Shdr *symstrtab_hdr;
5272 char *outbound_syms;
5273 char *outbound_shndx;
5274 int idx;
5275 bfd_size_type amt;
5277 if (!elf_map_symbols (abfd))
5278 return FALSE;
5280 /* Dump out the symtabs. */
5281 stt = _bfd_elf_stringtab_init ();
5282 if (stt == NULL)
5283 return FALSE;
5285 bed = get_elf_backend_data (abfd);
5286 symcount = bfd_get_symcount (abfd);
5287 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5288 symtab_hdr->sh_type = SHT_SYMTAB;
5289 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5290 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5291 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5292 symtab_hdr->sh_addralign = bed->s->file_align;
5294 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5295 symstrtab_hdr->sh_type = SHT_STRTAB;
5297 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5298 outbound_syms = bfd_alloc (abfd, amt);
5299 if (outbound_syms == NULL)
5300 return FALSE;
5301 symtab_hdr->contents = (PTR) outbound_syms;
5303 outbound_shndx = NULL;
5304 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5305 if (symtab_shndx_hdr->sh_name != 0)
5307 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5308 outbound_shndx = bfd_zalloc (abfd, amt);
5309 if (outbound_shndx == NULL)
5310 return FALSE;
5311 symtab_shndx_hdr->contents = outbound_shndx;
5312 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5313 symtab_shndx_hdr->sh_size = amt;
5314 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5315 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5318 /* Now generate the data (for "contents"). */
5320 /* Fill in zeroth symbol and swap it out. */
5321 Elf_Internal_Sym sym;
5322 sym.st_name = 0;
5323 sym.st_value = 0;
5324 sym.st_size = 0;
5325 sym.st_info = 0;
5326 sym.st_other = 0;
5327 sym.st_shndx = SHN_UNDEF;
5328 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5329 outbound_syms += bed->s->sizeof_sym;
5330 if (outbound_shndx != NULL)
5331 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5334 syms = bfd_get_outsymbols (abfd);
5335 for (idx = 0; idx < symcount; idx++)
5337 Elf_Internal_Sym sym;
5338 bfd_vma value = syms[idx]->value;
5339 elf_symbol_type *type_ptr;
5340 flagword flags = syms[idx]->flags;
5341 int type;
5343 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5345 /* Local section symbols have no name. */
5346 sym.st_name = 0;
5348 else
5350 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5351 syms[idx]->name,
5352 TRUE, FALSE);
5353 if (sym.st_name == (unsigned long) -1)
5354 return FALSE;
5357 type_ptr = elf_symbol_from (abfd, syms[idx]);
5359 if ((flags & BSF_SECTION_SYM) == 0
5360 && bfd_is_com_section (syms[idx]->section))
5362 /* ELF common symbols put the alignment into the `value' field,
5363 and the size into the `size' field. This is backwards from
5364 how BFD handles it, so reverse it here. */
5365 sym.st_size = value;
5366 if (type_ptr == NULL
5367 || type_ptr->internal_elf_sym.st_value == 0)
5368 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5369 else
5370 sym.st_value = type_ptr->internal_elf_sym.st_value;
5371 sym.st_shndx = _bfd_elf_section_from_bfd_section
5372 (abfd, syms[idx]->section);
5374 else
5376 asection *sec = syms[idx]->section;
5377 int shndx;
5379 if (sec->output_section)
5381 value += sec->output_offset;
5382 sec = sec->output_section;
5385 /* Don't add in the section vma for relocatable output. */
5386 if (! relocatable_p)
5387 value += sec->vma;
5388 sym.st_value = value;
5389 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5391 if (bfd_is_abs_section (sec)
5392 && type_ptr != NULL
5393 && type_ptr->internal_elf_sym.st_shndx != 0)
5395 /* This symbol is in a real ELF section which we did
5396 not create as a BFD section. Undo the mapping done
5397 by copy_private_symbol_data. */
5398 shndx = type_ptr->internal_elf_sym.st_shndx;
5399 switch (shndx)
5401 case MAP_ONESYMTAB:
5402 shndx = elf_onesymtab (abfd);
5403 break;
5404 case MAP_DYNSYMTAB:
5405 shndx = elf_dynsymtab (abfd);
5406 break;
5407 case MAP_STRTAB:
5408 shndx = elf_tdata (abfd)->strtab_section;
5409 break;
5410 case MAP_SHSTRTAB:
5411 shndx = elf_tdata (abfd)->shstrtab_section;
5412 break;
5413 case MAP_SYM_SHNDX:
5414 shndx = elf_tdata (abfd)->symtab_shndx_section;
5415 break;
5416 default:
5417 break;
5420 else
5422 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5424 if (shndx == -1)
5426 asection *sec2;
5428 /* Writing this would be a hell of a lot easier if
5429 we had some decent documentation on bfd, and
5430 knew what to expect of the library, and what to
5431 demand of applications. For example, it
5432 appears that `objcopy' might not set the
5433 section of a symbol to be a section that is
5434 actually in the output file. */
5435 sec2 = bfd_get_section_by_name (abfd, sec->name);
5436 if (sec2 == NULL)
5438 _bfd_error_handler (_("\
5439 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5440 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5441 sec->name);
5442 bfd_set_error (bfd_error_invalid_operation);
5443 return FALSE;
5446 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5447 BFD_ASSERT (shndx != -1);
5451 sym.st_shndx = shndx;
5454 if ((flags & BSF_THREAD_LOCAL) != 0)
5455 type = STT_TLS;
5456 else if ((flags & BSF_FUNCTION) != 0)
5457 type = STT_FUNC;
5458 else if ((flags & BSF_OBJECT) != 0)
5459 type = STT_OBJECT;
5460 else
5461 type = STT_NOTYPE;
5463 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5464 type = STT_TLS;
5466 /* Processor-specific types. */
5467 if (type_ptr != NULL
5468 && bed->elf_backend_get_symbol_type)
5469 type = ((*bed->elf_backend_get_symbol_type)
5470 (&type_ptr->internal_elf_sym, type));
5472 if (flags & BSF_SECTION_SYM)
5474 if (flags & BSF_GLOBAL)
5475 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5476 else
5477 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5479 else if (bfd_is_com_section (syms[idx]->section))
5480 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5481 else if (bfd_is_und_section (syms[idx]->section))
5482 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5483 ? STB_WEAK
5484 : STB_GLOBAL),
5485 type);
5486 else if (flags & BSF_FILE)
5487 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5488 else
5490 int bind = STB_LOCAL;
5492 if (flags & BSF_LOCAL)
5493 bind = STB_LOCAL;
5494 else if (flags & BSF_WEAK)
5495 bind = STB_WEAK;
5496 else if (flags & BSF_GLOBAL)
5497 bind = STB_GLOBAL;
5499 sym.st_info = ELF_ST_INFO (bind, type);
5502 if (type_ptr != NULL)
5503 sym.st_other = type_ptr->internal_elf_sym.st_other;
5504 else
5505 sym.st_other = 0;
5507 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5508 outbound_syms += bed->s->sizeof_sym;
5509 if (outbound_shndx != NULL)
5510 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5513 *sttp = stt;
5514 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5515 symstrtab_hdr->sh_type = SHT_STRTAB;
5517 symstrtab_hdr->sh_flags = 0;
5518 symstrtab_hdr->sh_addr = 0;
5519 symstrtab_hdr->sh_entsize = 0;
5520 symstrtab_hdr->sh_link = 0;
5521 symstrtab_hdr->sh_info = 0;
5522 symstrtab_hdr->sh_addralign = 1;
5524 return TRUE;
5527 /* Return the number of bytes required to hold the symtab vector.
5529 Note that we base it on the count plus 1, since we will null terminate
5530 the vector allocated based on this size. However, the ELF symbol table
5531 always has a dummy entry as symbol #0, so it ends up even. */
5533 long
5534 _bfd_elf_get_symtab_upper_bound (abfd)
5535 bfd *abfd;
5537 long symcount;
5538 long symtab_size;
5539 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5541 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5542 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5543 if (symcount > 0)
5544 symtab_size -= sizeof (asymbol *);
5546 return symtab_size;
5549 long
5550 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5551 bfd *abfd;
5553 long symcount;
5554 long symtab_size;
5555 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5557 if (elf_dynsymtab (abfd) == 0)
5559 bfd_set_error (bfd_error_invalid_operation);
5560 return -1;
5563 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5564 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5565 if (symcount > 0)
5566 symtab_size -= sizeof (asymbol *);
5568 return symtab_size;
5571 long
5572 _bfd_elf_get_reloc_upper_bound (abfd, asect)
5573 bfd *abfd ATTRIBUTE_UNUSED;
5574 sec_ptr asect;
5576 return (asect->reloc_count + 1) * sizeof (arelent *);
5579 /* Canonicalize the relocs. */
5581 long
5582 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5583 bfd *abfd;
5584 sec_ptr section;
5585 arelent **relptr;
5586 asymbol **symbols;
5588 arelent *tblptr;
5589 unsigned int i;
5590 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5592 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5593 return -1;
5595 tblptr = section->relocation;
5596 for (i = 0; i < section->reloc_count; i++)
5597 *relptr++ = tblptr++;
5599 *relptr = NULL;
5601 return section->reloc_count;
5604 long
5605 _bfd_elf_get_symtab (abfd, alocation)
5606 bfd *abfd;
5607 asymbol **alocation;
5609 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5610 long symcount = bed->s->slurp_symbol_table (abfd, alocation, FALSE);
5612 if (symcount >= 0)
5613 bfd_get_symcount (abfd) = symcount;
5614 return symcount;
5617 long
5618 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5619 bfd *abfd;
5620 asymbol **alocation;
5622 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5623 long symcount = bed->s->slurp_symbol_table (abfd, alocation, TRUE);
5625 if (symcount >= 0)
5626 bfd_get_dynamic_symcount (abfd) = symcount;
5627 return symcount;
5630 /* Return the size required for the dynamic reloc entries. Any
5631 section that was actually installed in the BFD, and has type
5632 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5633 considered to be a dynamic reloc section. */
5635 long
5636 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5637 bfd *abfd;
5639 long ret;
5640 asection *s;
5642 if (elf_dynsymtab (abfd) == 0)
5644 bfd_set_error (bfd_error_invalid_operation);
5645 return -1;
5648 ret = sizeof (arelent *);
5649 for (s = abfd->sections; s != NULL; s = s->next)
5650 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5651 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5652 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5653 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5654 * sizeof (arelent *));
5656 return ret;
5659 /* Canonicalize the dynamic relocation entries. Note that we return
5660 the dynamic relocations as a single block, although they are
5661 actually associated with particular sections; the interface, which
5662 was designed for SunOS style shared libraries, expects that there
5663 is only one set of dynamic relocs. Any section that was actually
5664 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5665 the dynamic symbol table, is considered to be a dynamic reloc
5666 section. */
5668 long
5669 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5670 bfd *abfd;
5671 arelent **storage;
5672 asymbol **syms;
5674 bfd_boolean (*slurp_relocs)
5675 PARAMS ((bfd *, asection *, asymbol **, bfd_boolean));
5676 asection *s;
5677 long ret;
5679 if (elf_dynsymtab (abfd) == 0)
5681 bfd_set_error (bfd_error_invalid_operation);
5682 return -1;
5685 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5686 ret = 0;
5687 for (s = abfd->sections; s != NULL; s = s->next)
5689 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5690 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5691 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5693 arelent *p;
5694 long count, i;
5696 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
5697 return -1;
5698 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5699 p = s->relocation;
5700 for (i = 0; i < count; i++)
5701 *storage++ = p++;
5702 ret += count;
5706 *storage = NULL;
5708 return ret;
5711 /* Read in the version information. */
5713 bfd_boolean
5714 _bfd_elf_slurp_version_tables (abfd)
5715 bfd *abfd;
5717 bfd_byte *contents = NULL;
5718 bfd_size_type amt;
5720 if (elf_dynverdef (abfd) != 0)
5722 Elf_Internal_Shdr *hdr;
5723 Elf_External_Verdef *everdef;
5724 Elf_Internal_Verdef *iverdef;
5725 Elf_Internal_Verdef *iverdefarr;
5726 Elf_Internal_Verdef iverdefmem;
5727 unsigned int i;
5728 unsigned int maxidx;
5730 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5732 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5733 if (contents == NULL)
5734 goto error_return;
5735 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5736 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5737 goto error_return;
5739 /* We know the number of entries in the section but not the maximum
5740 index. Therefore we have to run through all entries and find
5741 the maximum. */
5742 everdef = (Elf_External_Verdef *) contents;
5743 maxidx = 0;
5744 for (i = 0; i < hdr->sh_info; ++i)
5746 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5748 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5749 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5751 everdef = ((Elf_External_Verdef *)
5752 ((bfd_byte *) everdef + iverdefmem.vd_next));
5755 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5756 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5757 if (elf_tdata (abfd)->verdef == NULL)
5758 goto error_return;
5760 elf_tdata (abfd)->cverdefs = maxidx;
5762 everdef = (Elf_External_Verdef *) contents;
5763 iverdefarr = elf_tdata (abfd)->verdef;
5764 for (i = 0; i < hdr->sh_info; i++)
5766 Elf_External_Verdaux *everdaux;
5767 Elf_Internal_Verdaux *iverdaux;
5768 unsigned int j;
5770 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5772 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5773 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5775 iverdef->vd_bfd = abfd;
5777 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5778 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5779 if (iverdef->vd_auxptr == NULL)
5780 goto error_return;
5782 everdaux = ((Elf_External_Verdaux *)
5783 ((bfd_byte *) everdef + iverdef->vd_aux));
5784 iverdaux = iverdef->vd_auxptr;
5785 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5787 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5789 iverdaux->vda_nodename =
5790 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5791 iverdaux->vda_name);
5792 if (iverdaux->vda_nodename == NULL)
5793 goto error_return;
5795 if (j + 1 < iverdef->vd_cnt)
5796 iverdaux->vda_nextptr = iverdaux + 1;
5797 else
5798 iverdaux->vda_nextptr = NULL;
5800 everdaux = ((Elf_External_Verdaux *)
5801 ((bfd_byte *) everdaux + iverdaux->vda_next));
5804 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5806 if (i + 1 < hdr->sh_info)
5807 iverdef->vd_nextdef = iverdef + 1;
5808 else
5809 iverdef->vd_nextdef = NULL;
5811 everdef = ((Elf_External_Verdef *)
5812 ((bfd_byte *) everdef + iverdef->vd_next));
5815 free (contents);
5816 contents = NULL;
5819 if (elf_dynverref (abfd) != 0)
5821 Elf_Internal_Shdr *hdr;
5822 Elf_External_Verneed *everneed;
5823 Elf_Internal_Verneed *iverneed;
5824 unsigned int i;
5826 hdr = &elf_tdata (abfd)->dynverref_hdr;
5828 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5829 elf_tdata (abfd)->verref =
5830 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5831 if (elf_tdata (abfd)->verref == NULL)
5832 goto error_return;
5834 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5836 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5837 if (contents == NULL)
5838 goto error_return;
5839 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5840 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5841 goto error_return;
5843 everneed = (Elf_External_Verneed *) contents;
5844 iverneed = elf_tdata (abfd)->verref;
5845 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5847 Elf_External_Vernaux *evernaux;
5848 Elf_Internal_Vernaux *ivernaux;
5849 unsigned int j;
5851 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5853 iverneed->vn_bfd = abfd;
5855 iverneed->vn_filename =
5856 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5857 iverneed->vn_file);
5858 if (iverneed->vn_filename == NULL)
5859 goto error_return;
5861 amt = iverneed->vn_cnt;
5862 amt *= sizeof (Elf_Internal_Vernaux);
5863 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5865 evernaux = ((Elf_External_Vernaux *)
5866 ((bfd_byte *) everneed + iverneed->vn_aux));
5867 ivernaux = iverneed->vn_auxptr;
5868 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5870 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5872 ivernaux->vna_nodename =
5873 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5874 ivernaux->vna_name);
5875 if (ivernaux->vna_nodename == NULL)
5876 goto error_return;
5878 if (j + 1 < iverneed->vn_cnt)
5879 ivernaux->vna_nextptr = ivernaux + 1;
5880 else
5881 ivernaux->vna_nextptr = NULL;
5883 evernaux = ((Elf_External_Vernaux *)
5884 ((bfd_byte *) evernaux + ivernaux->vna_next));
5887 if (i + 1 < hdr->sh_info)
5888 iverneed->vn_nextref = iverneed + 1;
5889 else
5890 iverneed->vn_nextref = NULL;
5892 everneed = ((Elf_External_Verneed *)
5893 ((bfd_byte *) everneed + iverneed->vn_next));
5896 free (contents);
5897 contents = NULL;
5900 return TRUE;
5902 error_return:
5903 if (contents == NULL)
5904 free (contents);
5905 return FALSE;
5908 asymbol *
5909 _bfd_elf_make_empty_symbol (abfd)
5910 bfd *abfd;
5912 elf_symbol_type *newsym;
5913 bfd_size_type amt = sizeof (elf_symbol_type);
5915 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5916 if (!newsym)
5917 return NULL;
5918 else
5920 newsym->symbol.the_bfd = abfd;
5921 return &newsym->symbol;
5925 void
5926 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5927 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5928 asymbol *symbol;
5929 symbol_info *ret;
5931 bfd_symbol_info (symbol, ret);
5934 /* Return whether a symbol name implies a local symbol. Most targets
5935 use this function for the is_local_label_name entry point, but some
5936 override it. */
5938 bfd_boolean
5939 _bfd_elf_is_local_label_name (abfd, name)
5940 bfd *abfd ATTRIBUTE_UNUSED;
5941 const char *name;
5943 /* Normal local symbols start with ``.L''. */
5944 if (name[0] == '.' && name[1] == 'L')
5945 return TRUE;
5947 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5948 DWARF debugging symbols starting with ``..''. */
5949 if (name[0] == '.' && name[1] == '.')
5950 return TRUE;
5952 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5953 emitting DWARF debugging output. I suspect this is actually a
5954 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5955 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5956 underscore to be emitted on some ELF targets). For ease of use,
5957 we treat such symbols as local. */
5958 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5959 return TRUE;
5961 return FALSE;
5964 alent *
5965 _bfd_elf_get_lineno (ignore_abfd, symbol)
5966 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5967 asymbol *symbol ATTRIBUTE_UNUSED;
5969 abort ();
5970 return NULL;
5973 bfd_boolean
5974 _bfd_elf_set_arch_mach (abfd, arch, machine)
5975 bfd *abfd;
5976 enum bfd_architecture arch;
5977 unsigned long machine;
5979 /* If this isn't the right architecture for this backend, and this
5980 isn't the generic backend, fail. */
5981 if (arch != get_elf_backend_data (abfd)->arch
5982 && arch != bfd_arch_unknown
5983 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5984 return FALSE;
5986 return bfd_default_set_arch_mach (abfd, arch, machine);
5989 /* Find the function to a particular section and offset,
5990 for error reporting. */
5992 static bfd_boolean
5993 elf_find_function (abfd, section, symbols, offset,
5994 filename_ptr, functionname_ptr)
5995 bfd *abfd ATTRIBUTE_UNUSED;
5996 asection *section;
5997 asymbol **symbols;
5998 bfd_vma offset;
5999 const char **filename_ptr;
6000 const char **functionname_ptr;
6002 const char *filename;
6003 asymbol *func;
6004 bfd_vma low_func;
6005 asymbol **p;
6007 filename = NULL;
6008 func = NULL;
6009 low_func = 0;
6011 for (p = symbols; *p != NULL; p++)
6013 elf_symbol_type *q;
6015 q = (elf_symbol_type *) *p;
6017 if (bfd_get_section (&q->symbol) != section)
6018 continue;
6020 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6022 default:
6023 break;
6024 case STT_FILE:
6025 filename = bfd_asymbol_name (&q->symbol);
6026 break;
6027 case STT_NOTYPE:
6028 case STT_FUNC:
6029 if (q->symbol.section == section
6030 && q->symbol.value >= low_func
6031 && q->symbol.value <= offset)
6033 func = (asymbol *) q;
6034 low_func = q->symbol.value;
6036 break;
6040 if (func == NULL)
6041 return FALSE;
6043 if (filename_ptr)
6044 *filename_ptr = filename;
6045 if (functionname_ptr)
6046 *functionname_ptr = bfd_asymbol_name (func);
6048 return TRUE;
6051 /* Find the nearest line to a particular section and offset,
6052 for error reporting. */
6054 bfd_boolean
6055 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
6056 filename_ptr, functionname_ptr, line_ptr)
6057 bfd *abfd;
6058 asection *section;
6059 asymbol **symbols;
6060 bfd_vma offset;
6061 const char **filename_ptr;
6062 const char **functionname_ptr;
6063 unsigned int *line_ptr;
6065 bfd_boolean found;
6067 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6068 filename_ptr, functionname_ptr,
6069 line_ptr))
6071 if (!*functionname_ptr)
6072 elf_find_function (abfd, section, symbols, offset,
6073 *filename_ptr ? NULL : filename_ptr,
6074 functionname_ptr);
6076 return TRUE;
6079 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6080 filename_ptr, functionname_ptr,
6081 line_ptr, 0,
6082 &elf_tdata (abfd)->dwarf2_find_line_info))
6084 if (!*functionname_ptr)
6085 elf_find_function (abfd, section, symbols, offset,
6086 *filename_ptr ? NULL : filename_ptr,
6087 functionname_ptr);
6089 return TRUE;
6092 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6093 &found, filename_ptr,
6094 functionname_ptr, line_ptr,
6095 &elf_tdata (abfd)->line_info))
6096 return FALSE;
6097 if (found && (*functionname_ptr || *line_ptr))
6098 return TRUE;
6100 if (symbols == NULL)
6101 return FALSE;
6103 if (! elf_find_function (abfd, section, symbols, offset,
6104 filename_ptr, functionname_ptr))
6105 return FALSE;
6107 *line_ptr = 0;
6108 return TRUE;
6112 _bfd_elf_sizeof_headers (abfd, reloc)
6113 bfd *abfd;
6114 bfd_boolean reloc;
6116 int ret;
6118 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6119 if (! reloc)
6120 ret += get_program_header_size (abfd);
6121 return ret;
6124 bfd_boolean
6125 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
6126 bfd *abfd;
6127 sec_ptr section;
6128 PTR location;
6129 file_ptr offset;
6130 bfd_size_type count;
6132 Elf_Internal_Shdr *hdr;
6133 bfd_signed_vma pos;
6135 if (! abfd->output_has_begun
6136 && ! (_bfd_elf_compute_section_file_positions
6137 (abfd, (struct bfd_link_info *) NULL)))
6138 return FALSE;
6140 hdr = &elf_section_data (section)->this_hdr;
6141 pos = hdr->sh_offset + offset;
6142 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6143 || bfd_bwrite (location, count, abfd) != count)
6144 return FALSE;
6146 return TRUE;
6149 void
6150 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
6151 bfd *abfd ATTRIBUTE_UNUSED;
6152 arelent *cache_ptr ATTRIBUTE_UNUSED;
6153 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
6155 abort ();
6158 /* Try to convert a non-ELF reloc into an ELF one. */
6160 bfd_boolean
6161 _bfd_elf_validate_reloc (abfd, areloc)
6162 bfd *abfd;
6163 arelent *areloc;
6165 /* Check whether we really have an ELF howto. */
6167 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6169 bfd_reloc_code_real_type code;
6170 reloc_howto_type *howto;
6172 /* Alien reloc: Try to determine its type to replace it with an
6173 equivalent ELF reloc. */
6175 if (areloc->howto->pc_relative)
6177 switch (areloc->howto->bitsize)
6179 case 8:
6180 code = BFD_RELOC_8_PCREL;
6181 break;
6182 case 12:
6183 code = BFD_RELOC_12_PCREL;
6184 break;
6185 case 16:
6186 code = BFD_RELOC_16_PCREL;
6187 break;
6188 case 24:
6189 code = BFD_RELOC_24_PCREL;
6190 break;
6191 case 32:
6192 code = BFD_RELOC_32_PCREL;
6193 break;
6194 case 64:
6195 code = BFD_RELOC_64_PCREL;
6196 break;
6197 default:
6198 goto fail;
6201 howto = bfd_reloc_type_lookup (abfd, code);
6203 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6205 if (howto->pcrel_offset)
6206 areloc->addend += areloc->address;
6207 else
6208 areloc->addend -= areloc->address; /* addend is unsigned!! */
6211 else
6213 switch (areloc->howto->bitsize)
6215 case 8:
6216 code = BFD_RELOC_8;
6217 break;
6218 case 14:
6219 code = BFD_RELOC_14;
6220 break;
6221 case 16:
6222 code = BFD_RELOC_16;
6223 break;
6224 case 26:
6225 code = BFD_RELOC_26;
6226 break;
6227 case 32:
6228 code = BFD_RELOC_32;
6229 break;
6230 case 64:
6231 code = BFD_RELOC_64;
6232 break;
6233 default:
6234 goto fail;
6237 howto = bfd_reloc_type_lookup (abfd, code);
6240 if (howto)
6241 areloc->howto = howto;
6242 else
6243 goto fail;
6246 return TRUE;
6248 fail:
6249 (*_bfd_error_handler)
6250 (_("%s: unsupported relocation type %s"),
6251 bfd_archive_filename (abfd), areloc->howto->name);
6252 bfd_set_error (bfd_error_bad_value);
6253 return FALSE;
6256 bfd_boolean
6257 _bfd_elf_close_and_cleanup (abfd)
6258 bfd *abfd;
6260 if (bfd_get_format (abfd) == bfd_object)
6262 if (elf_shstrtab (abfd) != NULL)
6263 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6266 return _bfd_generic_close_and_cleanup (abfd);
6269 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6270 in the relocation's offset. Thus we cannot allow any sort of sanity
6271 range-checking to interfere. There is nothing else to do in processing
6272 this reloc. */
6274 bfd_reloc_status_type
6275 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
6276 bfd *abfd ATTRIBUTE_UNUSED;
6277 arelent *re ATTRIBUTE_UNUSED;
6278 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
6279 PTR data ATTRIBUTE_UNUSED;
6280 asection *is ATTRIBUTE_UNUSED;
6281 bfd *obfd ATTRIBUTE_UNUSED;
6282 char **errmsg ATTRIBUTE_UNUSED;
6284 return bfd_reloc_ok;
6287 /* Elf core file support. Much of this only works on native
6288 toolchains, since we rely on knowing the
6289 machine-dependent procfs structure in order to pick
6290 out details about the corefile. */
6292 #ifdef HAVE_SYS_PROCFS_H
6293 # include <sys/procfs.h>
6294 #endif
6296 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6298 static int
6299 elfcore_make_pid (abfd)
6300 bfd *abfd;
6302 return ((elf_tdata (abfd)->core_lwpid << 16)
6303 + (elf_tdata (abfd)->core_pid));
6306 /* If there isn't a section called NAME, make one, using
6307 data from SECT. Note, this function will generate a
6308 reference to NAME, so you shouldn't deallocate or
6309 overwrite it. */
6311 static bfd_boolean
6312 elfcore_maybe_make_sect (abfd, name, sect)
6313 bfd *abfd;
6314 char *name;
6315 asection *sect;
6317 asection *sect2;
6319 if (bfd_get_section_by_name (abfd, name) != NULL)
6320 return TRUE;
6322 sect2 = bfd_make_section (abfd, name);
6323 if (sect2 == NULL)
6324 return FALSE;
6326 sect2->_raw_size = sect->_raw_size;
6327 sect2->filepos = sect->filepos;
6328 sect2->flags = sect->flags;
6329 sect2->alignment_power = sect->alignment_power;
6330 return TRUE;
6333 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6334 actually creates up to two pseudosections:
6335 - For the single-threaded case, a section named NAME, unless
6336 such a section already exists.
6337 - For the multi-threaded case, a section named "NAME/PID", where
6338 PID is elfcore_make_pid (abfd).
6339 Both pseudosections have identical contents. */
6340 bfd_boolean
6341 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
6342 bfd *abfd;
6343 char *name;
6344 size_t size;
6345 ufile_ptr filepos;
6347 char buf[100];
6348 char *threaded_name;
6349 size_t len;
6350 asection *sect;
6352 /* Build the section name. */
6354 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6355 len = strlen (buf) + 1;
6356 threaded_name = bfd_alloc (abfd, (bfd_size_type) len);
6357 if (threaded_name == NULL)
6358 return FALSE;
6359 memcpy (threaded_name, buf, len);
6361 sect = bfd_make_section (abfd, threaded_name);
6362 if (sect == NULL)
6363 return FALSE;
6364 sect->_raw_size = size;
6365 sect->filepos = filepos;
6366 sect->flags = SEC_HAS_CONTENTS;
6367 sect->alignment_power = 2;
6369 return elfcore_maybe_make_sect (abfd, name, sect);
6372 /* prstatus_t exists on:
6373 solaris 2.5+
6374 linux 2.[01] + glibc
6375 unixware 4.2
6378 #if defined (HAVE_PRSTATUS_T)
6379 static bfd_boolean elfcore_grok_prstatus
6380 PARAMS ((bfd *, Elf_Internal_Note *));
6382 static bfd_boolean
6383 elfcore_grok_prstatus (abfd, note)
6384 bfd *abfd;
6385 Elf_Internal_Note *note;
6387 size_t raw_size;
6388 int offset;
6390 if (note->descsz == sizeof (prstatus_t))
6392 prstatus_t prstat;
6394 raw_size = sizeof (prstat.pr_reg);
6395 offset = offsetof (prstatus_t, pr_reg);
6396 memcpy (&prstat, note->descdata, sizeof (prstat));
6398 /* Do not overwrite the core signal if it
6399 has already been set by another thread. */
6400 if (elf_tdata (abfd)->core_signal == 0)
6401 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6402 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6404 /* pr_who exists on:
6405 solaris 2.5+
6406 unixware 4.2
6407 pr_who doesn't exist on:
6408 linux 2.[01]
6410 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6411 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6412 #endif
6414 #if defined (HAVE_PRSTATUS32_T)
6415 else if (note->descsz == sizeof (prstatus32_t))
6417 /* 64-bit host, 32-bit corefile */
6418 prstatus32_t prstat;
6420 raw_size = sizeof (prstat.pr_reg);
6421 offset = offsetof (prstatus32_t, pr_reg);
6422 memcpy (&prstat, note->descdata, sizeof (prstat));
6424 /* Do not overwrite the core signal if it
6425 has already been set by another thread. */
6426 if (elf_tdata (abfd)->core_signal == 0)
6427 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6428 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6430 /* pr_who exists on:
6431 solaris 2.5+
6432 unixware 4.2
6433 pr_who doesn't exist on:
6434 linux 2.[01]
6436 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6437 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6438 #endif
6440 #endif /* HAVE_PRSTATUS32_T */
6441 else
6443 /* Fail - we don't know how to handle any other
6444 note size (ie. data object type). */
6445 return TRUE;
6448 /* Make a ".reg/999" section and a ".reg" section. */
6449 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6450 raw_size, note->descpos + offset);
6452 #endif /* defined (HAVE_PRSTATUS_T) */
6454 /* Create a pseudosection containing the exact contents of NOTE. */
6455 static bfd_boolean
6456 elfcore_make_note_pseudosection (abfd, name, note)
6457 bfd *abfd;
6458 char *name;
6459 Elf_Internal_Note *note;
6461 return _bfd_elfcore_make_pseudosection (abfd, name,
6462 note->descsz, note->descpos);
6465 /* There isn't a consistent prfpregset_t across platforms,
6466 but it doesn't matter, because we don't have to pick this
6467 data structure apart. */
6469 static bfd_boolean
6470 elfcore_grok_prfpreg (abfd, note)
6471 bfd *abfd;
6472 Elf_Internal_Note *note;
6474 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6477 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6478 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6479 literally. */
6481 static bfd_boolean
6482 elfcore_grok_prxfpreg (abfd, note)
6483 bfd *abfd;
6484 Elf_Internal_Note *note;
6486 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6489 #if defined (HAVE_PRPSINFO_T)
6490 typedef prpsinfo_t elfcore_psinfo_t;
6491 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6492 typedef prpsinfo32_t elfcore_psinfo32_t;
6493 #endif
6494 #endif
6496 #if defined (HAVE_PSINFO_T)
6497 typedef psinfo_t elfcore_psinfo_t;
6498 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6499 typedef psinfo32_t elfcore_psinfo32_t;
6500 #endif
6501 #endif
6503 /* return a malloc'ed copy of a string at START which is at
6504 most MAX bytes long, possibly without a terminating '\0'.
6505 the copy will always have a terminating '\0'. */
6507 char *
6508 _bfd_elfcore_strndup (abfd, start, max)
6509 bfd *abfd;
6510 char *start;
6511 size_t max;
6513 char *dups;
6514 char *end = memchr (start, '\0', max);
6515 size_t len;
6517 if (end == NULL)
6518 len = max;
6519 else
6520 len = end - start;
6522 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6523 if (dups == NULL)
6524 return NULL;
6526 memcpy (dups, start, len);
6527 dups[len] = '\0';
6529 return dups;
6532 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6533 static bfd_boolean elfcore_grok_psinfo
6534 PARAMS ((bfd *, Elf_Internal_Note *));
6536 static bfd_boolean
6537 elfcore_grok_psinfo (abfd, note)
6538 bfd *abfd;
6539 Elf_Internal_Note *note;
6541 if (note->descsz == sizeof (elfcore_psinfo_t))
6543 elfcore_psinfo_t psinfo;
6545 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6547 elf_tdata (abfd)->core_program
6548 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6549 sizeof (psinfo.pr_fname));
6551 elf_tdata (abfd)->core_command
6552 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6553 sizeof (psinfo.pr_psargs));
6555 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6556 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6558 /* 64-bit host, 32-bit corefile */
6559 elfcore_psinfo32_t psinfo;
6561 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6563 elf_tdata (abfd)->core_program
6564 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6565 sizeof (psinfo.pr_fname));
6567 elf_tdata (abfd)->core_command
6568 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6569 sizeof (psinfo.pr_psargs));
6571 #endif
6573 else
6575 /* Fail - we don't know how to handle any other
6576 note size (ie. data object type). */
6577 return TRUE;
6580 /* Note that for some reason, a spurious space is tacked
6581 onto the end of the args in some (at least one anyway)
6582 implementations, so strip it off if it exists. */
6585 char *command = elf_tdata (abfd)->core_command;
6586 int n = strlen (command);
6588 if (0 < n && command[n - 1] == ' ')
6589 command[n - 1] = '\0';
6592 return TRUE;
6594 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6596 #if defined (HAVE_PSTATUS_T)
6597 static bfd_boolean elfcore_grok_pstatus
6598 PARAMS ((bfd *, Elf_Internal_Note *));
6600 static bfd_boolean
6601 elfcore_grok_pstatus (abfd, note)
6602 bfd *abfd;
6603 Elf_Internal_Note *note;
6605 if (note->descsz == sizeof (pstatus_t)
6606 #if defined (HAVE_PXSTATUS_T)
6607 || note->descsz == sizeof (pxstatus_t)
6608 #endif
6611 pstatus_t pstat;
6613 memcpy (&pstat, note->descdata, sizeof (pstat));
6615 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6617 #if defined (HAVE_PSTATUS32_T)
6618 else if (note->descsz == sizeof (pstatus32_t))
6620 /* 64-bit host, 32-bit corefile */
6621 pstatus32_t pstat;
6623 memcpy (&pstat, note->descdata, sizeof (pstat));
6625 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6627 #endif
6628 /* Could grab some more details from the "representative"
6629 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6630 NT_LWPSTATUS note, presumably. */
6632 return TRUE;
6634 #endif /* defined (HAVE_PSTATUS_T) */
6636 #if defined (HAVE_LWPSTATUS_T)
6637 static bfd_boolean elfcore_grok_lwpstatus
6638 PARAMS ((bfd *, Elf_Internal_Note *));
6640 static bfd_boolean
6641 elfcore_grok_lwpstatus (abfd, note)
6642 bfd *abfd;
6643 Elf_Internal_Note *note;
6645 lwpstatus_t lwpstat;
6646 char buf[100];
6647 char *name;
6648 size_t len;
6649 asection *sect;
6651 if (note->descsz != sizeof (lwpstat)
6652 #if defined (HAVE_LWPXSTATUS_T)
6653 && note->descsz != sizeof (lwpxstatus_t)
6654 #endif
6656 return TRUE;
6658 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6660 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6661 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6663 /* Make a ".reg/999" section. */
6665 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6666 len = strlen (buf) + 1;
6667 name = bfd_alloc (abfd, (bfd_size_type) len);
6668 if (name == NULL)
6669 return FALSE;
6670 memcpy (name, buf, len);
6672 sect = bfd_make_section (abfd, name);
6673 if (sect == NULL)
6674 return FALSE;
6676 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6677 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6678 sect->filepos = note->descpos
6679 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6680 #endif
6682 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6683 sect->_raw_size = sizeof (lwpstat.pr_reg);
6684 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6685 #endif
6687 sect->flags = SEC_HAS_CONTENTS;
6688 sect->alignment_power = 2;
6690 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6691 return FALSE;
6693 /* Make a ".reg2/999" section */
6695 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6696 len = strlen (buf) + 1;
6697 name = bfd_alloc (abfd, (bfd_size_type) len);
6698 if (name == NULL)
6699 return FALSE;
6700 memcpy (name, buf, len);
6702 sect = bfd_make_section (abfd, name);
6703 if (sect == NULL)
6704 return FALSE;
6706 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6707 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6708 sect->filepos = note->descpos
6709 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6710 #endif
6712 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6713 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6714 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6715 #endif
6717 sect->flags = SEC_HAS_CONTENTS;
6718 sect->alignment_power = 2;
6720 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6722 #endif /* defined (HAVE_LWPSTATUS_T) */
6724 #if defined (HAVE_WIN32_PSTATUS_T)
6725 static bfd_boolean
6726 elfcore_grok_win32pstatus (abfd, note)
6727 bfd *abfd;
6728 Elf_Internal_Note *note;
6730 char buf[30];
6731 char *name;
6732 size_t len;
6733 asection *sect;
6734 win32_pstatus_t pstatus;
6736 if (note->descsz < sizeof (pstatus))
6737 return TRUE;
6739 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6741 switch (pstatus.data_type)
6743 case NOTE_INFO_PROCESS:
6744 /* FIXME: need to add ->core_command. */
6745 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6746 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6747 break;
6749 case NOTE_INFO_THREAD:
6750 /* Make a ".reg/999" section. */
6751 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6753 len = strlen (buf) + 1;
6754 name = bfd_alloc (abfd, (bfd_size_type) len);
6755 if (name == NULL)
6756 return FALSE;
6758 memcpy (name, buf, len);
6760 sect = bfd_make_section (abfd, name);
6761 if (sect == NULL)
6762 return FALSE;
6764 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6765 sect->filepos = (note->descpos
6766 + offsetof (struct win32_pstatus,
6767 data.thread_info.thread_context));
6768 sect->flags = SEC_HAS_CONTENTS;
6769 sect->alignment_power = 2;
6771 if (pstatus.data.thread_info.is_active_thread)
6772 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6773 return FALSE;
6774 break;
6776 case NOTE_INFO_MODULE:
6777 /* Make a ".module/xxxxxxxx" section. */
6778 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6780 len = strlen (buf) + 1;
6781 name = bfd_alloc (abfd, (bfd_size_type) len);
6782 if (name == NULL)
6783 return FALSE;
6785 memcpy (name, buf, len);
6787 sect = bfd_make_section (abfd, name);
6789 if (sect == NULL)
6790 return FALSE;
6792 sect->_raw_size = note->descsz;
6793 sect->filepos = note->descpos;
6794 sect->flags = SEC_HAS_CONTENTS;
6795 sect->alignment_power = 2;
6796 break;
6798 default:
6799 return TRUE;
6802 return TRUE;
6804 #endif /* HAVE_WIN32_PSTATUS_T */
6806 static bfd_boolean
6807 elfcore_grok_note (abfd, note)
6808 bfd *abfd;
6809 Elf_Internal_Note *note;
6811 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6813 switch (note->type)
6815 default:
6816 return TRUE;
6818 case NT_PRSTATUS:
6819 if (bed->elf_backend_grok_prstatus)
6820 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6821 return TRUE;
6822 #if defined (HAVE_PRSTATUS_T)
6823 return elfcore_grok_prstatus (abfd, note);
6824 #else
6825 return TRUE;
6826 #endif
6828 #if defined (HAVE_PSTATUS_T)
6829 case NT_PSTATUS:
6830 return elfcore_grok_pstatus (abfd, note);
6831 #endif
6833 #if defined (HAVE_LWPSTATUS_T)
6834 case NT_LWPSTATUS:
6835 return elfcore_grok_lwpstatus (abfd, note);
6836 #endif
6838 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6839 return elfcore_grok_prfpreg (abfd, note);
6841 #if defined (HAVE_WIN32_PSTATUS_T)
6842 case NT_WIN32PSTATUS:
6843 return elfcore_grok_win32pstatus (abfd, note);
6844 #endif
6846 case NT_PRXFPREG: /* Linux SSE extension */
6847 if (note->namesz == 6
6848 && strcmp (note->namedata, "LINUX") == 0)
6849 return elfcore_grok_prxfpreg (abfd, note);
6850 else
6851 return TRUE;
6853 case NT_PRPSINFO:
6854 case NT_PSINFO:
6855 if (bed->elf_backend_grok_psinfo)
6856 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6857 return TRUE;
6858 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6859 return elfcore_grok_psinfo (abfd, note);
6860 #else
6861 return TRUE;
6862 #endif
6866 static bfd_boolean
6867 elfcore_netbsd_get_lwpid (note, lwpidp)
6868 Elf_Internal_Note *note;
6869 int *lwpidp;
6871 char *cp;
6873 cp = strchr (note->namedata, '@');
6874 if (cp != NULL)
6876 *lwpidp = atoi(cp + 1);
6877 return TRUE;
6879 return FALSE;
6882 static bfd_boolean
6883 elfcore_grok_netbsd_procinfo (abfd, note)
6884 bfd *abfd;
6885 Elf_Internal_Note *note;
6888 /* Signal number at offset 0x08. */
6889 elf_tdata (abfd)->core_signal
6890 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6892 /* Process ID at offset 0x50. */
6893 elf_tdata (abfd)->core_pid
6894 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6896 /* Command name at 0x7c (max 32 bytes, including nul). */
6897 elf_tdata (abfd)->core_command
6898 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6900 return TRUE;
6903 static bfd_boolean
6904 elfcore_grok_netbsd_note (abfd, note)
6905 bfd *abfd;
6906 Elf_Internal_Note *note;
6908 int lwp;
6910 if (elfcore_netbsd_get_lwpid (note, &lwp))
6911 elf_tdata (abfd)->core_lwpid = lwp;
6913 if (note->type == NT_NETBSDCORE_PROCINFO)
6915 /* NetBSD-specific core "procinfo". Note that we expect to
6916 find this note before any of the others, which is fine,
6917 since the kernel writes this note out first when it
6918 creates a core file. */
6920 return elfcore_grok_netbsd_procinfo (abfd, note);
6923 /* As of Jan 2002 there are no other machine-independent notes
6924 defined for NetBSD core files. If the note type is less
6925 than the start of the machine-dependent note types, we don't
6926 understand it. */
6928 if (note->type < NT_NETBSDCORE_FIRSTMACH)
6929 return TRUE;
6932 switch (bfd_get_arch (abfd))
6934 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6935 PT_GETFPREGS == mach+2. */
6937 case bfd_arch_alpha:
6938 case bfd_arch_sparc:
6939 switch (note->type)
6941 case NT_NETBSDCORE_FIRSTMACH+0:
6942 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6944 case NT_NETBSDCORE_FIRSTMACH+2:
6945 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6947 default:
6948 return TRUE;
6951 /* On all other arch's, PT_GETREGS == mach+1 and
6952 PT_GETFPREGS == mach+3. */
6954 default:
6955 switch (note->type)
6957 case NT_NETBSDCORE_FIRSTMACH+1:
6958 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6960 case NT_NETBSDCORE_FIRSTMACH+3:
6961 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6963 default:
6964 return TRUE;
6967 /* NOTREACHED */
6970 /* Function: elfcore_write_note
6972 Inputs:
6973 buffer to hold note
6974 name of note
6975 type of note
6976 data for note
6977 size of data for note
6979 Return:
6980 End of buffer containing note. */
6982 char *
6983 elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6984 bfd *abfd;
6985 char *buf;
6986 int *bufsiz;
6987 const char *name;
6988 int type;
6989 const PTR input;
6990 int size;
6992 Elf_External_Note *xnp;
6993 size_t namesz;
6994 size_t pad;
6995 size_t newspace;
6996 char *p, *dest;
6998 namesz = 0;
6999 pad = 0;
7000 if (name != NULL)
7002 struct elf_backend_data *bed;
7004 namesz = strlen (name) + 1;
7005 bed = get_elf_backend_data (abfd);
7006 pad = -namesz & (bed->s->file_align - 1);
7009 newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size;
7011 p = realloc (buf, *bufsiz + newspace);
7012 dest = p + *bufsiz;
7013 *bufsiz += newspace;
7014 xnp = (Elf_External_Note *) dest;
7015 H_PUT_32 (abfd, namesz, xnp->namesz);
7016 H_PUT_32 (abfd, size, xnp->descsz);
7017 H_PUT_32 (abfd, type, xnp->type);
7018 dest = xnp->name;
7019 if (name != NULL)
7021 memcpy (dest, name, namesz);
7022 dest += namesz;
7023 while (pad != 0)
7025 *dest++ = '\0';
7026 --pad;
7029 memcpy (dest, input, size);
7030 return p;
7033 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7034 char *
7035 elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
7036 bfd *abfd;
7037 char *buf;
7038 int *bufsiz;
7039 const char *fname;
7040 const char *psargs;
7042 int note_type;
7043 char *note_name = "CORE";
7045 #if defined (HAVE_PSINFO_T)
7046 psinfo_t data;
7047 note_type = NT_PSINFO;
7048 #else
7049 prpsinfo_t data;
7050 note_type = NT_PRPSINFO;
7051 #endif
7053 memset (&data, 0, sizeof (data));
7054 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7055 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7056 return elfcore_write_note (abfd, buf, bufsiz,
7057 note_name, note_type, &data, sizeof (data));
7059 #endif /* PSINFO_T or PRPSINFO_T */
7061 #if defined (HAVE_PRSTATUS_T)
7062 char *
7063 elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7064 bfd *abfd;
7065 char *buf;
7066 int *bufsiz;
7067 long pid;
7068 int cursig;
7069 const PTR gregs;
7071 prstatus_t prstat;
7072 char *note_name = "CORE";
7074 memset (&prstat, 0, sizeof (prstat));
7075 prstat.pr_pid = pid;
7076 prstat.pr_cursig = cursig;
7077 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7078 return elfcore_write_note (abfd, buf, bufsiz,
7079 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7081 #endif /* HAVE_PRSTATUS_T */
7083 #if defined (HAVE_LWPSTATUS_T)
7084 char *
7085 elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7086 bfd *abfd;
7087 char *buf;
7088 int *bufsiz;
7089 long pid;
7090 int cursig;
7091 const PTR gregs;
7093 lwpstatus_t lwpstat;
7094 char *note_name = "CORE";
7096 memset (&lwpstat, 0, sizeof (lwpstat));
7097 lwpstat.pr_lwpid = pid >> 16;
7098 lwpstat.pr_cursig = cursig;
7099 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7100 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7101 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7102 #if !defined(gregs)
7103 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7104 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7105 #else
7106 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7107 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7108 #endif
7109 #endif
7110 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7111 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7113 #endif /* HAVE_LWPSTATUS_T */
7115 #if defined (HAVE_PSTATUS_T)
7116 char *
7117 elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7118 bfd *abfd;
7119 char *buf;
7120 int *bufsiz;
7121 long pid;
7122 int cursig;
7123 const PTR gregs;
7125 pstatus_t pstat;
7126 char *note_name = "CORE";
7128 memset (&pstat, 0, sizeof (pstat));
7129 pstat.pr_pid = pid & 0xffff;
7130 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7131 NT_PSTATUS, &pstat, sizeof (pstat));
7132 return buf;
7134 #endif /* HAVE_PSTATUS_T */
7136 char *
7137 elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
7138 bfd *abfd;
7139 char *buf;
7140 int *bufsiz;
7141 const PTR fpregs;
7142 int size;
7144 char *note_name = "CORE";
7145 return elfcore_write_note (abfd, buf, bufsiz,
7146 note_name, NT_FPREGSET, fpregs, size);
7149 char *
7150 elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
7151 bfd *abfd;
7152 char *buf;
7153 int *bufsiz;
7154 const PTR xfpregs;
7155 int size;
7157 char *note_name = "LINUX";
7158 return elfcore_write_note (abfd, buf, bufsiz,
7159 note_name, NT_PRXFPREG, xfpregs, size);
7162 static bfd_boolean
7163 elfcore_read_notes (abfd, offset, size)
7164 bfd *abfd;
7165 file_ptr offset;
7166 bfd_size_type size;
7168 char *buf;
7169 char *p;
7171 if (size <= 0)
7172 return TRUE;
7174 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7175 return FALSE;
7177 buf = bfd_malloc (size);
7178 if (buf == NULL)
7179 return FALSE;
7181 if (bfd_bread (buf, size, abfd) != size)
7183 error:
7184 free (buf);
7185 return FALSE;
7188 p = buf;
7189 while (p < buf + size)
7191 /* FIXME: bad alignment assumption. */
7192 Elf_External_Note *xnp = (Elf_External_Note *) p;
7193 Elf_Internal_Note in;
7195 in.type = H_GET_32 (abfd, xnp->type);
7197 in.namesz = H_GET_32 (abfd, xnp->namesz);
7198 in.namedata = xnp->name;
7200 in.descsz = H_GET_32 (abfd, xnp->descsz);
7201 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7202 in.descpos = offset + (in.descdata - buf);
7204 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7206 if (! elfcore_grok_netbsd_note (abfd, &in))
7207 goto error;
7209 else
7211 if (! elfcore_grok_note (abfd, &in))
7212 goto error;
7215 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7218 free (buf);
7219 return TRUE;
7222 /* Providing external access to the ELF program header table. */
7224 /* Return an upper bound on the number of bytes required to store a
7225 copy of ABFD's program header table entries. Return -1 if an error
7226 occurs; bfd_get_error will return an appropriate code. */
7228 long
7229 bfd_get_elf_phdr_upper_bound (abfd)
7230 bfd *abfd;
7232 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7234 bfd_set_error (bfd_error_wrong_format);
7235 return -1;
7238 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7241 /* Copy ABFD's program header table entries to *PHDRS. The entries
7242 will be stored as an array of Elf_Internal_Phdr structures, as
7243 defined in include/elf/internal.h. To find out how large the
7244 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7246 Return the number of program header table entries read, or -1 if an
7247 error occurs; bfd_get_error will return an appropriate code. */
7250 bfd_get_elf_phdrs (abfd, phdrs)
7251 bfd *abfd;
7252 void *phdrs;
7254 int num_phdrs;
7256 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7258 bfd_set_error (bfd_error_wrong_format);
7259 return -1;
7262 num_phdrs = elf_elfheader (abfd)->e_phnum;
7263 memcpy (phdrs, elf_tdata (abfd)->phdr,
7264 num_phdrs * sizeof (Elf_Internal_Phdr));
7266 return num_phdrs;
7269 void
7270 _bfd_elf_sprintf_vma (abfd, buf, value)
7271 bfd *abfd ATTRIBUTE_UNUSED;
7272 char *buf;
7273 bfd_vma value;
7275 #ifdef BFD64
7276 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7278 i_ehdrp = elf_elfheader (abfd);
7279 if (i_ehdrp == NULL)
7280 sprintf_vma (buf, value);
7281 else
7283 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7285 #if BFD_HOST_64BIT_LONG
7286 sprintf (buf, "%016lx", value);
7287 #else
7288 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7289 _bfd_int64_low (value));
7290 #endif
7292 else
7293 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7295 #else
7296 sprintf_vma (buf, value);
7297 #endif
7300 void
7301 _bfd_elf_fprintf_vma (abfd, stream, value)
7302 bfd *abfd ATTRIBUTE_UNUSED;
7303 PTR stream;
7304 bfd_vma value;
7306 #ifdef BFD64
7307 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7309 i_ehdrp = elf_elfheader (abfd);
7310 if (i_ehdrp == NULL)
7311 fprintf_vma ((FILE *) stream, value);
7312 else
7314 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7316 #if BFD_HOST_64BIT_LONG
7317 fprintf ((FILE *) stream, "%016lx", value);
7318 #else
7319 fprintf ((FILE *) stream, "%08lx%08lx",
7320 _bfd_int64_high (value), _bfd_int64_low (value));
7321 #endif
7323 else
7324 fprintf ((FILE *) stream, "%08lx",
7325 (unsigned long) (value & 0xffffffff));
7327 #else
7328 fprintf_vma ((FILE *) stream, value);
7329 #endif
7332 enum elf_reloc_type_class
7333 _bfd_elf_reloc_type_class (rela)
7334 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
7336 return reloc_class_normal;
7339 /* For RELA architectures, return the relocation value for a
7340 relocation against a local symbol. */
7342 bfd_vma
7343 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
7344 bfd *abfd;
7345 Elf_Internal_Sym *sym;
7346 asection *sec;
7347 Elf_Internal_Rela *rel;
7349 bfd_vma relocation;
7351 relocation = (sec->output_section->vma
7352 + sec->output_offset
7353 + sym->st_value);
7354 if ((sec->flags & SEC_MERGE)
7355 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7356 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7358 asection *msec;
7360 msec = sec;
7361 rel->r_addend =
7362 _bfd_merged_section_offset (abfd, &msec,
7363 elf_section_data (sec)->sec_info,
7364 sym->st_value + rel->r_addend,
7365 (bfd_vma) 0)
7366 - relocation;
7367 rel->r_addend += msec->output_section->vma + msec->output_offset;
7369 return relocation;
7372 bfd_vma
7373 _bfd_elf_rel_local_sym (abfd, sym, psec, addend)
7374 bfd *abfd;
7375 Elf_Internal_Sym *sym;
7376 asection **psec;
7377 bfd_vma addend;
7379 asection *sec = *psec;
7381 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7382 return sym->st_value + addend;
7384 return _bfd_merged_section_offset (abfd, psec,
7385 elf_section_data (sec)->sec_info,
7386 sym->st_value + addend, (bfd_vma) 0);
7389 bfd_vma
7390 _bfd_elf_section_offset (abfd, info, sec, offset)
7391 bfd *abfd;
7392 struct bfd_link_info *info;
7393 asection *sec;
7394 bfd_vma offset;
7396 struct bfd_elf_section_data *sec_data;
7398 sec_data = elf_section_data (sec);
7399 switch (sec->sec_info_type)
7401 case ELF_INFO_TYPE_STABS:
7402 return _bfd_stab_section_offset (abfd,
7403 &elf_hash_table (info)->merge_info,
7404 sec, &sec_data->sec_info, offset);
7405 case ELF_INFO_TYPE_EH_FRAME:
7406 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7407 default:
7408 return offset;